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

# PydanticAI

> Build a typed PydanticAI agent with RedPill

PydanticAI exposes an OpenAI-compatible provider directly. Use `OpenAIChatModel` for RedPill's Chat
Completions endpoint.

## Install

```bash theme={null}
pip install pydantic-ai
export REDPILL_AI_API_KEY="YOUR_API_KEY"
```

## Build an 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
from pydantic_ai.usage import UsageLimits


model = OpenAIChatModel(
    "z-ai/glm-5.2",
    provider=OpenAIProvider(
        base_url="https://api.redpill.ai/v1",
        api_key=os.environ["REDPILL_AI_API_KEY"],
    ),
)

agent = Agent(model, system_prompt="Be concise and state uncertainty.")


@agent.tool_plain
def deployment_status(service: str) -> str:
    """Return a sample deployment status."""
    return f"{service}: healthy"


result = agent.run_sync(
    "Check the gateway deployment status.",
    usage_limits=UsageLimits(request_limit=3, tool_calls_limit=1),
)
print(result.output)
```

## Production checks

Keep explicit `UsageLimits`, add a network timeout, and validate tool arguments at the application
boundary. For confidential-only calls, use `https://tee.redpill.ai/v1` with an `is_tee: true`
model.

## Troubleshooting

| Symptom                        | Check                                                     |
| ------------------------------ | --------------------------------------------------------- |
| Provider initialization fails  | Use `OpenAIProvider` with the `/v1` base URL.             |
| Tool calls exceed expectations | Lower `request_limit` and `tool_calls_limit`.             |
| Model cannot call tools        | Select a model that declares `tools` in the live catalog. |

## References

* [PydanticAI OpenAI models](https://ai.pydantic.dev/models/openai/)
* [RedPill function calling](/guides/function-calling)
