§ INTEGRATION · LLAMAINDEX
LlamaIndex-Event-Handler
Registrieren Sie einen Dispatcher-Event-Handler beim App-Start. Jede Query, jeder Retrieval-, LLM- und Agent-Tool-Aufruf wird gesteuert und nachverfolgt.
Voraussetzungen
- Execlave-Konto mit aktivem API-Schlüssel — Einstellungen → API-Schlüssel.
- Ein registrierter Agent in Execlave. Verwenden Sie die lowercase-kebab
agent_iddes Agents im Handler. - LlamaIndex
llama-index-core >=0.11(Python).
Installation
Der Handler ist über ein optionales Extra verfügbar, sodass die Standardinstallation des SDK LlamaIndex nicht als transitive Abhängigkeit mitzieht.
Python
pip install 'execlave-sdk[llamaindex]'Sofort-Einrichtung mit einem Copilot
Fügen Sie diesen Prompt in Ihren KI-Coding-Assistenten ein. Er verbindet Execlave mit Ihrem bestehenden LlamaIndex-Bootstrap.
You are adding Execlave (an AI agent governance platform) to an existing Python project that uses LlamaIndex. Do not rewrite existing indexes, retrievers, or query engines — only attach instrumentation. Rules you MUST follow:1. Install the correct extra: `pip install 'execlave-sdk[llamaindex]'`. Add it to requirements.txt / pyproject.toml.2. Read `EXECLAVE_API_KEY` from environment variables. Never hardcode keys. Add it to `.env.example`.3. Create exactly ONE Execlave client per process and reuse it: `exe = Execlave(api_key=os.environ["EXECLAVE_API_KEY"])`.4. Register the handler ONCE at app start, not per request: ``` from llama_index.core.instrumentation import get_dispatcher from execlave.integrations.llamaindex import ExeclaveLlamaIndexHandler handler = ExeclaveLlamaIndexHandler(exe, agent_id="<stable-kebab-id>") get_dispatcher().add_event_handler(handler) ```5. `agent_id` is a stable kebab-case string identifying this app in the Execlave dashboard.6. Wrap `query_engine.query()` / `agent.chat()` in try/except for `execlave.errors.PolicyBlockedError`. On block, return a structured 4xx response containing `exc.violations`. Do NOT swallow the error.7. Do NOT call `exe.enforce_policy` manually — the handler runs enforcement on `QueryStartEvent` and `AgentToolCallEvent`.8. On process shutdown (ASGI lifespan / SIGTERM handler), call `exe.flush()`. Deliverables:- One diff per file. Touch only app bootstrap and the entry-point that runs queries.- Add `EXECLAVE_API_KEY` and optional `EXECLAVE_BASE_URL` to `.env.example`. Reference: https://www.execlave.com/docs/integrations/llamaindexAPI reference: https://www.execlave.com/docs/sdk-referenceSchnellstart
Ein Handler abonniert jedes Event, das LlamaIndex auslöst.
Python
from llama_index.core.instrumentation import get_dispatcherfrom execlave import Execlavefrom execlave.integrations.llamaindex import ExeclaveLlamaIndexHandler exe = Execlave(api_key="exe_prod_...")handler = ExeclaveLlamaIndexHandler(exe, agent_id="rag-bot") dispatcher = get_dispatcher()dispatcher.add_event_handler(handler)dispatcher.add_span_handler(handler.span_handler()) # Enforcement fires on QueryStartEvent (user query) and on every# AgentToolCallEvent (tool name + arguments). A PolicyBlockedError# halts the LlamaIndex pipeline before the tool or LLM is invoked.response = index.as_query_engine().query("Summarise our Q3 pipeline")Was instrumentiert wird
Jedes LlamaIndex-Dispatcher-Event wird einem Span unter derselben Trace-ID zugeordnet.
| Event | Span-Art | Hinweise |
|---|---|---|
QueryStartEvent | chain | Die übergeordnete Query wird durch die Pre-Execution-Durchsetzung geleitet. |
LLMChatStartEvent / LLMCompletionStartEvent | llm | Erfasst Modell und Nachrichten. |
RetrievalStartEvent / EmbeddingStartEvent | retriever | Erfasst einen RAG-Lookup oder Embedding-Aufruf. |
AgentToolCallEvent | tool | Jeder Tool-Aufruf wird durchgesetzt, wobei der Tool-Name in die Allowlist-Prüfung einfließt. |
AgentRunStepStartEvent / AgentChatWithStepStartEvent | agent | Erfasst einen Planungsschritt eines LlamaIndex-Agent-Runners. |
*EndEvent | — | Schließt den zugehörigen Span mit Output und Token-Verbrauch ab, sofern vorhanden. |
Umgang mit Blockierungen
Die Durchsetzung läuft bei der übergeordneten Query und bei jedem Tool-Aufruf. Ein blockierter Aufruf löst PolicyBlockedError mit der genauen Liste der Verstöße aus.
Python
from execlave.errors import PolicyBlockedError try: response = query_engine.query(user_question)except PolicyBlockedError as exc: # Policy blocked the query before the LLM was called. return {"error": "blocked", "violations": exc.violations}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, AutoGen, MCP und mehr.
LangChain-Integration
Verwenden Sie den Callback-Handler, wenn Ihr Stack auch LangChain-Chains ausführt.