OpenAI Agents SDK
Build Python and TypeScript agents with the OpenAI Agents SDK while routing model calls through AICredits billing.
Use this page with an AI assistant
Opens a new chat with this docs URL and the correct AICredits base URLs.
The OpenAI Agents SDK can use OpenAI-compatible Chat Completions models. Point the SDK client at AICredits so agent runs, tool calls, and model switching are billed through your AICredits wallet.
Python Quick Start
import asyncio
from agents import (
Agent,
Runner,
function_tool,
set_default_openai_api,
set_default_openai_client,
set_tracing_disabled,
)
from openai import AsyncOpenAI
set_tracing_disabled(True)
client = AsyncOpenAI(
base_url="https://api.aicredits.in/v1",
api_key="sk-your-aicredits-key",
)
set_default_openai_client(client, use_for_tracing=False)
set_default_openai_api("chat_completions")
@function_tool
def search_docs(query: str) -> str:
return f"Docs result for {query}: use the AICredits base URL."
agent = Agent(
name="Docs helper",
instructions="Answer briefly and call search_docs when useful.",
tools=[search_docs],
model="openai/gpt-4o-mini",
)
async def main():
result = await Runner.run(agent, "How do I verify an AICredits request?")
print(result.final_output)
asyncio.run(main())set_default_openai_client() makes the SDK use AICredits for model calls. set_default_openai_api("chat_completions") is recommended for AICredits-routed agents because Chat Completions is the broadest compatibility path across OpenAI-compatible models. Disable tracing with set_tracing_disabled(True) unless you have configured a separate OpenAI tracing key.
Tool Calling
import asyncio
from agents import Agent, Runner, function_tool
@function_tool
def get_order_status(order_id: str) -> str:
return f"Order {order_id} is paid and ready for fulfilment."
agent = Agent(
name="Support assistant",
instructions="Use tools for order-specific answers.",
model="anthropic/claude-sonnet-4.5",
tools=[get_order_status],
)
async def main():
result = await Runner.run(agent, "What is the status of order AIC-1024?")
print(result.final_output)
asyncio.run(main())Tool calls are normal model requests routed through AICredits. Check Dashboard → Usage to see each run's model, tokens, and INR cost.
Handoffs
import asyncio
from agents import Agent, Runner
billing_agent = Agent(
name="Billing specialist",
handoff_description="Handles wallet balance, invoices, and payment questions.",
instructions="Answer billing questions clearly and ask for missing account details.",
model="openai/gpt-4o-mini",
)
technical_agent = Agent(
name="Technical specialist",
handoff_description="Handles API errors, SDK setup, and integration debugging.",
instructions="Give concise debugging steps with exact endpoints and model IDs.",
model="anthropic/claude-sonnet-4.5",
)
triage_agent = Agent(
name="Triage",
instructions="Route the user to the best specialist when needed.",
model="openai/gpt-4o-mini",
handoffs=[billing_agent, technical_agent],
)
async def main():
result = await Runner.run(triage_agent, "My bot gets 402 from AICredits.")
print(result.final_output)
asyncio.run(main())Use lower-cost models for triage and reserve stronger models for specialist agents.
TypeScript Setup
import { Agent, OpenAIChatCompletionsModel, run, tool } from "@openai/agents";
import OpenAI from "openai";
import { z } from "zod";
const client = new OpenAI({
baseURL: "https://api.aicredits.in/v1",
apiKey: "sk-your-aicredits-key",
});
const searchDocs = tool({
name: "search_docs",
description: "Search AICredits docs",
parameters: z.object({ query: z.string() }),
execute: async ({ query }) => `Docs result for ${query}: check /dashboard/usage.`,
});
const agent = new Agent({
name: "Docs helper",
instructions: "Answer briefly and call search_docs when useful.",
tools: [searchDocs],
model: new OpenAIChatCompletionsModel(client, "openai/gpt-4o-mini"),
});
const result = await run(agent, "How do I verify an AICredits request?");
console.log(result.finalOutput);Use anthropic/claude-sonnet-4.5 for more complex agent loops and google/gemini-2.5-flash for long-context planning. See Models for the full list.
Verify
Ask the agent:
Reply with "AICredits via OpenAI Agents SDK" after calling one tool.Then check Dashboard -> Usage.
Troubleshooting
Requests fail on Responses API - Set set_default_openai_api("chat_completions") for the Python SDK, or use OpenAIChatCompletionsModel in code that needs an explicit model adapter.
Tracing errors appear - Use set_tracing_disabled(True) for gateway-only use, or configure a separate OpenAI tracing key if you need hosted traces.
Model not available - Use an exact model ID from Models, such as openai/gpt-4o-mini, anthropic/claude-sonnet-4.5, or google/gemini-2.5-flash.
Tool calls are not returned - Confirm the selected model supports tool calling and keep tool schemas simple. See Tool Calling for the supported OpenAI-compatible shape.
402 Payment Required - Add credits at Dashboard → Billing or raise the API key's budget cap.
429 Too Many Requests - Reduce parallel agent runs or add a client-side queue. See Rate Limits.