§ INTEGRATION · OPENAI CHAT COMPLETIONS
OpenAI-Client-Wrapper
Umschließen Sie den openai-Client einmal — jeder chat.completions.create-Aufruf setzt Richtlinien für den Nutzer-Prompt durch und erfasst einen LLM-Span mit Modell und Token-Verbrauch.
Voraussetzungen
- Execlave-Konto mit aktivem API-Schlüssel — Einstellungen → API-Schlüssel.
- Ein registrierter Agent in Execlave. Verwenden Sie dessen lowercase-kebab
agent_id. - OpenAI SDK:
openai >=1.0(Python) oderopenai >=4(JS).
Installation
Die Instrumentierung ist im SDK enthalten; das openai-SDK ist die einzige zusätzliche Abhängigkeit, die Sie benötigen.
Python
pip install 'execlave-sdk[openai]'JavaScript / TypeScript
npm install @execlave/sdk openaiSofort-Einrichtung mit einem Copilot
Fügen Sie diesen Prompt in Ihren KI-Coding-Assistenten ein.
You are adding Execlave (an AI agent governance platform) to an existing project that calls the OpenAI Chat Completions API directly via the official `openai` SDK. Do not rewrite prompt construction — only wrap the client. Rules you MUST follow:1. Install the correct extra. Python: `pip install 'execlave-sdk[openai]'`. JS/TS: `npm install @execlave/sdk openai` (the openai SDK is already a project dependency in most cases).2. Read `EXECLAVE_API_KEY` from environment variables. Never hardcode. Add it to `.env.example`.3. Create exactly ONE Execlave client per process and reuse it.4. Wrap the openai client IMMEDIATELY AFTER construction, BEFORE any `chat.completions.create` call. Python: `openai = instrument_openai(OpenAI(), exe, agent_id="<stable-kebab-id>")`. JS: `const openai = instrumentOpenAI(new OpenAI(), exe, { agentId: '<stable-kebab-id>' })`. Both return the SAME client for fluent assignment.5. The wrap supports BOTH sync (`OpenAI`) and async (`AsyncOpenAI`) clients in Python, and is idempotent — a `_execlaveInstrumented` / `_execlave_instrumented` marker prevents double-wrapping.6. `agent_id` is a stable kebab-case string identifying THIS LLM-call site in the Execlave dashboard.7. Wrap call sites in try/except for `execlave.errors.PolicyBlockedError` (Python) or `PolicyBlockedError` from `@execlave/sdk` (JS). On block, return a structured 4xx response containing `exc.violations`. Do NOT swallow.8. Do NOT call `exe.enforce_policy` manually — the wrapper enforces using the LAST user message as the policy input. If the prompt is multimodal, only the `text` parts are extracted for the policy check.9. Token usage, model name, and the response `content` are captured automatically into the span. Do NOT add manual `trace.set_*` calls around `create`.10. On process shutdown, call `exe.flush()` (Python) or `await exe.shutdown()` (JS). Deliverables:- Wrap the client at the single bootstrap site that constructs it.- Add env vars to `.env.example`.- One diff per file. Reference: https://www.execlave.com/docs/integrations/openai-chatAPI reference: https://www.execlave.com/docs/sdk-referenceSchnellstart
Umschließen Sie den Client einmal beim Bootstrap. Sowohl synchrone als auch asynchrone OpenAI-Clients werden unterstützt; multimodale Content-Teile werden für die Richtlinienprüfung in Text umgewandelt.
Python
from openai import OpenAIfrom execlave import Execlavefrom execlave.integrations.openai_chat import instrument_openai exe = Execlave(api_key="exe_prod_...")openai = instrument_openai(OpenAI(), exe, agent_id="support-bot") # Enforcement runs on the LAST user message before the request leaves;# the response model + token usage is captured automatically.resp = openai.chat.completions.create( model="gpt-4o-mini", messages=[ {"role": "system", "content": "You are concise."}, {"role": "user", "content": "Summarise our Q3 pipeline"}, ],)JavaScript / TypeScript
import OpenAI from 'openai';import { Execlave } from '@execlave/sdk';import { instrumentOpenAI } from '@execlave/sdk/integrations/openai-chat'; const exe = new Execlave({ apiKey: process.env.EXECLAVE_API_KEY! });const openai = instrumentOpenAI(new OpenAI(), exe, { agentId: 'support-bot' }); const resp = await openai.chat.completions.create({ model: 'gpt-4o-mini', messages: [ { role: 'system', content: 'You are concise.' }, { role: 'user', content: 'Summarise our Q3 pipeline' }, ],});Umgang mit Blockierungen
Ein blockierter Prompt löst PolicyBlockedError aus, BEVOR die Anfrage den Prozess verlässt; OpenAI berechnet dafür keine Tokens.
Python
from execlave.errors import PolicyBlockedError try: resp = openai.chat.completions.create(model="gpt-4o-mini", messages=messages)except PolicyBlockedError as exc: return {"error": "blocked", "violations": exc.violations}JavaScript / TypeScript
import { PolicyBlockedError } from '@execlave/sdk'; try { await openai.chat.completions.create({ model: 'gpt-4o-mini', messages });} catch (err) { if (err instanceof PolicyBlockedError) { return { error: 'blocked', violations: err.violations }; } throw err;}Wie geht es weiter?
Ihre erste Richtlinie schreiben
Block-Modi, Approval-Modus und Monitor-Baselines.
SDK-Referenz
Jede Execlave-Klasse, -Methode und -Fehlerart.
Weitere Integrationen
LangChain, OpenAI Agents SDK, CrewAI, LlamaIndex, AutoGen, MCP.
OpenAI Agents SDK
Verwenden Sie für das Agents SDK den Tracing-Prozessor anstelle dieses Wrappers.