Integrate your agent
Code snippets for integrating Rungate with the OpenAI SDK, the Anthropic SDK, raw HTTP, and popular agent frameworks.
Rungate is a drop-in replacement for the OpenAI and Anthropic API
endpoints. Point your existing SDK at Rungate's base URL, swap
your provider key for an rg_agt_* token, and the
request/response shape is unchanged.
Base URL: https://app.rungate.dev/v1
OpenAI SDK · Python
from openai import OpenAI
client = OpenAI(
base_url="https://app.rungate.dev/v1",
api_key="rg_agt_your_token_here",
)
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "..."}],
extra_headers={
"x-rungate-run-id": "run_customer_refund_2026_04_26",
"x-rungate-policy": "prod-agents/v4",
},
)
# Streaming
stream = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "..."}],
stream=True,
extra_headers={"x-rungate-run-id": "run_customer_refund_2026_04_26"},
)
for chunk in stream:
print(chunk.choices[0].delta.content or "", end="") OpenAI SDK · TypeScript
import OpenAI from 'openai';
const client = new OpenAI({
baseURL: 'https://app.rungate.dev/v1',
apiKey: 'rg_agt_your_token_here',
});
const response = await client.chat.completions.create({
model: 'gpt-4',
messages: [{ role: 'user', content: '...' }],
}, {
headers: {
'x-rungate-run-id': 'run_customer_refund_2026_04_26',
'x-rungate-policy': 'prod-agents/v4',
},
}); Anthropic SDK · Python
from anthropic import Anthropic
client = Anthropic(
base_url="https://app.rungate.dev/v1",
api_key="rg_agt_your_token_here",
)
message = client.messages.create(
model="claude-sonnet-4",
max_tokens=1024,
messages=[{"role": "user", "content": "..."}],
extra_headers={
"x-rungate-run-id": "run_customer_refund_2026_04_26",
},
) Anthropic SDK · TypeScript
import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic({
baseURL: 'https://app.rungate.dev/v1',
apiKey: 'rg_agt_your_token_here',
});
const message = await client.messages.create({
model: 'claude-sonnet-4',
max_tokens: 1024,
messages: [{ role: 'user', content: '...' }],
}, {
headers: {
'x-rungate-run-id': 'run_customer_refund_2026_04_26',
},
}); curl
curl -X POST https://app.rungate.dev/v1/chat/completions \
-H "Authorization: Bearer rg_agt_your_token_here" \
-H "x-rungate-run-id: run_customer_refund_2026_04_26" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4",
"messages": [{"role": "user", "content": "Hello, Rungate."}]
}' Headers reference
All directives work as x-rungate-* headers, or as
body fields under a rungate key (use the body form if
your SDK strips unknown headers):
| Header | Body field | Purpose |
|---|---|---|
x-rungate-run-id | rungate.run_id | Stable ID grouping calls into one run. |
x-rungate-new-run | rungate.new_run | Boolean. Force a new run on this call. |
x-rungate-policy | rungate.policy | Override the agent's default policy. |
x-rungate-user | rungate.user | End-user identifier for analytics. |
x-rungate-tags | rungate.tags | Comma-separated tags. |
Frameworks
Rungate is provider-shape compatible, so any framework that lets
you configure a custom base_url works out of the
box. A few popular ones:
LangChain
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
base_url="https://app.rungate.dev/v1",
api_key="rg_agt_your_token_here",
model="gpt-4",
default_headers={"x-rungate-run-id": "run_..."},
) LlamaIndex
from llama_index.llms.openai import OpenAI
llm = OpenAI(
api_base="https://app.rungate.dev/v1",
api_key="rg_agt_your_token_here",
model="gpt-4",
additional_kwargs={"extra_headers": {"x-rungate-run-id": "run_..."}},
) OpenClaw
Set the RUNGATE_BASE_URL and RUNGATE_AGENT_KEY
environment variables. OpenClaw routes provider calls through
Rungate automatically.
export RUNGATE_BASE_URL=https://app.rungate.dev
export RUNGATE_AGENT_KEY=rg_agt_your_token_here What Rungate forwards unchanged
- The full request body (any OpenAI Chat Completions or Anthropic Messages payload).
-
Streaming (
stream: true). The response is a standard SSE stream. - Function calls / tool use — the provider's native shape.
- Vision / multimodal payloads.
What Rungate may add or change
-
Governance response codes (
202,402,403,429) before forwarding to the provider. - Cross-provider failover — the response format the agent sent is preserved, but the actual response may come from a fallback provider.
-
An
X-Rungate-Run-Idresponse header echoing the run the call was attached to.
Failure modes
See response codes in the agent reference for the full table. Quick map:
200— success.202— approval gate (see Approval gates).402— budget exceeded (see Policies & budgets).403— policy violation.409— run already closed; start a new run.429— rate limit; honorRetry-After.5xx— Rungate or upstream provider error; exponential backoff.