> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bitfab.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenAI Responses API

> Record prompts, models, tokens, and cost for direct OpenAI Responses API calls

Bitfab records requests your code sends to the [OpenAI Responses API](https://platform.openai.com/docs/api-reference/responses) through the official `openai` client: the prompt, the model, token counts, and latency. Bitfab works out the cost from the model and the token counts, or keeps the cost the response reports when it carries one.

**Canonical signatures:** [TypeScript `wrapOpenAIResponses`](/reference/typescript#wrapopenairesponses) · [Python `wrap_openai_responses`](/reference/python#wrap-openai-responses)

## Language Support

| Language | API | Status |
| - | - | - |
| TypeScript | `wrapOpenAIResponses()`, plus automatic capture under `withTrace()` with the build transform | ✅ Supported |
| Python | `wrap_openai_responses()`, plus automatic capture under `trace()` | ✅ Supported |

## Under a trace root

Beneath a trace root, every non-streaming `responses.create` call on an `openai` client is recorded with no wrapper, whatever its base URL, as its own `llm` span named `Responses.create` (Python's async client: `AsyncResponses.create`). The span attaches under the function that made the call.

```typescript TypeScript theme={null}
import OpenAI from "openai"
import { Bitfab } from "@bitfab/sdk"

const bitfab = new Bitfab({ apiKey: process.env.BITFAB_API_KEY })
const llm = new OpenAI()

async function classify(email: string) {
  const response = await llm.responses.create({
    model: "gpt-5",
    instructions: "You classify email.",
    input: email,
  })
  return response.output_text
}

export const detect = bitfab.withTrace("inbox-detection", (email: string) =>
  classify(email),
)
```

```python Python theme={null}
from openai import OpenAI
from bitfab import Bitfab

bitfab = Bitfab()
llm = OpenAI()


def classify(email: str) -> str:
    response = llm.responses.create(
        model="gpt-5",
        instructions="You classify email.",
        input=email,
    )
    return response.output_text


@bitfab.trace("inbox-detection", type="llm")
def detect(email: str) -> str:
    return classify(email)
```

The trace shows `detect`, then `classify`, then an `llm` span named `Responses.create` carrying the prompt, tokens, and cost.

In TypeScript, the [build transform](/typescript-sdk#experimental-subtree-tracing) rewrites each `.responses.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()` keeps working. In Python, the SDK patches the `openai` responses classes when a `trace()` root is set up.

`exclude: ["create"]` on the root skips these calls, and `withNode()` / `node()` on a wrapper (below) configures the one span.

## Wrapping a call

Use `wrapOpenAIResponses()` / `wrap_openai_responses()` when you trace with `withSpan()` / `span()` instead of a trace root. It wraps the function that sends the request and writes the prompt and usage into the span that is current when the call starts. It opens no span of its own.

```typescript TypeScript theme={null}
const respond = bitfab.wrapOpenAIResponses(llm.responses.create.bind(llm.responses))

const classify = bitfab.withSpan(
  "classify-email",
  { type: "llm" },
  async (email: string) => {
    const response = await respond({ model: "gpt-5", input: email })
    return response.output_text
  },
)
```

```python Python theme={null}
respond = bitfab.wrap_openai_responses(llm.responses.create)


@bitfab.span("classify-email", type="llm")
def classify(email: str) -> str:
    return respond(model="gpt-5", input=email).output_text
```

Beneath a trace root, a wrapper around a call that is already recorded as `Responses.create` records nothing more. `onResponse` / `on_response` receives each response 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](/frameworks/openai-agents) is recording as a generation or response span, or the [LangChain callback handler](/frameworks/langgraph) 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](#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. The built-in `OpenAIResponsesModel` opens a response span, so its calls are left to that integration.

A custom OpenAI Agents SDK model class that calls `responses.create` directly opens no generation or response span, so its calls are recorded as `Responses.create` spans under the agent run.

## What gets captured

| Field | Value |
| - | - |
| Prompt | The request's `instructions` as a `system` turn, then its `input` (a string becomes one `user` turn), as a JSON list of `role` and `content` |
| `provider` | `"openai"` |
| `model` | The model from the response |
| `inputTokens` | `usage.input_tokens` |
| `outputTokens` | `usage.output_tokens` |
| `cachedInputTokens` | `usage.input_tokens_details.cached_tokens` |
| `reasoningTokens` | `usage.output_tokens_details.reasoning_tokens` |
| `costUsd` | `usage.cost`, only when the response reports one |
| `durationMs` | How long the request took |

The context fields go in one context entry on the span. A key the response does not carry is left out. Without `costUsd`, Bitfab works out the cost from `model` and the token counts.

## Limitations

* **Streaming calls are not recorded.** A call with `stream: true` / `stream=True` runs normally and records nothing.
* **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 `responses.create`.** The `responses.parse` and `responses.stream` helpers are not recorded. For chat completions sent to OpenRouter, see [OpenRouter](/frameworks/openrouter).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.