Client API examples¶
The gateway implements the documented OpenAI API routes. Clients that use those
routes can point their base URL at the gateway and send the platform headers. The
OpenAPI contract
and scope list the exact routes and fields.
These examples use http://127.0.0.1:8080, where the Docker Compose stack
serves the gateway with API key local-development-only. In the kind lab, port-forward the
gateway service first; in a customer cluster, use your ingress host.
For durable, budgeted agent steps, start with bring your agent: the workflow adapters configure these clients with run correlation and show OpenAI Agents SDK, LangGraph, and approved MCP tools. The standalone examples below call the gateway directly, outside Temporal runs.
The model id must be on the active profile's allowlist. Compose defaults to the synthetic
demo-openai route; its fixtures demonstrate the chat flow.
Configure an approved provider for real inference using model selection.
The optional local lab allows qwen2.5:0.5b; customer self-hosted profiles use qwen3.5:0.8b (Ollama) and
Qwen/Qwen3-Coder-Next (vLLM). Streaming is admitted by default in every shipped
profile (admission.allowStreaming: true); a deployment that requires end-of-stream-only
guardrail enforcement sets it false. Examples below that
use a customer model are marked accordingly.
All business endpoints accept:
Authorization: Bearer <api-key-or-jwt>(or theX-API-Keyheader) when auth is enabledX-Sandbox-ID: <sandbox>: the tenant/sandbox the request is attributed to. A credential bound to a sandbox (an API-key record or a JWT tenant claim) supplies it automatically, and a header naming a different sandbox returns403.X-Request-IDand W3Ctraceparent: optional, echoed back for tracing
Responses echo X-Request-ID, X-Sandbox-ID, and traceparent for correlation. When
sandbox budgets are enabled, /v1/chat/completions (including streaming) and
/v1/embeddings responses also carry the OpenAI-style budget headers that agent
frameworks parse to pace themselves:
x-ratelimit-limit-requests/x-ratelimit-remaining-requests: the sandbox request budget and what remains of it in the current windowx-ratelimit-limit-tokens/x-ratelimit-remaining-tokens: the estimated-token budget and what remains of it, floored at zero
Each pair is present only when the corresponding limit is configured (greater than zero);
cache hits (X-Cache: HIT) are free of budget and omit them. See the
budget controls runbook for sizing and triage.
curl¶
GATEWAY=http://127.0.0.1:8080
KEY=local-development-only
# Chat completion
curl -fsS "$GATEWAY/v1/chat/completions" \
-H "Authorization: Bearer $KEY" -H "X-Sandbox-ID: demo" \
-H 'Content-Type: application/json' \
-d '{"messages":[{"role":"user","content":"hello"}]}'
# Embeddings (requires a configured provider/model with embeddings support)
curl -fsS "$GATEWAY/v1/embeddings" \
-H "Authorization: Bearer $KEY" -H "X-Sandbox-ID: demo" \
-H 'Content-Type: application/json' \
-d '{"input":"embed this text"}'
# Legacy text completion (prompt-based; governed like chat, non-streaming)
curl -fsS "$GATEWAY/v1/completions" \
-H "Authorization: Bearer $KEY" -H "X-Sandbox-ID: demo" \
-H 'Content-Type: application/json' \
-d '{"prompt":"Write a haiku about the sea.","max_tokens":64}'
# Native Anthropic Messages API (governed like chat; add "stream":true for SSE). max_tokens is required.
curl -fsS "$GATEWAY/v1/messages" \
-H "Authorization: Bearer $KEY" -H "X-Sandbox-ID: demo" \
-H 'Content-Type: application/json' \
-d '{"model":"demo-openai","max_tokens":64,"messages":[{"role":"user","content":"hello"}]}'
# OpenAI Responses API (synchronous; optional tenant-scoped server-side state).
curl -fsS "$GATEWAY/v1/responses" \
-H "Authorization: Bearer $KEY" -H "X-Sandbox-ID: demo" \
-H 'Content-Type: application/json' \
-d '{"model":"demo-openai","input":"hello","max_output_tokens":64}'
# Moderations (content policy classification)
curl -fsS "$GATEWAY/v1/moderations" \
-H "Authorization: Bearer $KEY" -H "X-Sandbox-ID: demo" \
-H 'Content-Type: application/json' \
-d '{"input":"text to classify"}'
Moderations taxonomy (governance categories)¶
/v1/moderations is OpenAI-compatible in shape and classifies against the platform
governance taxonomy. The response carries a top-level
"taxonomy": "governance" marker and the categories/category_scores keys are
credential, pii, and blocked_terms (rule-based credential/PII/denylist detection).
Branch on the taxonomy field if you consume both an OpenAI moderation endpoint and this one.
Legacy /v1/completions (prompt-based)¶
The gateway also exposes the pre-chat /v1/completions endpoint for tools that still use a
prompt (a string or list of strings) instead of messages. It runs through the same
governance path as chat (model allowlist, admission limits, prompt secret policy, sandbox
budget, output guardrail, and audit), so legacy-completion traffic gets the same controls.
/v1/completions is non-streaming: send stream: false, and use /v1/chat/completions
for streaming (a streaming request returns streaming_not_supported). Prefer
/v1/chat/completions for new integrations.
Native Anthropic Messages API (/v1/messages)¶
The gateway exposes a native Anthropic-shaped /v1/messages endpoint, so Anthropic-SDK
and Claude-style agents can point at the gateway directly. The Anthropic request and response are translated to and from the
internal OpenAI chat shape and run through the same governance path as chat (model
allowlist, admission limits, prompt secret policy, sandbox budget, output guardrail, and
audit), so /v1/messages traffic gets the same controls. Anthropic requires
max_tokens; the gateway validates it and enforces it against the completion-token cap. The response is an Anthropic Message (type: "message",
content blocks, stop_reason, usage.input_tokens/output_tokens).
curl -fsS "$GATEWAY/v1/messages" \
-H "Authorization: Bearer $KEY" -H "X-Sandbox-ID: demo" \
-H 'Content-Type: application/json' \
-d '{
"model": "qwen2.5:0.5b",
"max_tokens": 128,
"system": "You are a terse assistant.",
"messages": [{"role": "user", "content": "Give me one fact about the sea."}]
}'
Message and system text translate exactly. Anthropic tool
definitions (name/description/input_schema) and tool_use/tool_result content blocks
map to their closest OpenAI equivalents. For other Anthropic-shaped content blocks, the
translation-sidecar approach below is available.
Streaming¶
stream: true returns the Anthropic event sequence (message_start, then a
content_block_start / content_block_delta / content_block_stop run per content block,
then message_delta with the stop reason and usage, then message_stop), translated from
the same governed chat stream /v1/chat/completions uses. Interactive Claude-style agents
use this path, since the Anthropic SDKs stream by default.
import anthropic
client = anthropic.Anthropic(base_url=GATEWAY, api_key=KEY)
with client.messages.stream(
model="demo-openai",
max_tokens=128,
messages=[{"role": "user", "content": "Give me one fact about the sea."}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
final = stream.get_final_message()
Streaming obeys the same admission.allowStreaming toggle as chat, so one setting governs
both surfaces. Streaming works as follows:
- Token counts arrive at the end. OpenAI-compatible runtimes only report usage on a
terminal event, which is after
message_starthas to be sent, somessage_startcarries zeros and the true counts ride on the finalmessage_delta. The Anthropic SDKs reconcile the final usage from there, soget_final_message().usageis correct. - The output guardrail flags streamed output. The guardrail scans the assistant text at end-of-stream and records a finding. Use non-streaming requests where the guardrail should block output.
The translator reads delta.content and delta.tool_calls only, so reasoning and thinking
deltas stay out of the Anthropic events.
The service tests exercise translated payloads and streaming. The Compose walkthrough uses local protocol fixtures; run it against your provider and client versions to confirm your setup.
OpenAI Responses API (/v1/responses)¶
The gateway exposes the synchronous OpenAI Responses API, so tooling built
on client.responses.create(...) can point at the gateway directly. The Responses request and
response are translated to and from the internal OpenAI chat shape and run through the same
governance path as chat (model allowlist, admission limits, prompt secret policy, sandbox
budget, output guardrail, and audit), so /v1/responses traffic gets the same controls.
input accepts a plain string or an array of input items/messages; instructions is prepended
as a system message; max_output_tokens maps to the gateway's completion-token cap and is
enforced against it. The response is a Responses object (object: "response", status,
output[] with output_text content parts and any function_call items,
usage.input_tokens/output_tokens/total_tokens).
curl -fsS "$GATEWAY/v1/responses" \
-H "Authorization: Bearer $KEY" -H "X-Sandbox-ID: demo" \
-H 'Content-Type: application/json' \
-d '{
"model": "qwen2.5:0.5b",
"max_output_tokens": 128,
"instructions": "You are a terse assistant.",
"input": "Give me one fact about the sea."
}'
Server-side state is opt-in, since it persists raw conversation content.
Set RESPONSES_STORE_ENABLED=true and use the Redis backend for multi-replica deployments;
then store: true, previous_response_id, GET/DELETE /v1/responses/{id}, and
GET /v1/responses/{id}/input_items are tenant-scoped and TTL-bounded. With state off,
those requests return stateful_not_supported. /v1/responses is non-streaming: send
stream: false, and use /v1/chat/completions for OpenAI-shaped streaming (a streaming request
returns streaming_not_supported). Assistant tool_calls map to
function_call output items (name, arguments, call_id), and a
length finish maps to status: "incomplete" with incomplete_details.reason:
"max_output_tokens".
Error responses¶
Every gateway error body is OpenAI-shaped:
{
"error": {
"message": "requested completion tokens is 50; limit is 10",
"type": "invalid_request_error",
"code": "max_tokens_too_large",
"request_id": "…",
"sandbox_id": "demo"
},
"detail": { "…": "original detail, preserved this release" }
}
error.type follows the OpenAI taxonomy (invalid_request_error,
authentication_error, permission_error, rate_limit_error, api_error, …) so an
OpenAI SDK's typed exceptions (e.g. RateLimitError) map correctly. error.code carries
the gateway's machine reason. The legacy detail object is preserved alongside for one
release while callers migrate; build new integrations on error. Pydantic request-validation
errors (HTTP 422) keep FastAPI's default {"detail": [...]} shape.
Python (openai SDK)¶
from openai import OpenAI
client = OpenAI(
base_url="http://127.0.0.1:8080/v1",
api_key="local-development-only",
default_headers={"X-Sandbox-ID": "demo"},
)
# Tool-calling (the flagship coding-agent path) is forwarded to the runtime.
resp = client.chat.completions.create(
model="demo-openai",
messages=[{"role": "user", "content": "What is the weather in Berlin?"}],
tools=[{
"type": "function",
"function": {
"name": "get_weather",
"parameters": {"type": "object", "properties": {"city": {"type": "string"}}},
},
}],
)
print(resp.choices[0].message)
# Streaming (admitted by default in every shipped profile)
for chunk in client.chat.completions.create(
model="demo-openai",
messages=[{"role": "user", "content": "stream a haiku"}],
stream=True,
):
print(chunk.choices[0].delta.content or "", end="")
Python (plain httpx, no SDK)¶
import httpx
gateway = "http://127.0.0.1:8080"
headers = {"Authorization": "Bearer local-development-only", "X-Sandbox-ID": "demo"}
r = httpx.post(
f"{gateway}/v1/chat/completions",
headers=headers,
json={"messages": [{"role": "user", "content": "hello"}]},
timeout=120,
)
r.raise_for_status()
print(r.json()["choices"][0]["message"]["content"])
Python (first-party client)¶
The platform ships a minimal, retry-aware first-party client (sdk/python, packaged as
agentworkflows) for lightweight scripts:
python -m pip install https://github.com/RamazanKara/agentworkflows/releases/download/v0.10.0/agentworkflows-0.10.0-py3-none-any.whl
The wheel and checksums are attached to each GitHub release. See release verification before installing.
from agentworkflows import GatewayClient
with GatewayClient("http://127.0.0.1:8080", api_key="local-development-only") as gw:
print(gw.chat([{"role": "user", "content": "hello"}])["choices"][0]["message"]["content"])
for delta in gw.chat_stream([{"role": "user", "content": "stream a haiku"}]):
print(delta, end="")
Agent & coding frameworks (drop-in)¶
Because the gateway is OpenAI-compatible, agent and coding frameworks work by pointing their
OpenAI base URL at the gateway and adding the X-Sandbox-ID header. Route all framework traffic
through the gateway so auth, model allowlists, budgets, guardrails, and audit apply.
LangChain¶
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
base_url="http://127.0.0.1:8080/v1",
api_key="local-development-only",
model="demo-openai",
default_headers={"X-Sandbox-ID": "demo"},
)
print(llm.invoke("hello").content)
LlamaIndex¶
from llama_index.llms.openai_like import OpenAILike
llm = OpenAILike(
api_base="http://127.0.0.1:8080/v1",
api_key="local-development-only",
model="demo-openai",
is_chat_model=True,
default_headers={"X-Sandbox-ID": "demo"},
)
print(llm.complete("hello"))
Aider (coding agent)¶
Assumes a customer vLLM profile serving Qwen/Qwen3-Coder-Next; point the base URL at
your gateway host.
export OPENAI_API_BASE=http://127.0.0.1:8080/v1
export OPENAI_API_KEY=local-development-only
# Bind Aider's sandbox with a JWT tenant claim (auth.jwt.tenantClaim)
# or run Aider from inside an agent-workspace namespace whose egress sets X-Sandbox-ID at the proxy.
aider --model openai/Qwen/Qwen3-Coder-Next
Continue / Cline (VS Code)¶
Point the assistant at an OpenAI-compatible provider with apiBase:
http://<gateway-host>/v1, the API key, and a requestOptions.headers entry setting
X-Sandbox-ID. The gateway's /v1/models lists the approved models to configure.
For frameworks without custom header support, bind the sandbox with a JWT tenant claim (
auth.jwt.tenantClaim) so per-sandbox budgets and attribution stay tied to the verified tenant.
Anthropic SDK / Claude-style agents (translation sidecar as an alternative)¶
The gateway now exposes a native Anthropic /v1/messages endpoint (see above), which is the
preferred path for Anthropic-SDK and Claude-style agents. A translation sidecar is a
supported alternative for additional Anthropic-shaped content blocks. For example, a
LiteLLM proxy that exposes an Anthropic-shaped /v1/messages
endpoint and forwards to the gateway's /v1/chat/completions. The sidecar does the
Anthropic-to-OpenAI request/response translation; the gateway still applies auth, model
allowlists, budgets, guardrails, and audit. Point the sidecar's upstream at the gateway and
set the platform headers on the forwarded request.
Minimal LiteLLM config sketch (config.yaml), with the gateway as the OpenAI-compatible
upstream:
model_list:
- model_name: claude-shim # the name Anthropic-SDK clients request
litellm_params:
model: openai/qwen2.5:0.5b # an allowlisted gateway model
api_base: http://<gateway-host>/v1
api_key: os.environ/GATEWAY_API_KEY
extra_headers:
X-Sandbox-ID: demo # or bind the sandbox via a JWT tenant claim
Anthropic-SDK clients that use the sidecar point base_url at it in place of the gateway's
native /v1/messages; the sidecar maps requests onto OpenAI chat completions. See
scope for the exact list of protocol surfaces the gateway implements.