Skip to main content

Make an AI API call

Tetrate Agent Router exposes one OpenAI-compatible API that works with every model (OpenAI, Anthropic, Google, and others), with automatic failover, usage tracking, and cost controls.


info

All code snippets in this guide are Python. Any OpenAI SDK is supported; Python is not required.

Step 1: get an API key​

Sign up at router.tetrate.ai and create an API key from the Console. The key appears under Settings → API Keys.

An API key has the form sk-... and is required for every request. Keep it secure. The key identifies the caller and carries the fallback and traffic-splitting policies, budgets, and usage tracking attached to it, so each request is both authorized and governed by the key it presents.

Step 2: make the first request​

Because Agent Router implements the OpenAI API, an existing OpenAI SDK needs only two changes: the base URL (https://api.router.tetrate.ai/v1) and the API key. The request names a model, Agent Router resolves that model to a provider, forwards the call with the right credentials, and returns a standard chat completion response.

Python
from openai import OpenAI

client = OpenAI(
api_key="your-api-key",
base_url="https://api.router.tetrate.ai/v1",
)

response = client.chat.completions.create(
model="gpt-5.6-terra",
messages=[{"role": "user", "content": "What is Agent Router?"}],
)

print(response.choices[0].message.content)
tip

Replace gpt-5.6-terra with any model in Agent Router's catalog, such as claude-sonnet-5 or gemini-2.5-flash. To see which models are routable for a given API key, call GET /v1/models or client.models.list(); see Models API.

Step 3: stream responses​

Setting stream=True makes Agent Router forward tokens as the provider generates them, which lowers the time to first token and suits chat-style interfaces. The streamed chunks use the OpenAI event format, so client code written for OpenAI streaming works without modification.

Python
stream = client.chat.completions.create(
model="gpt-5.6-terra",
messages=[{"role": "user", "content": "Explain Agent Router in one paragraph."}],
stream=True,
)

for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")

Step 4: generate embeddings​

Embeddings convert text into numeric vectors for search, retrieval, or clustering workloads. They are requested through the same client and endpoint as chat completions, with only the method and model changing, and the same routing, usage tracking, and cost controls apply.

Python
response = client.embeddings.create(
model="text-embedding-3-small",
input="What is an AI gateway?",
)

print(f"Dimensions: {len(response.data[0].embedding)}")

Step 5: generate images​

An image request reaches the provider through the same endpoint and key used for text, and the response returns a URL to the generated image. Cost is recorded per image by size, quality, and count, and appears in the same usage reports as chat and embedding spend.

Python
response = client.images.generate(
model="dall-e-3",
prompt="A futuristic city powered by AI, digital art",
size="1024x1024",
)

print(response.data[0].url)

Use the tetrate CLI and typed SDKs​

The steps above use a generic OpenAI SDK pointed at the gateway. Agent Router also ships the tetrate CLI for minting keys and typed SDKs for Go, Python, and TypeScript, each with a runnable examples/quickstart. This path suits teams that prefer a typed client and bundled examples over raw HTTP.

All downloads are public; no sign-in is required. In the commands below, <version> is the version shown on the SDKs page, <keyid>.<secret> is a key minted below, and api.<your-domain> is the gateway host for the deployment.

Install the CLI and mint a key​

tetrate is the official Agent Router CLI and the fastest way to verify reach.

curl -fsSL https://tare.tetrate.ai/tools/install.sh | bash
tetrate --version

The installer verifies the signed release manifest, installs tetrate into ~/.tetrate/bin, and prints the PATH line to add when that directory is not on PATH yet.

Authenticate, then create a long-lived key the SDKs use as AGENTROUTER_API_KEY (the secret field is shown exactly once):

tetrate agentrouter api login --issuer https://idp.example.com
tetrate agentrouter api whoami
tetrate agentrouter api keys create --name quickstart

See the tetrate CLI reference for the available commands.

Download and run a typed SDK​

Download each SDK from the public stable channel, install it from the local file (no public registry), then run its bundled example, so a first call needs no copied snippets.

Go​

curl -fLO https://tare.tetrate.ai/tools/sdks/stable/agentrouter-go-<version>.tar.gz
mkdir -p third_party && tar -xzf agentrouter-go-<version>.tar.gz -C third_party/

# go.mod:
require github.com/tetrateio/agentrouter-go v0.1.0
replace github.com/tetrateio/agentrouter-go => ./third_party/agentrouter-go-<version>

go mod tidy

From the extracted SDK root: go run ./examples/quickstart.

Python​

curl -fLO https://tare.tetrate.ai/tools/sdks/stable/agentrouter-python-<version>.tar.gz
pip install ./agentrouter-python-<version>.tar.gz

The sdist unpacks examples/quickstart next to the package: python examples/quickstart/quickstart.py.

TypeScript​

curl -fLO https://tare.tetrate.ai/tools/sdks/stable/agentrouter-typescript-<version>.tgz
npm install ./agentrouter-typescript-<version>.tgz

From the extracted SDK root run bun install once, then cd examples/quickstart && bun install && bun run quickstart.ts.

curl​

No SDK is needed; the API can be called directly with the key:

export AGENTROUTER_API_KEY=<keyid>.<secret>
curl -sS https://api.<your-domain>/v1/me \
-H "Authorization: Bearer ${AGENTROUTER_API_KEY}"