Skip to content
Zurück zur Startseite

§ 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.

§ 01

Voraussetzungen

  • Execlave-Konto mit aktivem API-Schlüssel — Einstellungen → API-Schlüssel.
  • Ein registrierter Agent in Execlave. Verwenden Sie die lowercase-kebab agent_id des Agents im Handler.
  • LlamaIndex llama-index-core >=0.11 (Python).
§ 02

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]'
§ 03

Sofort-Einrichtung mit einem Copilot

Fügen Sie diesen Prompt in Ihren KI-Coding-Assistenten ein. Er verbindet Execlave mit Ihrem bestehenden LlamaIndex-Bootstrap.

§ Copy prompt · paste into your AI coding assistant
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-reference
§ 04

Schnellstart

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")
§ 05

Was instrumentiert wird

Jedes LlamaIndex-Dispatcher-Event wird einem Span unter derselben Trace-ID zugeordnet.

EventSpan-ArtHinweise
QueryStartEventchainDie übergeordnete Query wird durch die Pre-Execution-Durchsetzung geleitet.
LLMChatStartEvent / LLMCompletionStartEventllmErfasst Modell und Nachrichten.
RetrievalStartEvent / EmbeddingStartEventretrieverErfasst einen RAG-Lookup oder Embedding-Aufruf.
AgentToolCallEventtoolJeder Tool-Aufruf wird durchgesetzt, wobei der Tool-Name in die Allowlist-Prüfung einfließt.
AgentRunStepStartEvent / AgentChatWithStepStartEventagentErfasst einen Planungsschritt eines LlamaIndex-Agent-Runners.
*EndEventSchließt den zugehörigen Span mit Output und Token-Verbrauch ab, sofern vorhanden.
§ 06

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}
§ 07

Wie geht es weiter?

LlamaIndex-Integration — Execlave Docs