# How to use Pydantic AI with Subconscious

> To use Pydantic AI with Subconscious, create an `OpenAIChatModel("subconscious/glm-5.3-marathon")` with `provider=OpenAIProvider(base_url="https://api.subconscious.dev/v1", api_key=...)` and pass it to `Agent`. Your typed agents then run on Subconscious, billed per token.

Canonical page: https://www.subconscious.dev/agents/frameworks/pydantic-ai
Category: [Frameworks](https://www.subconscious.dev/agents/frameworks)
Last verified: 2026-09-30
Pydantic AI: https://pydantic.dev/docs/ai/
Source: https://github.com/pydantic/pydantic-ai

## Before you start

- Python 3.10+
- A Subconscious account and API key

## Set up Pydantic AI

### 1. Install Pydantic AI

Install the framework and its OpenAI-compatible client.

```bash
pip install "pydantic-ai-slim[openai]"
```

### 2. Create a Subconscious API key

Sign in at https://platform.subconscious.dev, create an API key, and export it so the tool can read it.

```bash
export SUBCONSCIOUS_API_KEY="your_key"
```

### 3. Point it at Subconscious

OpenAIChatModel uses Chat Completions. It replaces the older OpenAIModel class name.

```python
import os
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIChatModel
from pydantic_ai.providers.openai import OpenAIProvider

model = OpenAIChatModel(
    "subconscious/glm-5.3-marathon",
    provider=OpenAIProvider(
        base_url="https://api.subconscious.dev/v1",
        api_key=os.environ["SUBCONSCIOUS_API_KEY"],
    ),
)
agent = Agent(model, instructions="Be concise.")

result = agent.run_sync("Where does 'hello world' come from?")
print(result.output)
```

### 4. Run it

Run the script.

```bash
python agent.py
```

## Connection details

- OpenAI-compatible base URL: https://api.subconscious.dev/v1
- Anthropic-compatible base URL: https://api.subconscious.dev
- API key: From subc login or the Subconscious dashboard
- Model: subconscious/glm-5.3-marathon
- Multimodal, high-throughput model: subconscious/deepseek-v4.1-flash-marathon

Full API reference: https://docs.subconscious.dev

## Frequently asked questions

### Tool calls or structured output misbehave in Pydantic AI. What should I check?

OpenAIProvider picks a model profile as if the model were an OpenAI model. Pass a profile, for example OpenAIModelProfile(openai_supports_strict_tool_definition=False), to OpenAIChatModel to turn off OpenAI-specific behavior.

### Which Subconscious model should I use with Pydantic AI?

Start with subconscious/glm-5.3-marathon, an open frontier coding model built for long agent runs. subconscious/deepseek-v4.1-flash-marathon is a cheaper, high-throughput alternative that also takes image input. Use the full model ID, including the subconscious/ prefix.

### Is Pydantic AI on Subconscious OpenAI- or Anthropic-compatible?

This guide connects Pydantic AI through the OpenAI-compatible Subconscious API. Subconscious serves both formats: OpenAI Chat Completions at https://api.subconscious.dev/v1 and Anthropic Messages at https://api.subconscious.dev.

### How is Pydantic AI usage on Subconscious billed?

Per token by default, with no commitment. Heavy users can switch to a monthly token plan: a fixed price for a large daily token allowance shared across your team.
