AICredits logo
Integrations

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 Setup

import asyncio
from agents import Agent, Runner, function_tool, set_tracing_disabled
from agents.models.openai_chatcompletions import OpenAIChatCompletionsModel
from openai import AsyncOpenAI

set_tracing_disabled(True)

client = AsyncOpenAI(
    base_url="https://api.aicredits.in/v1",
    api_key="sk-your-aicredits-key",
)

@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=OpenAIChatCompletionsModel(
        model="openai/gpt-4o-mini",
        openai_client=client,
    ),
)

async def main():
    result = await Runner.run(agent, "How do I verify an AICredits request?")
    print(result.final_output)

asyncio.run(main())

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 - Use the Chat Completions model path shown above because AICredits is OpenAI-compatible at https://api.aicredits.in/v1.

Tracing errors appear - Disable tracing 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.

On this page