Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/clarify-reasoning-tokens-usage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@effect/ai": patch
"@effect/ai-anthropic": patch
"@effect/ai-amazon-bedrock": patch
---

Clarify that `Response.Usage.reasoningTokens` is only populated by providers whose API reports reasoning tokens as a discrete value (e.g. OpenAI via `output_tokens_details.reasoning_tokens`).

The Anthropic Messages API and the Amazon Bedrock Converse API do **not** report reasoning tokens separately - when extended thinking is enabled, reasoning tokens are already included in `outputTokens`. The `@effect/ai-anthropic` and `@effect/ai-amazon-bedrock` providers therefore leave `reasoningTokens` unset. This documents the behavior so consumers do not rely on the field for cost calculations with these providers. No runtime behavior changes.
8 changes: 8 additions & 0 deletions packages/ai/ai/src/Response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2214,6 +2214,14 @@ export class Usage extends Schema.Class<Usage>("@effect/ai/AiResponse/Usage")({
/**
* The number of reasoning tokens that the model used to generate the output
* for the request.
*
* **NOTE**: This value is only populated by providers whose API reports
* reasoning tokens as a discrete value (e.g. OpenAI via
* `output_tokens_details.reasoning_tokens`). Providers such as Anthropic and
* Amazon Bedrock do not report reasoning tokens separately - for those
* providers reasoning tokens are already included in `outputTokens`, and this
* field will be left unset. Do not rely on this field for cost calculations
* unless your provider is known to report it.
*/
reasoningTokens: Schema.optional(Schema.Number),
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,10 @@ const makeResponse: (
parts.push({
type: "finish",
reason: finishReason,
// NOTE: `reasoningTokens` is intentionally left unset. The Bedrock Converse
// API does not report reasoning tokens as a discrete value - when extended
// thinking is enabled, reasoning tokens are already included in
// `outputTokens`. See the `TokenUsage` schema in `AmazonBedrockSchema.ts`.
usage: {
inputTokens: response.usage.inputTokens,
outputTokens: response.usage.outputTokens,
Expand Down Expand Up @@ -657,6 +661,10 @@ const makeStreamResponse: (
let trace: ConverseTrace | undefined = undefined
let cacheWriteInputTokens: number | undefined = undefined
let finishReason: Response.FinishReason | undefined = undefined
// NOTE: `reasoningTokens` is intentionally omitted. The Bedrock Converse API
// does not report reasoning tokens as a discrete value - when extended
// thinking is enabled, reasoning tokens are already included in
// `outputTokens`. See the `TokenUsage` schema in `AmazonBedrockSchema.ts`.
const usage: Mutable<typeof Response.Usage.Encoded> = {
inputTokens: undefined,
outputTokens: undefined,
Expand Down
8 changes: 8 additions & 0 deletions packages/ai/anthropic/src/AnthropicLanguageModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,10 @@ const makeResponse: (
parts.push({
type: "finish",
reason: finishReason,
// NOTE: `reasoningTokens` is intentionally left unset. The Anthropic
// Messages API does not report reasoning tokens as a discrete value -
// when extended thinking is enabled, reasoning tokens are already
// included in `output_tokens`. See the `Usage` schema in `Generated.ts`.
usage: {
inputTokens: response.usage.input_tokens,
outputTokens: response.usage.output_tokens,
Expand Down Expand Up @@ -951,6 +955,10 @@ const makeStreamResponse: (
| "mcp_tool_result"
| "container_upload"
| undefined = undefined
// NOTE: `reasoningTokens` is intentionally omitted. The Anthropic Messages
// API does not report reasoning tokens as a discrete value - when extended
// thinking is enabled, reasoning tokens are already included in
// `output_tokens`. See the `Usage` schema in `Generated.ts`.
const usage: Mutable<typeof Response.Usage.Encoded> = {
inputTokens: undefined,
outputTokens: undefined,
Expand Down
Loading