Skip to main content
This page shows how to:
  • Call any LLM with the same API. TensorZero unifies every major LLM API (e.g. OpenAI) and inference server (e.g. Ollama).
  • Get started with a few lines of code. Later, you can optionally add observability, automatic fallbacks, A/B testing, and much more.
  • Use any programming language. You can use TensorZero with any OpenAI SDK (Python, Node, Go, etc.).
You can find a complete runnable example of this guide on GitHub.
You can point the OpenAI Python SDK to a TensorZero Gateway to call any LLM with a unified API.
1

Set up the credentials for your LLM provider

For example, if you’re using OpenAI, you can set the OPENAI_API_KEY environment variable with your API key.
export OPENAI_API_KEY="sk-..."
See the Integrations page to learn how to set up credentials for other LLM providers.
2

Install the OpenAI Python SDK

You can install the OpenAI SDK with a Python package manager like pip.
pip install openai
3

Deploy the TensorZero Gateway

Let’s deploy the TensorZero Gateway using Docker. For simplicity, we’ll use the gateway without observability or custom configuration.
docker run \
  -e OPENAI_API_KEY \
  -p 3000:3000 \
  tensorzero/gateway \
  --default-config
See the Deploy the TensorZero Gateway page for more details.
4

Initialize the OpenAI client

Let’s initialize the OpenAI SDK and point it to the gateway we just launched.
from openai import OpenAI

client = OpenAI(base_url="http://localhost:3000/openai/v1", api_key="not-used")
5

Call the LLM

response = client.chat.completions.create(
    model="tensorzero::model_name::openai::gpt-5-mini",
    # or: model="tensorzero::model_name::anthropic::claude-sonnet-4-20250514"
    # or: Google, AWS, Azure, xAI, vLLM, Ollama, and many more
    messages=[
        {
            "role": "user",
            "content": "Tell me a fun fact.",
        }
    ],
)
ChatCompletion(
    id='0198d33f-24f6-7cc3-9dd0-62ba627b27db',
    choices=[
        Choice(
            finish_reason='stop',
            index=0,
            logprobs=None,
            message=ChatCompletionMessage(
                content='Sure! Did you know that octopuses have three hearts? Two pump blood to the gills, while the third pumps it to the rest of the body. And, when an octopus swims, the heart that delivers blood to the body actually **stops beating**—which is why they prefer to crawl rather than swim!',
                refusal=None,
                role='assistant',
                annotations=None,
                audio=None,
                function_call=None,
                tool_calls=[]
            )
        )
    ],
    created=1755890789,
    model='tensorzero::model_name::openai::gpt-5-mini',
    object='chat.completion',
    service_tier=None,
    system_fingerprint='',
    usage=CompletionUsage(
        completion_tokens=67,
        prompt_tokens=13,
        total_tokens=80,
        completion_tokens_details=None,
        prompt_tokens_details=None
    ),
    episode_id='0198d33f-24f6-7cc3-9dd0-62cd7028c3d7'
)
See the Inference (OpenAI) API Reference for more details on the request and response formats.
See Configure models and providers to set up multiple providers with routing and fallbacks and Configure functions and variants to manage your LLM logic with experimentation and observability.