Knowledge Base Sections ▾

Tools

Tools

PydanticAI + Gonka — typed AI agents for pennies

PydanticAI is a Python framework for creating AI agents from the Pydantic team (the same validation library that powers half of the Python ecosystem). The main feature of PydanticAI is typed output: you describe the result as a regular Pydantic model, and the framework ensures that the model returns exactly that structure, validated and ready for use. Plus, clear @agent.tool tool calling, dependency injection, and support for any provider.

The problem is the same as for all agent frameworks—the price of tokens. An agent with tools cycles context: query → tool call → result → re-query. A single task can easily consume several million tokens. At OpenAI's rates ($2.50–15 per 1M) and Anthropic's ($3–15 per 1M), even a prototype becomes expensive, and production with thousands of requests per day becomes unaffordable.

PydanticAI natively works with any OpenAI-compatible endpoint via the OpenAIChatModel and OpenAIProvider classes. This means that JoinGonka Gateway connects with a few lines of code—without separate packages or adapters. The result: typed agents working for $0.0005 per 1M input tokens instead of $2.50–15 for OpenAI/Anthropic—hundreds to thousands of times cheaper.

Quick Start: Connecting in Code

First, get your key: register at gate.joingonka.ai/register—we give 10M free tokens upon registration—and create a jg-xxx key in Dashboard → API Keys.

Installation:

pip install pydantic-ai
# or a lightweight option with only OpenAI dependencies:
# pip install "pydantic-ai-slim[openai]"

Minimal example—an agent via Gonka. PydanticAI sets a custom endpoint via OpenAIProvider(base_url=..., api_key=...), which is passed to OpenAIChatModel:

from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIChatModel
from pydantic_ai.providers.openai import OpenAIProvider

model = OpenAIChatModel(
    "Qwen/Qwen3-235B-A22B-Instruct-2507-FP8",
    provider=OpenAIProvider(
        base_url="https://gate.joingonka.ai/v1",
        api_key="jg-your-key",
    ),
)

agent = Agent(model)

result = agent.run_sync("Explain in two sentences what PoUW is")
print(result.output)

That's it—your PydanticAI agent works through the decentralized Gonka network for pennies. The run_sync method is convenient for scripts; for async code, there's await agent.run(...).

Model parameters: Qwen3-235B context window is 128K tokens (131072), maximum response length via Gateway is up to 8192 tokens. You can limit output via model settings (OpenAIChatModelSettings(max_tokens=8192)). Also available are moonshotai/Kimi-K2.6 (up to 3072 output tokens) and MiniMaxAI/MiniMax-M2.7 (up to 4096)—just change the model name in the first argument of OpenAIChatModel.

PydanticAI Feature: Typed Output

The main reason to choose PydanticAI is structured output. Instead of parsing text with regex, you describe the result as a Pydantic model and pass it to the output_type parameter. The framework uses the model's tool calling to force it to return data strictly according to the schema, validates it, and returns the ready-made object via result.output.

from pydantic import BaseModel
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIChatModel
from pydantic_ai.providers.openai import OpenAIProvider

model = OpenAIChatModel(
    "Qwen/Qwen3-235B-A22B-Instruct-2507-FP8",
    provider=OpenAIProvider(
        base_url="https://gate.joingonka.ai/v1",
        api_key="jg-your-key",
    ),
)


class Profile(BaseModel):
    name: str
    role: str
    skills: list[str]


agent = Agent(model, output_type=Profile)

result = agent.run_sync(
    "Extract data: Anna is a backend developer, knows Python, Go, and Postgres"
)
print(result.output)
# name='Anna' role='backend-developer' skills=['Python', 'Go', 'Postgres']
print(result.output.skills)  # ['Python', 'Go', 'Postgres'] — already list[str], not text

This works because Qwen3-235B (and both other Gonka models) support native tool calling—PydanticAI relies on it to return a valid JSON structure. The output is a typed Python object, not a string that needs to be parsed manually. Ideal for data extraction, classification, form filling, and RAG pipelines where the result must flow further in the code in a strict format.

Cost Comparison

PydanticAI is a framework for agents and pipelines that work continuously: extracting data, calling tools, processing request streams. Here, token cost determines whether a project remains a prototype or goes into production. Let's compare typical workloads:

ScenarioTokensOpenAI / AnthropicJoinGonka Gonka
Extracting structure from a document~3K$0.008 — $0.045~$0.000002
Agent with tool calling (one cycle)~15K$0.04 — $0.22~$0.00001
RAG pipeline (1000 requests/day)~5M/day$12 — $75/day~$0.003/day
Production agent (100K requests/day)~500M/day$1,250 — $7,500/day~$0.30/day

The difference is hundreds to thousands of times. For a prototype, this means 10M free tokens are enough for hundreds of agent runs. For production, processing hundreds of thousands of requests per day, the savings amount to tens of thousands of dollars per month—with the same PydanticAI code, just with a different base_url.

One jg-xxx key and one balance work for both OpenAI format (/v1) and Anthropic format (/v1/messages)—but for PydanticAI, the OpenAI-compatible endpoint shown above is sufficient.

Tool calling and model selection

The second key capability of PydanticAI is tools. A function can be registered with the @agent.tool_plain decorator (without context) or @agent.tool (with access to RunContext and dependency injection). The model decides when to call the tool, gets the result, and continues reasoning:

import random
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIChatModel
from pydantic_ai.providers.openai import OpenAIProvider

model = OpenAIChatModel(
    "Qwen/Qwen3-235B-A22B-Instruct-2507-FP8",
    provider=OpenAIProvider(
        base_url="https://gate.joingonka.ai/v1",
        api_key="jg-your-key",
    ),
)

agent = Agent(
    model,
    instructions="You are an assistant. Use tools when necessary.",
)


@agent.tool_plain
def roll_dice() -> str:
    """Rolls a six-sided die and returns the result."""
    return str(random.randint(1, 6))


@agent.tool_plain
def calculator(expression: str) -> str:
    """Evaluates a mathematical expression."""
    return str(eval(expression))


result = agent.run_sync("Roll a die and multiply the result by 7")
print(result.output)

Since Gonka's tool calling is native, tools are called reliably—without fragile parsing of text responses. The entire cycle (query → tool call → final response) costs about $0.00001 via Gonka versus $0.04–0.22 via OpenAI/Anthropic.

Which model to choose: Qwen/Qwen3-235B-A22B-Instruct-2507-FP8 is the default, offering the best balance of quality and the largest output limit (8192). moonshotai/Kimi-K2.6 is strong in coding and complex reasoning (output up to 3072). MiniMaxAI/MiniMax-M2.7 is for long dialogues (output up to 4096). All three are available right now with a single key—only the model string changes. Similar tools: LangChain for chains and RAG, LlamaIndex for data indexing.

PydanticAI + Gonka = typed AI agents in Python for pennies. Structured output with Pydantic models, native tool calling, dependency injection—all via OpenAIChatModel + OpenAIProvider with a single base_url. Cost: from $0.0005 per 1M tokens instead of $2.50–15 for OpenAI and Anthropic.

Want to learn more?

Explore other sections or start earning GNK right now.

Get 10M free tokens →