llm:call_cost event with the model, token usage, and computed dollar cost.
Use this to log costs to your observability stack, trigger alerts when spend exceeds a threshold, or aggregate cost per workflow over time.
Setup
Cost events use the same hooks system as error hooks:- Create a hook file and import
onfrom@outputai/core/hooks. - Register a handler for
llm:call_cost. - Add the file path to
output.hookFilesinpackage.json.
src/llm_cost_hooks.js
When events fire
Anllm:call_cost 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 a single object:| Field | Type | Description |
|---|---|---|
workflowId | string | undefined | Temporal workflow execution ID. Present when the call runs inside a workflow. |
activityId | string | undefined | Temporal activity ID of the step that made the LLM call. Present when the call runs inside a workflow activity. |
modelId | string | Model identifier (e.g. claude-sonnet-4-20250514, gpt-4o). From the prompt file config. |
usage | object | Token usage for this call. See Usage shape. |
cost | object | Computed dollar cost for this call. See Cost structure. |
Usage shape
usage follows the AI SDK’s totalUsage shape, aggregated across all steps for that call:
reasoningTokens and outputTokenDetails.reasoningTokens are set. Cached prompt tokens are reported in cachedInputTokens and inputTokenDetails.cacheReadTokens.
Cost structure
Cost is computed from the model’s per-million-token pricing, fetched from a built-in pricing source and cached for 24 hours. The formula is(tokens / 1_000_000) * pricePerMillion for each component. Non-cached input uses inputTokens - (cachedInputTokens ?? 0).
| Field | Type | Description |
|---|---|---|
total | number | null | Total estimated cost in dollars. null if pricing is unavailable for this model. |
message | string | Present when total is null — describes why cost could not be computed. |
components | object | Present when cost was calculated. Per-token-type breakdown. |
| Component | Description |
|---|---|
input | Cost for non-cached input (prompt) tokens. |
cachedInput | Cost for cached input tokens (prompt cache read). |
output | Cost for output (completion) tokens. |
reasoning | Cost for reasoning/thinking tokens, when the provider exposes them. Omitted when the provider does not separate reasoning from output. |
{ value: number } or { value: null, message: string } when that component’s pricing is unavailable. When a component has value: null, total still sums the components that have numeric values.
Successful calculation: