Documentation

OpenAI Agents SDK

How to use GATE/0 with OpenAI Agents SDK

Introduction

The OpenAI Agents SDK provides a lightweight, production-ready framework for building agentic AI applications with minimal abstractions. It introduces powerful core primitives — Agents, Handoffs, and Guardrails — that simplify the creation of complex, tool-using LLM workflows in Python.

GATE/0 integrates seamlessly with the OpenAI Agents SDK and is fully compatible with its API layer. The following guide walks you through using GATE/0 with the SDK.

Using GATE/0 with the OpenAI Agents SDK

Set up your OpenAI provider in GATE/0

  1. Retrieve your OpenAI API key from OpenAI
  2. Add a new OpenAI provider in GATE/0 here
  3. Copy your GATE/0 API key from here

Install OpenAI Agents SDK

pip install openai-agents

Configure the GATE/0 API key Set your GATE/0 API key as an environment variable:

export GATE0_API_KEY=your-gate0-api-key

Implement your agent

import asyncio
import os
from agents import (
    Agent, 
    Runner, 
    OpenAIResponsesModel, 
    ModelSettings
)
from openai import AsyncOpenAI

async def main():
    client = AsyncOpenAI(
        base_url="https://gateway.gate.io/v1", # GATE0 cloud proxy URL
        api_key=os.environ.get("GATE0_API_KEY") # API key provided by GATE0
    )

    agent = Agent(
        name="Assistant", 
        instructions="You only respond in haikus.", 
        model=OpenAIResponsesModel(
            model="openai/gpt-4o", # Model name with provider prefix
            openai_client=client
        ),
        model_settings=ModelSettings(
            extra_headers={ # Custom labels
                'x-gate0-label-project': 'alpha', 
                'x-gate0-label-client': 'frontend', 
                'x-gate0-label-env': 'prod'
            }
        ),
    )

    result = await Runner.run(agent, "Tell me about recursion in programming.")
    print(result.final_output)


if __name__ == "__main__":
    asyncio.run(main())

Explanation of the implementation

  1. An instance of AsyncOpenAI is created with the base URL set to https://gateway.gate0.io/v1, using your GATE0 API key instead of the default OpenAI key.
  2. An OpenAIResponsesModel is initialized with the client and a prefixed model name. Make sure the model name is prefixed with the provider slug (e.g., use openai/gpt-4o instead of gpt-4o).
  3. A ModelSettings instance is created with extra_headers to include custom labels — useful for resource allocation and cost tracking.
  4. The model and model_settings objects are passed to the Agent constructor to finalize the setup.