> ## 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

> Sử dụng TokenLab với Pydantic AI thông qua các mô hình tương thích với OpenAI

## Tổng quan

Pydantic AI có thể sử dụng TokenLab với `OpenAIChatModel` và `OpenAIProvider(base_url=...)`.

<Note>
  **Loại**: Framework tác nhân (Agent Framework)

  **Đường dẫn chính**: Chat completions tương thích với OpenAI

  **Độ tin cậy hỗ trợ**: Hỗ trợ đường dẫn nhà cung cấp tương thích với OpenAI
</Note>

## Môi trường

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

## Tác nhân cơ bản

```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)
```

## Công cụ (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)
```

## Ghi chú về Endpoint

Đường dẫn này nhắm mục tiêu đến lớp mô hình OpenAI Chat Completions của Pydantic AI. Đối với hành vi giao thức gốc của Responses hoặc nhà cung cấp, hãy chọn đường dẫn client hiển thị trực tiếp giao thức đó.
