The opentoken SDKs do two things, and you can use either or both:
- Observe: trace every LLM call, tool call and agent step with tokens and cost. Your requests keep going straight to Amazon Bedrock in your account; Open Token only receives spans in the background.
- Govern: route calls through the gateway for budgets and per-run limits, with fail-open: if we're slow or down, the call goes straight to Bedrock and is still measured.
Framework guides: Strands Agents, LangGraph and LangChain, LlamaIndex, Amazon Bedrock and AgentCore, OpenAI SDK, Vercel AI SDK.
To instrument your client directly, see Trace LLM calls with OpenTelemetry in Node.js.
1. Install
pip install "getopentoken[all]"2. Create an ingest key and set the environment
Under API keys, create a key with the Ingest scope. It can only send telemetry, never make model calls. Put it in your secret store as OT_INGEST_KEY, then:
# OT_INGEST_KEY: an ingest key from API keys (scope: Ingest). Keep it in your secret store.
export OTEL_EXPORTER_OTLP_ENDPOINT="https://getopentoken.com/api/otel"
export OTEL_SERVICE_NAME="support-agent"3. Python: one line
import opentoken
opentoken.init() # reads OT_INGEST_KEY and OTEL_EXPORTER_OTLP_ENDPOINT
with opentoken.context(feature="refunds", customer="acme"):
pass # every LLM call and agent step in here is traced and attributed to this feature and customer
print(opentoken.tracing())opentoken.init() sets up OpenTelemetry and instruments whatever is installed:
| Framework | How it's traced |
|---|---|
| OpenAI SDK, LangChain and LangGraph, LlamaIndex, boto3 (Bedrock Converse and InvokeModel) | Auto-instrumentation (OpenLLMetry, Apache-2.0), included in the extras |
| Strands Agents, Bedrock AgentCore | Native OpenTelemetry GenAI spans, sent to Open Token |
If your app already configured OpenTelemetry (your own provider, ADOT, StrandsTelemetry), init() adds Open Token to it instead of replacing it. Limit what's instrumented with instrument=["openai", "langchain"] or OT_INSTRUMENT.
opentoken.context(feature=..., customer=..., run_id=...) attributes every span inside the block, so costs break down by feature and end customer.
Without changing code
Start your app through the launcher. It applies opentoken.init() at interpreter startup, like opentelemetry-instrument:
opentoken-instrument --service-name support-agent python app.pyNode.js
Preload the SDK so it's set up before your modules load (this is what instruments ESM import OpenAI from "openai"):
node --import @getopentoken/sdk/register app.mjsOr call init() yourself as early as possible. With a bundler, or an ESM app started without --import, pass the modules to patch: init({ instrumentModules: { openai: OpenAI } }). If you build your own tracer provider (for example registerOTel from @vercel/otel), pass it spanProcessors: await spanProcessors() and call init() after registering it.
Send the same traces to Langfuse
Keep your Langfuse (or any OTLP backend) and add Open Token next to it: one trace, both destinations.
import opentoken
# Also sends every trace to Langfuse, read from LANGFUSE_PUBLIC_KEY, LANGFUSE_SECRET_KEY and LANGFUSE_HOST.
# Same as OT_ALSO_EXPORT=langfuse. Any OTLP/HTTP backend works: also_export=["https://collector:4318"].
opentoken.init(also_export=["langfuse"])
print(opentoken.tracing().targets)With the environment only: OT_ALSO_EXPORT=langfuse plus Langfuse's usual LANGFUSE_PUBLIC_KEY, LANGFUSE_SECRET_KEY and LANGFUSE_HOST. For another collector: OT_ALSO_EXPORT=otlp with OT_ALSO_EXPORT_ENDPOINT and OT_ALSO_EXPORT_HEADERS.
Record tools, parameters and agent roles (opt-in)
To evaluate or replay an agent later, its traces need more than tokens: the request parameters, the tools it could call and what each tool call did. Turn it on with init({ captureReplay: true }) in Node, opentoken.init(capture_replay=True) in Python, or OT_CAPTURE_REPLAY=1. It's off by default, so upgrading the SDK changes nothing until you enable it.
- Request parameters on the SDK's own model spans:
gen_ai.request.temperature,top_p,max_tokens,seed,stop_sequences,frequency_penalty,presence_penalty, plusot.request.tool_choice,ot.request.response_format(the type only) andgen_ai.output.type. When the request offers tools you wrapped,ot.tool.effectslists their declared effects (lookup_order:read). - Agent and role:
withContext({ agentName, agentRole })in Node,opentoken.context(agent_name=..., agent_role=...)in Python. Every span in the block getsgen_ai.agent.name(unless the framework already named its agent) andot.agent.role, for exampleplanner,tool-callerorsummarizer. - Tool calls: wrap each tool with
traceTool(fn, { effect: "read" })in Node, or decorate it with@opentoken.trace_tool(effect="write")in Python. Each call becomes anexecute_toolspan withgen_ai.tool.name, its duration and error, andot.tool.effect. Declarereadfor tools that only look things up andwritefor tools that change something. Open Token keeps the effect with each tool call and tool definition, even without content, so Evals can tell which steps had side effects. PasscallId(call_id) to record the model's tool call id asgen_ai.tool.call.id.
Tool definitions (gen_ai.tool.definitions), arguments (gen_ai.tool.call.arguments) and results (gen_ai.tool.call.result) are content: they're only recorded when OT_CAPTURE_CONTENT=1 is also set, and only stored if the project captures content. The project's hidden fields and masking apply to them on the server, as for any other content. The wrappers never change what your tool returns or throws, and a tracing failure never reaches your agent.
Upgrading an existing integration
Nothing changes until you opt in, and your current spans keep their shape. To adopt it:
- Upgrade the SDK and set
OT_CAPTURE_REPLAY=1(or theinitoption). Parameters and roles start arriving as metadata. - Wrap your tools with
traceToolor@opentoken.trace_tool, declaring each one's effect. A tool you don't wrap still works; it just has noexecute_toolspan. - Set the agent and role where your code switches between agents. Framework spans that already name their agent keep that name.
- Add
OT_CAPTURE_CONTENT=1only if you want definitions, arguments and results recorded, and the project's capture settings allow it.
If you already send OpenTelemetry GenAI spans yourself, the same attribute names work without the SDK.
Guarantees
- Never in your way. Spans are exported in the background from a bounded queue with a 5-second timeout. If Open Token (or Langfuse) is slow or down, spans are dropped; your requests never wait and never fail because of telemetry. A misconfiguration is skipped, not raised.
- No prompts or responses by default. Framework instrumentation records sizes and tokens, not text. Set
OT_CAPTURE_CONTENT=1only if you want content in your traces. - No double counting. When the SDK routes a call through the gateway, the gateway measures it; when it falls back to Bedrock, the SDK's own span does. Either way the framework's span for that call is marked (
opentoken.usage_owner) and its tokens aren't counted again. Agent and chain spans that repeat their children's usage aren't counted either. - Short-lived processes (Lambda handlers, scripts): call
opentoken.shutdown()(Node:await shutdown()) before exiting to flush.
Settings
| Variable | Purpose |
|---|---|
OT_INGEST_KEY | Ingest key for traces |
OT_API_KEY | API key for the gateway (govern path only) |
OTEL_EXPORTER_OTLP_ENDPOINT or OT_OTLP_ENDPOINT | Where traces go (the second is the full /v1/traces URL) |
OTEL_SERVICE_NAME or OT_SERVICE_NAME | Service name on every trace |
OT_PROJECT, OT_ENVIRONMENT | Project and environment on every trace |
OT_INSTRUMENT | all (default), none, or a list: openai,langchain,llamaindex,bedrock |
OT_ALSO_EXPORT | Extra destinations: langfuse, otlp, or a URL |
OT_CAPTURE_CONTENT | 1 to include prompt and response text |
OT_CAPTURE_REPLAY | 1 to record request parameters, agent roles and tool calls (see above) |