Skip to main content
Bitfab records chat completions your code sends to OpenRouter through the official openai client: the prompt, the model OpenRouter routed to, token counts, latency, and the cost OpenRouter reports for that call. Canonical signatures: TypeScript wrapOpenRouter · Python wrap_openrouter

Language Support

Under a trace root

Beneath a trace root, every chat.completions.create call on an openai client whose base URL is openrouter.ai is recorded with no wrapper, as its own llm span named Completions.create (Python’s async client: AsyncCompletions.create). chat.completions.parse (Completions.parse), streamed calls, and embeddings.create (Embeddings.create) are recorded the same way. A call on any other base URL, such as OpenAI itself, is recorded with provider "openai"; see OpenAI API. The span attaches under the function that made the call.
The trace shows detect, then classify, then an llm span named Completions.create carrying the prompt and cost. In TypeScript, the build transform rewrites each .chat.completions.create(...) call site in your code, so it works in bundled apps and the SDK never loads or patches openai. The client’s own APIPromise is handed back, so .withResponse() and .asResponse() keep working. In Python, the SDK patches the openai completions classes when a trace() root is set up. Calls made through with_raw_response (as Braintrust’s wrap_openai and Langfuse do) are recorded with their usage too, and the response’s .parse() still returns the same object. exclude on the root matches the method name ("create", "parse") or the span name, and withNode() / node() on a wrapper (below) configures the one span.

Streaming

A streamed call (stream: true / stream=True, or chat.completions.stream(), which is recorded as its Completions.create call) is recorded as one llm span when the stream finishes, is closed, or fails, with usage read from the last chunk. Usage arrives on a stream only when the request sets stream_options with include_usage. A stream returned from the trace root keeps the trace open until your code reads it. A stream your code drops without reading to the end is recorded when the runtime frees it, with the request and whatever was read but usually no usage, and its trace finishes then. Streamed calls always run for real during a replay, and the wrappers never record a stream. A withNode() / node() placed directly on the client method records the stream on its own span instead of opening a second one. That span ends when the stream does, and the node’s finalize is not used.

Wrapping a call

Use wrapOpenRouter() / wrap_openrouter() when you trace with withSpan() / span() instead of a trace root, or when your client reaches OpenRouter through a proxy whose host is not openrouter.ai. It wraps the function that sends the request and writes the prompt, usage, and cost into the span that is current when the call starts. It opens no span of its own.
Beneath a trace root, a wrapper around a call that is already recorded as Completions.create records nothing more. onResponse / on_response receives each completion after it is recorded.

No duplicate spans

Each model call is recorded once:
  • A wrapper around a call the trace root already recorded adds nothing.
  • A call the OpenAI Agents SDK integration is recording as a generation or response span, or the LangChain callback handler is recording as an LLM run, is not recorded a second time. This includes a call made inside your own LangChain chat model class or inside your own Agents model class that opens a generation span. A call made alongside a LangChain run in the same trace is still recorded, except beside the first LangChain run in a TypeScript process (see Limitations).
  • In TypeScript, calls made inside framework libraries in node_modules are never rewritten by the build transform, so only the framework’s own integration records them. In Python, the SDK patches the client classes, so a call another library makes through the client is recorded too, unless that library has a Bitfab integration recording it.

What gets captured

The context fields go in one context entry on the span. A key the response does not carry is left out. An embeddings.create call records the same fields except outputTokens, which is always 0, and the cached and reasoning token counts, which embeddings do not have.

Limitations

  • Calls outside any trace are not recorded. A wrapper used outside a span or trace records nothing and never starts a trace of its own.
  • TypeScript automatic capture needs the build transform. Code the transform does not compile (for example a call made from a dependency) is not rewritten; wrap it instead.
  • TypeScript: calls beside the first LangChain run in a process. The SDK hooks into LangChain in the background when the first Bitfab LangChain handler or withTrace() root is created. If a LangChain run starts before that finishes, calls made inside it are still recorded once, but a call made at the same time outside it (for example in Promise.all) is not recorded while that first run is open. Later runs are not affected.
  • Python automatic capture needs Python 3.12+, the same as trace() subtree capture.
  • Only chat completions and embeddings. Wrap chat.completions.parse with wrapOpenRouter / wrap_openrouter. To wrap embeddings, use wrap_openai_embeddings in Python, which reports "openrouter" for an OpenRouter client. In TypeScript, wrapOpenAIEmbeddings and wrapOpenAIChat cannot see the client’s base URL and always report "openai", so record OpenRouter embeddings under a trace root instead (OpenAI API). Responses API calls are recorded by the OpenAI Responses API integration, with provider openai.