The opentoken SDK (Python and JavaScript) sends each call either through the Open Token gateway (budgets, run limits, policies) or straight to Amazon Bedrock with your own AWS credentials. If the gateway is slow, unreachable or failing, calls go direct automatically, and they still show up in your costs.
What happens on each call
- Routing. You choose, per feature, which share of calls goes through the gateway (dashboard: SDK routing). The SDK caches that setting and refreshes it every minute in the background, so a change reaches running apps within a minute, without a redeploy. Until the SDK has a setting, and for features at 0%, calls go direct.
- Acceptance within 1.5 seconds. The gateway confirms each request as soon as your key and budget check out (well before the model starts answering). No confirmation within 1.5 s, a connection error or a 5xx: the SDK sends the same request directly to Bedrock.
- Circuit breaker. After a timeout, or 5 errors within 30 seconds, every call goes direct with no added delay. During an outage only the first call waits the 1.5 s. The SDK checks the gateway in the background every 30 seconds and returns to it when it's healthy.
- Deliberate decisions are respected. An invalid key, an exceeded budget or a blocked account come back to your code as the usual API error (4xx). The SDK never bypasses them.
- No silent retries mid-stream. Once the answer has started streaming, a failure is raised to your code instead of retried, so you never pay for the same tokens twice. If the gateway goes silent for 60 seconds after accepting a call (
streamIdleTimeoutMs/stream_idle_timeout_s), the SDK stops waiting: before any output the call goes direct, after it you getGatewayStreamIdleTimeout. A long answer that keeps arriving never times out. - Direct calls are measured. Each direct call sends a small OpenTelemetry span with the model, tokens and your attribution (no prompt or answer content) to Open Token. The export runs in the background with a bounded queue: if we're unreachable, spans are dropped, never your requests.
1. Install
pip install "getopentoken[openai,bedrock]" # Python 3.9+
npm install @getopentoken/sdk # Node.js 18.17+2. Set your environment
Coming soon The opentoken SDK package is on its way. Placeholder values are marked in capitals; this snippet will fill in your real values once it ships.
export OT_API_KEY="ot-..." # gateway key (API keys)
export OT_INGEST_KEY="ot-..." # ingest-scope key, so direct calls are measured
export AWS_REGION="us-east-1" # direct calls use your default AWS credential chainWithout OT_INGEST_KEY the SDK still fails open, but direct calls won't appear in your costs.
3. Wire your framework
OpenAI SDK
Coming soon The opentoken SDK package is on its way. Placeholder values are marked in capitals; this snippet will fill in your real values once it ships.
import opentoken
opentoken.init()
client = opentoken.openai_client() # a regular openai.OpenAI; async_openai_client() for asyncio
with opentoken.context(feature="onboarding", customer="acme"):
reply = client.chat.completions.create(
model="amazon/nova-pro",
messages=[{"role": "user", "content": "Summarize this ticket"}],
)LangChain and LangGraph
Coming soon The opentoken SDK package is on its way. Placeholder values are marked in capitals; this snippet will fill in your real values once it ships.
from opentoken.adapters.langchain import chat_model
llm = chat_model("anthropic/claude-sonnet-4-6") # a ChatOpenAI: use it in chains and LangGraph nodesLlamaIndex
Coming soon The opentoken SDK package is on its way. Placeholder values are marked in capitals; this snippet will fill in your real values once it ships.
from opentoken.adapters.llamaindex import llm
model = llm("amazon/nova-pro")Strands Agents
Coming soon The opentoken SDK package is on its way. Placeholder values are marked in capitals; this snippet will fill in your real values once it ships.
from strands import Agent
from opentoken.adapters.strands import bedrock_model, openai_model
agent = Agent(model=bedrock_model("us.anthropic.claude-sonnet-4-6")) # or openai_model("anthropic/claude-sonnet-4-6")Vercel AI SDK
Coming soon The opentoken SDK package is on its way. Placeholder values are marked in capitals; this snippet will fill in your real values once it ships.
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
import { generateText } from "ai";
import { vercelAIOptions } from "@getopentoken/sdk";
const opentoken = createOpenAICompatible(vercelAIOptions());
const { text } = await generateText({ model: opentoken("amazon/nova-pro"), prompt: "Hello" });boto3 and the AWS SDK (Converse)
Coming soon The opentoken SDK package is on its way. Placeholder values are marked in capitals; this snippet will fill in your real values once it ships.
import boto3, opentoken
bedrock = opentoken.wrap_bedrock(boto3.client("bedrock-runtime"))
bedrock.converse(modelId="us.amazon.nova-pro-v1:0", messages=[{"role": "user", "content": [{"text": "Hello"}]}])Only converse and converse_stream are routed. Requests the gateway can't take yet (images, documents, guardrails, extra model fields) go direct with your original arguments and are still measured.
4. Roll out one feature at a time
Under SDK routing, add the feature and raise its gateway share gradually: 1% → 5% → 25% → 100%. Setting it back to 0% takes it off the gateway within a minute. Calls of the same agent run (same run_id) always take the same path.
Tag calls with opentoken.context(feature=..., customer=..., run_id=...) (Python) or withContext({ feature, customer, runId }, fn) (JavaScript). An X-OT-Feature header set on the request works too.
Shadow gateway (dry run)
Before any traffic goes through the gateway, set a dry-run sample (1–10% is plenty). For those calls the SDK sends only metadata (model, estimated prompt size, max tokens, tool count, feature, customer, run id) in the background to POST /v1/decisions/dry-run. The gateway evaluates your keys, limits and budgets without calling the model, and SDK routing shows what it would have blocked and how long the check took. Your calls never wait for it, and the model is never called twice.
Short-lived processes
In AWS Lambda or scripts, call opentoken.shutdown() (Python) or await shutdown() (JavaScript) before returning, so pending spans are sent.
Troubleshooting
| Symptom | Cause |
|---|---|
| Every call goes direct | No routing set for the feature (0% is the default), or the breaker is open: check the gateway status and your network path to it. |
| Direct calls missing from costs | OT_INGEST_KEY not set, or it isn't an ingest-scope key. |
No Bedrock model id known for … | The model isn't in the catalog: pass model_map={"your/model": "<bedrock model id>"} (modelMap in JavaScript). |
429 budget_exceeded | A budget you set is doing its job; the SDK deliberately doesn't bypass it. |