Instead of managing API keys per-model and per-environment, the AI Gateway provides a centralized, authenticated entry point for all LLM interactions. It handles authentication, agent ID validation, and policy enforcement - while maintaining full OpenAI SDK compatibility.
The gateway wraps the OpenAI SDK - any framework that works with OpenAI works with AI Gateway. No code changes required.
Prerequisite: Ensure you’ve installed the SDK and configured your environment before proceeding.
To trace all gateway calls, set up observability and instrument the OpenAI library:
from bb_ai_sdk.observability import init, get_tracer_providerfrom bb_ai_sdk.ai_gateway import AIGatewayfrom openinference.instrumentation.openai import OpenAIInstrumentor# Step 1: initialize observabilityinit(agent_name="my-agent")# Step 2: instrument OpenAI library (required for tracing)# For vanilla code, openaiinstrumentor is used to trace openai-compatible clientsprovider = get_tracer_provider()OpenAIInstrumentor().instrument(tracer_provider=provider)# Step 3: create gateway - calls will now be tracedgateway = AIGateway.create(model_id="gpt-4o", agent_id="...")response = gateway.chat.completions.create( model="gpt-4o", messages=[{"role": "user", "content": "Hello!"}])# This call appears in Langfuse with full context: tokens, latency, cost
Initialize observability and instrument OpenAI before creating the gateway to ensure all calls are captured. See Observability for full configuration options.
For real-time responses (chatbots, interactive UIs), enable streaming:
Sync streaming
Async streaming
stream = gateway.chat.completions.create( model="gpt-4o", messages=[{"role": "user", "content": "Write a story"}], stream=True)for chunk in stream: if chunk.choices[0].delta.content: print(chunk.choices[0].delta.content, end="")
stream = await gateway.chat.completions.create( model="gpt-4o", messages=[{"role": "user", "content": "Write a story"}], stream=True)async for chunk in stream: if chunk.choices[0].delta.content: print(chunk.choices[0].delta.content, end="")
The AI Gateway is OpenAI-compatible out of the box, but if you’re using LangChain, LangGraph, or Agno, adapters convert the gateway into framework-native objects - no manual configuration required.
from bb_ai_sdk.ai_gateway import AIGatewayfrom bb_ai_sdk.ai_gateway.adapters.langchain import to_langchaingateway = AIGateway.create(model_id="gpt-4o", agent_id="...")model = to_langchain(gateway) # Returns a ChatOpenAI-compatible model# Use with LangChain componentsfrom langchain.schema.output_parser import StrOutputParserchain = model | StrOutputParser()response = chain.invoke("Tell me a joke")