Frameworks

OpenAI Agents SDK with Claude, Gemini and other models: cost tracking and run budgets through a gateway

Run OpenAI Agents SDK agents on any model by pointing the default client at an OpenAI-compatible gateway. Track cost per agent and per run, and stop runaway agents with a dollar budget.

The Agents SDK is built on the OpenAI client, so the gateway plugs in as the default client. Every agent, handoff and tool-calling turn then goes through it, on any model.

Setup

# pip install openai-agents
from openai import AsyncOpenAI
from agents import (Agent, Runner, function_tool, set_default_openai_api,
                    set_default_openai_client, set_tracing_disabled)
 
client = AsyncOpenAI(
    api_key="sk-cog-YOUR-KEY",
    base_url="https://api.cognocient.com/v1",
    default_headers={"X-Cost-Feature": "support-triage"},
)
set_default_openai_client(client, use_for_tracing=False)
set_default_openai_api("chat_completions")   # portable across every provider
set_tracing_disabled(True)                     # or set_tracing_export_api_key("sk-...openai...")
 
@function_tool
def lookup_order(order_id: str) -> str:
    return f"Order {order_id}: shipped"
 
billing = Agent(name="Billing", instructions="Answer billing questions.", model="gpt-4o-mini")
triage = Agent(
    name="Triage",
    instructions="Route the user to the right specialist, or look up their order.",
    model="claude-sonnet-4-6",
    tools=[lookup_order],
    handoffs=[billing],
)
 
result = Runner.run_sync(triage, "Where is order 1042?")
print(result.final_output)

Each agent can use a different model and provider; the gateway records each call against its real model and price.

Cost per run

Tag a run by creating the client per run, or by passing headers through ModelSettings:

from agents import ModelSettings
 
triage = Agent(
    name="Triage",
    model="claude-sonnet-4-6",
    model_settings=ModelSettings(extra_headers={"X-Cost-Run-ID": run_id, "X-Cost-Run-Budget": "0.75"}),
)

With X-Cost-Run-Budget the gateway stops the run with a 429 (run_budget_exceeded) once it has spent that amount. The SDK raises it as an exception from Runner.run, which you can catch and turn into a fallback answer. max_turns limits turns; the run budget limits dollars.

Responses API mode

You can keep the SDK on the Responses API with set_default_openai_api("responses"). The gateway's /v1/responses is stateless: the SDK's default behaviour of sending the full input each turn works, but previous_response_id and hosted tools don't. Chat Completions mode is the safer default for non-OpenAI models.

On this page