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

# Framework recipes

> Attach the user per call in LangChain, LlamaIndex, the OpenAI Agents SDK, LiteLLM and Pydantic AI.

Every recipe follows one rule: build the shared object once, without a user, and
attach the user on each call or each run, after your own sign-in check. Each
section also names the dangerous pattern: a user set on something shared. That
sends every request, for every user, as one user, so one user's facts reach
everyone.

The recipes use the [Atlaso SDK](https://docs.atlaso.ai/gateway/sdk.md) for the key, the address and
the user's headers (`for_user(id).headers()`). For the AI SDK, see
[AI SDK](https://docs.atlaso.ai/gateway/ai-sdk.md).

## LangChain (Python)

```bash theme={null}
pip install atlaso langchain-openai
```

```python theme={null}
from langchain_openai import ChatOpenAI
from atlaso import Atlaso

atlaso = Atlaso()
options = atlaso.client_options()
llm = ChatOpenAI(                     # shared, no user on it
    model="openai/gpt-6.1-terra",
    api_key=options["api_key"],
    base_url=options["base_url"],
    use_responses_api=False,
    include_response_headers=True,
)

def answer(user_id: str, question: str) -> str:
    user = atlaso.for_user(user_id)
    message = llm.invoke(question, extra_headers=dict(user.headers()))   # this call only
    receipt = atlaso.receipt_from_headers(message.response_metadata["headers"])
    print(receipt.memory.memory_ids if receipt.memory else [])
    return message.content
```

**Dangerous:** `ChatOpenAI(..., default_headers={"x-atlaso-user": user_id})` on
an instance you share across requests.

## LangChain (JavaScript)

Build the chat model inside the request, with the user's headers. A model built
this way serves only that request.

```bash theme={null}
npm install atlaso @langchain/openai
```

```typescript theme={null}
import { ChatOpenAI } from "@langchain/openai";
import { Atlaso } from "atlaso";

const atlaso = new Atlaso();   // shared, no user

export async function answer(userId: string, question: string) {
  const { apiKey, baseURL } = atlaso.clientOptions();
  const llm = new ChatOpenAI({   // per request
    model: "openai/gpt-6.1-terra",
    apiKey,
    configuration: { baseURL, defaultHeaders: atlaso.forUser(userId).headers() },
  });
  const message = await llm.invoke(question);
  return message.content;
}
```

**Dangerous:** the same `ChatOpenAI` with `defaultHeaders` holding a user, built
at module scope. There is no receipt in this recipe.

## LlamaIndex

LlamaIndex needs Python 3.10 or newer. `llama-index-llms-openai-like` 0.7 and
later declare Python 3.10 or newer (0.9 declares 3.11); on Python 3.9, pip picks
an older release whose dependencies fail to import. Tested together:
`llama-index-llms-openai-like` 0.9.0, `llama-index-llms-openai` 0.9.0 and
`openai` 3.57.0 on Python 3.15.

```bash theme={null}
pip install atlaso "llama-index-llms-openai-like>=0.7"
```

```python theme={null}
from llama_index.core.llms import ChatMessage
from llama_index.llms.openai_like import OpenAILike
from atlaso import Atlaso

atlaso = Atlaso()
options = atlaso.client_options()

def answer(user_id: str, question: str) -> str:
    llm = OpenAILike(                 # per request
        model="openai/gpt-6.1-terra",
        api_base=options["base_url"],
        api_key=options["api_key"],
        is_chat_model=True,
        temperature=1,                # OpenAILike sends 0.1 by default; this model accepts only 1
        default_headers=dict(atlaso.for_user(user_id).headers()),
    )
    return llm.chat([ChatMessage(role="user", content=question)]).message.content
```

**Dangerous:** an `OpenAILike(default_headers={"x-atlaso-user": ...})` at module
level, shared by every request. There is no receipt in this recipe.

## OpenAI Agents SDK

The OpenAI Agents SDK needs Python 3.10 or newer. Use `openai-agents` 0.21 or
later, which declares `openai>=3,<4`. Older releases pair with `openai` 2.x, and
some of those pairs fail before any call (for example `openai-agents` 0.8.4 with
`openai` 2.48.0). Tested together: `openai-agents` 0.25.0 and `openai` 3.57.0 on
Python 3.15.

```bash theme={null}
pip install atlaso "openai>=3,<4" "openai-agents>=0.21"
```

Give each run its own scoped client through `RunConfig(model_provider=...)`, as
in [Example 6](https://docs.atlaso.ai/gateway/sdk.md#example-6-the-openai-agents-sdk).

**Dangerous:** `set_default_openai_client()` with a user's client. It is global,
so every concurrent run uses that user's memory.

## LiteLLM

Use the LiteLLM Python library and pass the user's headers on each call.

```bash theme={null}
pip install atlaso litellm
```

```python theme={null}
import litellm
from atlaso import Atlaso

atlaso = Atlaso()
options = atlaso.client_options()
MODEL = "openai/gpt-6.1-terra"

def answer(user_id: str, question: str) -> str:
    user = atlaso.for_user(user_id)
    response = litellm.completion(
        model="openai/" + MODEL,   # LiteLLM's OpenAI-compatible route, then the Atlaso model ID
        api_base=options["base_url"],
        api_key=options["api_key"],
        messages=[{"role": "user", "content": question}],
        extra_headers=dict(user.headers()),   # this call only
    )
    headers = response._hidden_params.get("additional_headers", {})
    receipt = atlaso.receipt_from_headers(
        {name.removeprefix("llm_provider-"): value for name, value in headers.items()})
    print(receipt.memory.memory_ids if receipt.memory else [])
    return response.choices[0].message.content
```

LiteLLM returns the reply headers with an `llm_provider-` prefix in
`_hidden_params`, which LiteLLM may change between versions.

**Dangerous:** setting `litellm.headers` to a user's headers. It is a module
global, so every call from every request uses that user.

```python theme={null}
# Don't: every call below is sent as demo-user-42, whoever asked.
litellm.headers = {"x-atlaso-user": "demo-user-42"}
litellm.completion(model="openai/" + MODEL, api_base=options["base_url"],
                   api_key=options["api_key"],
                   messages=[{"role": "user", "content": "What is my project called?"}])
```

## Pydantic AI

```bash theme={null}
pip install atlaso "pydantic-ai-slim[openai]"
```

```python theme={null}
from openai import AsyncOpenAI
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIChatModel
from pydantic_ai.providers.openai import OpenAIProvider
from atlaso import AsyncAtlaso

atlaso = AsyncAtlaso()
client = AsyncOpenAI(**atlaso.client_options())   # shared, no user on it
model = OpenAIChatModel("openai/gpt-6.1-terra", provider=OpenAIProvider(openai_client=client))
agent = Agent(model, instructions="Be brief.")

async def answer(user_id: str, question: str) -> str:
    user = atlaso.for_user(user_id)
    result = await agent.run(question, model_settings={"extra_headers": dict(user.headers())})
    return result.output
```

**Dangerous:** an `AsyncOpenAI(default_headers={"x-atlaso-user": ...})` passed to
a provider at module level. There is no receipt in this recipe.


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