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

# Pydantic AI

> Use TokenLab with Pydantic AI through OpenAI-compatible models

## Overview

Pydantic AI can use TokenLab with `OpenAIChatModel` and `OpenAIProvider(base_url=...)`.

<Note>
  **Type**: Agent Framework

  **Primary Path**: OpenAI-compatible chat completions

  **Support Confidence**: Supported OpenAI-compatible provider path
</Note>

## Environment

```bash theme={null}
export TOKENLAB_API_KEY="sk-your-tokenlab-key"
```

## Basic Agent

```python theme={null}
import os

from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIChatModel
from pydantic_ai.providers.openai import OpenAIProvider

model = OpenAIChatModel(
    "claude-sonnet-5",
    provider=OpenAIProvider(
        base_url="https://api.tokenlab.sh/v1",
        api_key=os.environ["TOKENLAB_API_KEY"],
    ),
)

agent = Agent(model)
result = agent.run_sync("Explain one benefit of OpenAI-compatible gateways.")
print(result.output)
```

## Tools

```python theme={null}
from pydantic_ai import Agent, RunContext

agent = Agent(model)

@agent.tool
def lookup_status(ctx: RunContext[None], service: str) -> str:
    return f"{service} is online"

result = agent.run_sync("Check the TokenLab status.")
print(result.output)
```

## Endpoint Notes

This path targets Pydantic AI's OpenAI Chat Completions model class. For Responses-native or provider-native protocol behavior, choose a client path that exposes that protocol directly.
