Agents

AI agent API: architecture, costs and providers

An agent API is not a model with a clever system prompt. It is an API that lets a model call tools, read the results and continue until a stop condition. The design question is who runs that loop, where state lives and how you cap the spend.

Short answer. Four architectures dominate in 2026. Chat Completions with your own loop gives maximum control. OpenAI's Responses API runs built-in tools and can keep state. Anthropic's Messages API uses tool_use and tool_result blocks with client or server tools. MCP standardizes tool discovery and execution over JSON-RPC. All four can build an agent; they differ in lock-in, observability and how many requests you pay for.

What makes an API an agent API

A chat API returns text. An agent API returns an action: a tool name and arguments, or a request for more information. Your application executes the action, returns the result, and the model decides whether to continue. The loop ends when the model returns a final answer, hits a step limit, or fails a guardrail.

That definition excludes a lot of marketing. A model that can describe a workflow is not an agent API. A model that can emit a validated tool call, receive a result and call another tool is. The API needs structured output, a stable call identifier and a way to correlate each result with its request.

The four building blocks

  • Tool schema. A JSON schema for each function, with a name, description and typed arguments. The description is part of the prompt and is billed as input tokens.
  • Loop controller. Code that runs the tool, enforces a maximum number of steps, and decides when to stop. Without it, an agent can loop until the budget is gone.
  • State. The conversation, tool outputs and any reasoning items. Some APIs store it; others require you to resend it.
  • Guardrails. Allowlists, timeouts, idempotency keys and human approval for irreversible actions. These are application code, not model features.

Three architectures compared

ArchitectureWho runs the loopStateToolsBest for
Chat CompletionsYour codeYou resend messagesClient-side functionsMaximum control and portability
Responses APIOpenAI for built-in tools; your code for custom functionsOptional server stateBuilt-in plus customFast builds with web search, file search or code execution
Anthropic MessagesYour code or the SDK tool runnerYou resend blocksClient and server toolsClaude-specific workflows and strict tool schemas
MCPThe host applicationThe server or hostStandardized JSON-RPC toolsReusing the same tools across apps and vendors

Architecture descriptions are from the linked provider documents, checked 20 November 2026. "Who runs the loop" is a design property, not a pricing statement.

Code: a bounded tool loop

The loop below uses the OpenAI SDK's Chat Completions shape, which is the most portable across providers. It caps the number of turns, validates the tool name against an allowlist, and returns a final answer only when the model stops calling tools. The same structure works with Groq, Gemini's compatibility endpoint or a local Ollama server.

import json, os
from openai import OpenAI

client = OpenAI(api_key=os.environ["API_KEY"], base_url=os.environ["BASE_URL"])
TOOLS = [{
    "type": "function",
    "function": {
        "name": "lookup_order",
        "description": "Look up an order by ID.",
        "parameters": {
            "type": "object",
            "properties": {"order_id": {"type": "string"}},
            "required": ["order_id"],
        },
    },
}]

def run_agent(messages, max_turns=4):
    for _ in range(max_turns):
        response = client.chat.completions.create(
            model=os.environ["MODEL"], messages=messages, tools=TOOLS
        )
        message = response.choices[0].message
        if not message.tool_calls:
            return message.content
        messages.append(message)
        for call in message.tool_calls:
            if call.function.name != "lookup_order":
                raise ValueError("tool not allowed")
            args = json.loads(call.function.arguments)
            result = {"order_id": args["order_id"], "status": "shipped"}
            messages.append({
                "role": "tool",
                "tool_call_id": call.id,
                "content": json.dumps(result),
            })
    raise RuntimeError("agent step limit reached")

Add a wall-clock timeout and a per-run token budget around this function. A step limit alone does not stop a single expensive tool call.

Cost and failure math

Every tool step is another inference request. Groq's tool-use guide puts a single tool call at two inference calls, a multi-tool workflow at three to five, and a complex agent loop at ten or more. If your free tier allows 1,000 requests per day, that is 500 single-tool turns, not 1,000. If your agent averages four calls per user request, it is 250 user requests per day.

Tool definitions also cost input tokens. Anthropic, for example, documents 286 tool-use system-prompt tokens for Claude Opus 5.5 with tool_choice set to auto or none, and 406 for any or a specific tool, before counting the tool descriptions and results. OpenAI says function definitions count against the context limit and are billed as input tokens. A large tool catalogue can cost more than the user's question.

The common failure modes are predictable: a tool returns malformed JSON, the model retries with the same arguments, the loop never terminates, or a write action is executed twice after a timeout. Idempotency keys, schema validation and a hard stop budget solve most of them. Human approval is still the right control for payments, deletions and privilege changes.

For a hosted option, Plugsky exposes an OpenAI-compatible endpoint at https://api.plugsky.com/v1 with 39 models and a 262,144-token context on the main text models. The free plan does not include agents; paid plans unlock the full catalogue. See pricing and docs. For implementation details, see agents with function calling and what is an AI agent API.

FAQ

What is an AI agent API?

An API that lets a model request tool calls, receive the results and continue in a loop until it produces a final answer or hits a limit. It needs structured tool output, call identifiers and a way to return results.

Is the OpenAI Responses API an agent API?

It can be. Responses supports built-in tools, custom functions and an agentic loop within one request for some tools, and it can keep state with previous_response_id. Custom function execution still happens in your code.

How many requests does an agent use?

At least two per tool call: one to decide on the call and one to use the result. Multi-tool workflows often use three to five, and complex loops can use ten or more.

What is MCP?

The Model Context Protocol is an open JSON-RPC protocol for exposing tools, resources and prompts to AI applications. It standardizes discovery and execution so the same tool server can work across hosts.

How do I stop an agent from looping forever?

Set a maximum number of steps, a wall-clock timeout and a token budget. Validate tool names against an allowlist and require human approval for irreversible actions.

Sources, checked 20 November 2026: OpenAI function calling · OpenAI migrate to Responses · Anthropic tool use · Groq tool use · Model Context Protocol specification. Provider limits and prices can change; confirm them in the provider console before capacity planning.

Reviewed 20 November 2026. This page is maintained by the Plugsky editorial team. It describes published provider limits and does not promise that a free tier will remain unchanged.