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

> 通过 OpenAI 兼容模型在 Pydantic AI 中使用 TokenLab

## 概述

Pydantic AI 可以通过 `OpenAIChatModel` 和 `OpenAIProvider(base_url=...)` 使用 TokenLab。

<Note>
  **类型**: Agent 框架

  **主要路径**: OpenAI 兼容的聊天补全 (Chat Completions)

  **支持置信度**: 支持的 OpenAI 兼容提供商路径
</Note>

## 环境配置

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

## 基础 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)
```

## 端点说明

此路径针对 Pydantic AI 的 OpenAI Chat Completions 模型类。如需使用 Responses-native 或提供商原生协议行为，请选择直接公开该协议的客户端路径。
