> ## Documentation Index
> Fetch the complete documentation index at: https://docs.promptguard.co/llms.txt
> Use this file to discover all available pages before exploring further.

# LlamaIndex

> Scan LlamaIndex queries and LLM calls with a callback handler, a query guard, or auto-instrumentation

```bash theme={"system"}
pip install "promptguard-sdk[llamaindex]"
```

## Callback handler

`PromptGuardCallbackHandler` from `promptguard.integrations.llamaindex` joins LlamaIndex's callback manager. It scans the prompt at the start of every `llm` event and the query string at the start of every `query` event, and, unless you pass `scan_responses=False`, the response at the end of each.

```python theme={"system"}
from llama_index.core import Settings
from llama_index.core.callbacks import CallbackManager

from promptguard.integrations.llamaindex import PromptGuardCallbackHandler

Settings.callback_manager = CallbackManager([PromptGuardCallbackHandler()])  # reads PROMPTGUARD_API_KEY
```

A callback watches events but cannot change them, so in enforce mode a **redact** decision blocks, raising `PromptGuardBlockedError`, rather than let content through that should have been redacted. To redact a query, use the query guard below. To skip events, pass their types in `event_starts_to_ignore` or `event_ends_to_ignore`.

If you also use the LangChain handler, `LlamaIndexCallbackHandler` is the same class under a name that does not clash.

## Query guard

`PromptGuardQueryGuard.guard_query()` scans a query string before you hand it to an index, and returns it redacted when the decision is redact, in enforce mode. On a block it raises `PromptGuardBlockedError`.

```python theme={"system"}
from promptguard.integrations.llamaindex import PromptGuardQueryGuard

guard = PromptGuardQueryGuard()  # reads PROMPTGUARD_API_KEY

response = query_engine.query(guard.guard_query(user_question))
```

## Auto-instrumentation

LlamaIndex's LLM classes call the provider SDKs underneath, so [auto-instrumentation](/integrations/auto-instrumentation) scans calls made through the patched SDKs, such as `llama-index-llms-openai`, which uses the `openai` package. It sees each model call, not LlamaIndex's queries; use the handler for those.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.