Knowledge Base Sections ▾

Navigation

▸ Start here By roles

Categories

Tools 32
Glossary 12

Tools

PydanticAI + Gonka — typed AI agents for pennies

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

The problem is the same as with all agent frameworks—token costs. An agent with tools loops context iteratively: request → tool call → result → follow-up request. It is easy to burn through several million tokens for a single task. At OpenAI ($2.50–$15 per 1M) and Anthropic ($3–$15 per 1M) rates, even a prototype becomes expensive, and production with thousands of requests per day becomes unaffordable.

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

Quick Start: Connecting in Code

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

Installation:

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

A 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(
    "MiniMaxAI/MiniMax-M2.7",
    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 all — your PydanticAI agent works via the decentralized Gonka network for pennies. The run_sync method is convenient for scripts; for async code there is await agent.run(...).

Model parameters: the network model context window is 200K tokens (200000), and the maximum response length via Gateway is up to 8192 tokens. You can limit the output via model settings (OpenAIChatModelSettings(max_tokens=8192)). moonshotai/Kimi-K2.6 and MiniMaxAI/MiniMax-M2.7 are also available (up to 8192 output tokens each) — 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 the response 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 provides a 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(
    "MiniMaxAI/MiniMax-M2.7",
    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'] — it's already a list[str], not text

This works because both Gonka models (Kimi K2.6 and MiniMax M2.7) support native tool calling — PydanticAI relies on this to return a valid JSON structure. As a result, you get a typed Python object rather than a string that needs to be parsed manually. Ideal for data extraction, classification, form filling, and RAG pipelines, where the result needs to flow further through the code in a strict format.

Cost Comparison

PydanticAI is a framework for agents and pipelines that operate continuously: extracting data, invoking tools, and processing streams of requests. Here, token costs determine whether a project remains a prototype or goes into production. Let's compare typical loads:

ScenarioTokensOpenAI / AnthropicJoinGonka Gonka
Structure extraction from document~3K$0.008 — $0.045~$0.000014
Agent with tool calling (one cycle)~15K$0.04 — $0.22~$0.00007
RAG pipeline (1000 requests/day)~5M/day$12 — $75/day~$0.024/day
Production agent (100K requests/day)~500M/day$1,250 — $7,500/day~$2.40/day

The difference is hundreds and thousands-fold. For a prototype, it means 10M free tokens are enough for hundreds of agent runs. For production handling hundreds of thousands of requests per day, the savings reach tens of thousands of dollars per month—using the same PydanticAI code, just with a different base_url.

A single jg-xxx key and one balance work for both the OpenAI format (/v1) and the 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 using the @agent.tool_plain decorator (without context) or @agent.tool (with access to RunContext and dependency injection). The model decides for itself when to invoke a tool, receives the result, and continues its reasoning:

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

model = OpenAIChatModel(
    "MiniMaxAI/MiniMax-M2.7",
    provider=OpenAIProvider(
        base_url="https://gate.joingonka.ai/v1",
        api_key="jg-your-key",
    ),
)

agent = Agent(
    model,
    instructions="You are a helper. 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:
    """Calculates a mathematical expression."""
    return str(eval(expression))


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

Since tool calling in Gonka is native, tools are invoked reliably—without fragile parsing of text responses. The entire cycle (request → tool call → final response) costs about $0.00007 via Gonka compared to $0.04–0.22 with OpenAI/Anthropic.

Which model to choose: moonshotai/Kimi-K2.6 — strong in coding and complex reasoning, output up to 8192. MiniMaxAI/MiniMax-M2.7 — for long dialogues and balanced tasks, output up to 8192. Both are available right now using a single key—only the model string changes. Tools similar in spirit: LangChain for chains and RAG, LlamaIndex for data indexing.

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

Want to learn more?

Explore other sections or start earning GNK right now.

Get 10M free tokens →