# LlamaIndex

[LlamaIndex](https://docs.llamaindex.ai/) is a framework for building applications that combine LLM calls with data connectors, indexes, retrieval, and query workflows. AISIX fits at the model request boundary, while LlamaIndex continues to own document loading, indexing, retrieval, and response assembly.

The LlamaIndex OpenAI integration provides `OpenAIResponses`, an LLM adapter for the OpenAI Responses API with a custom API base. Use that adapter when a LlamaIndex application should send Responses API requests through AISIX.

This guide uses LlamaIndex Python with `llama-index-llms-openai`. You will configure `OpenAIResponses` with an AISIX proxy URL, caller API key, and model alias.

## Prerequisites[​](#prerequisites "Direct link to Prerequisites")

Before starting, prepare the following:

* A Python environment supported by [LlamaIndex](https://docs.llamaindex.ai/en/stable/getting_started/installation/).
* A running AISIX gateway that the application can reach.
* An AISIX caller API key.
* A model alias the caller API key can access through the [Responses API](https://docs.api7.ai/ai-gateway/endpoints/responses-api.md).

If AISIX is already deployed for your organization, obtain the gateway URL, model alias, and caller API key from the team that manages it. Otherwise, follow the [AISIX OSS getting-started guide](https://docs.api7.ai/ai-gateway/getting-started/gateway-quickstart.md) or [AISIX Enterprise getting-started guide](https://docs.api7.ai/ai-gateway/getting-started/aisix-cloud-quickstart.md), or [contact API7](https://api7.ai/contact) for AISIX Cloud access.

## Configure LlamaIndex[​](#configure-llamaindex "Direct link to Configure LlamaIndex")

Install the OpenAI LLM integration if the application does not already include it:

```
pip install llama-index-llms-openai
```

Set the values that the LlamaIndex application will use:

```
# AISIX_BASE_URL ends in /v1 and has no trailing slash

# The local quickstarts use http://127.0.0.1:3000/v1

export AISIX_BASE_URL="YOUR_AISIX_GATEWAY_URL/v1"

export AISIX_API_KEY="YOUR_CALLER_API_KEY"

export AISIX_MODEL="gpt-4o-prod"

export AISIX_CONTEXT_WINDOW="128000"
```

Create an `OpenAIResponses` client with the AISIX values:

```
import os



from llama_index.core.llms import ChatMessage

from llama_index.llms.openai import OpenAIResponses



llm = OpenAIResponses(

  model=os.environ["AISIX_MODEL"],

  api_base=os.environ["AISIX_BASE_URL"],

  api_key=os.environ["AISIX_API_KEY"],

  context_window=int(os.environ["AISIX_CONTEXT_WINDOW"]),

)



response = llm.chat([

  ChatMessage(role="user", content="Write one sentence about retrieval systems."),

])

print(response.message.content)
```

The model value is the AISIX model alias, not the upstream provider model ID. Set `AISIX_CONTEXT_WINDOW` to the context size of the model behind the alias so LlamaIndex does not need to infer it from the alias name. AISIX governs the model request path, while the LlamaIndex application continues to own data loading, indexing, retrieval, and response assembly.

## Verify the Integration[​](#verify-the-integration "Direct link to Verify the Integration")

Run the script from the shell where the AISIX environment variables are set.

When the request succeeds, verify the following results:

* The script prints a chat response.
* AISIX records a successful `POST /v1/responses` request for the selected model alias.

Verify the request in the AISIX gateway logs. You can also use configured metrics or upstream provider logs.

If the request fails, first confirm that the caller API key can access the selected model alias and that `api_base` points to the AISIX proxy API root with `/v1`.

LlamaIndex also provides OpenAI-compatible chat adapters, including `OpenAILike`, for applications that still require Chat Completions compatibility. Use those adapters only when an existing workflow or integration depends on the Chat Completions route; for new OpenAI-family LlamaIndex integrations, prefer `OpenAIResponses`.

LlamaIndex applications often combine retrieval, chat, tool calling, and structured response parsing. Validate the exact workflow before relying on gateway policy or telemetry for that path.

## Next Steps[​](#next-steps "Direct link to Next Steps")

* [Responses API](https://docs.api7.ai/ai-gateway/endpoints/responses-api.md): review gateway-facing request behavior.
* [OpenAI-Compatible API](https://docs.api7.ai/ai-gateway/endpoints/openai-compatible-chat.md): use the Chat Completions route when an existing LlamaIndex workflow requires it.
* [Embeddings](https://docs.api7.ai/ai-gateway/endpoints/embeddings.md): configure embedding traffic if your LlamaIndex workflow uses AISIX for embeddings.
* [Response Caching](https://docs.api7.ai/ai-gateway/traffic-controls/caching.md): cache eligible non-streaming chat responses.
