How to use Pydantic AI with Subconscious

Let your AI do the setup. Paste this into Claude Code, Cursor, ChatGPT or any assistant.

Make Subconscious the model provider in my Pydantic AI project.

Subconscious is an inference API for agents. It serves open models through OpenAI- and Anthropic-compatible endpoints, billed per token.
- OpenAI-compatible base URL: https://api.subconscious.dev/v1
- Anthropic-compatible base URL: https://api.subconscious.dev
- Model: subconscious/glm-5.3-marathon
- API key: read it from the SUBCONSCIOUS_API_KEY environment variable. Never commit the key or paste it into project files. If it is not set, ask me to create one at https://platform.subconscious.dev.

Follow these steps. Run commands and edit files yourself. If I already have agent code, change its model setup to match the example instead of adding a new file.

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

Full guide: https://www.subconscious.dev/agents/frameworks/pydantic-ai.md

When you are done, confirm the tool is using subconscious/glm-5.3-marathon, then summarize what you changed.

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.

Last verified against the Pydantic AI docs on

Before you start

  • Python 3.10+
  • A Subconscious account and API key

Pydantic AI docs · Source on GitHub

Set up Pydantic AI

  1. Step 1: Install Pydantic AI

    Install the framework and its OpenAI-compatible client.

    Terminal
    pip install "pydantic-ai-slim[openai]"
  2. Step 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.

    Terminal
    export SUBCONSCIOUS_API_KEY="your_key"
  3. Step 3: Point it at Subconscious

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

    agent.py
    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. Step 4: Run it

    Run the script.

    Terminal
    python agent.py

Why run Pydantic AI on Subconscious

No new SDK
Subconscious speaks OpenAI Chat Completions and Anthropic Messages, so the client you already use works with a base URL change.
Pay per token
No plan and no commitment: sign up, get an API key and pay for the tokens your agents use.
Long sessions stay fast
2x faster task completion on long tasks than standard open-model inference, especially past 200k tokens of context.
Context that keeps going
The OrangeLine runtime compresses the parts of context that stopped mattering, on the GPU, while the run continues. That gives a 5M+ token context window with no stop-and-compact pause.

Connection details

For any client that takes a custom OpenAI- or Anthropic-compatible endpoint.

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 reference in the Subconscious API docs.

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.

More frameworks

See all