Skip to main content
The cost estimation CLI shows you costs after a workflow finishes. Cost events expose similar data while a workflow runs:
  • cost:llm:request — emitted when an LLM call finishes (generateText / streamText), with an LLM usage attribute.
  • cost:http:request — emitted when you attach a dollar cost to an HTTP response with addRequestCost from @outputai/http, with an HTTP request cost attribute.
Use either or both to log spend to your observability stack, trigger alerts, or aggregate cost per workflow over time.

Setup

Cost events use the same hooks system as error hooks:
  1. Create a hook file and import on from @outputai/core/hooks.
  2. Register a handler for cost:llm:request, cost:http:request, or both.
  3. Add the file path to outputai.hookFiles in package.json.
See Error Hooks — Setup for the hook file registration pattern.
src/llm_cost_hooks.ts
src/http_cost_hooks.ts
Handler errors are caught and logged by the framework — they never affect the workflow or the request that triggered them.

LLM request cost

When events fire

An cost:llm:request event is emitted after every generateText and streamText call completes. For streaming, the event fires when the stream finishes, not when it starts.

Payload

The handler receives an event envelope. The LLM usage attribute is available under payload:

Usage entries

Cost is computed from the model’s per-million-token pricing, fetched from a built-in pricing source and cached for 24 hours. For each priced dimension, the dollar amount is (tokens / 1_000_000) * pricePerMillion. Non-cached input tokens use inputTokens - (cachedInputTokens ?? 0). payload.usage[].type values: Only available, finite usage dimensions are included. For example, reasoning is omitted when the model does not define separate reasoning pricing, and input_cached is omitted when there are no cached input tokens.

HTTP request cost

Events fire only when your code calls addRequestCost( response, total ) with a response object created by @outputai/http (or its exported fetch). The SDK attaches the cost to the existing HTTP trace event and emits cost:http:request. If the response did not originate from this package, addRequestCost no-ops (with a console warning) and no hook event is emitted.

Payload

The handler receives an event envelope. The HTTP request cost attribute is available under payload: