Skip to content

@effect/ai-amazon-bedrock: reasoningTokens never populated in GenerateTextResponse usage #6186

Closed

Description

@Chaoran-Huang

What version of Effect is running?

  • @effect/ai: ^0.33.0
  • @effect/ai-amazon-bedrock: ^0.13.0

Description

When extended thinking is enabled via additionalModelRequestFields.thinking, the Bedrock Converse API returns reasoning content in output.message.content (as reasoningContent blocks), and the @effect/ai-amazon-bedrock provider correctly extracts this into ReasoningPart objects (accessible via response.reasoningText).

However, response.usage.reasoningTokens is always undefined. The @effect/ai library defines reasoningTokens in Response.Usage (line 154 of LanguageModel.ts):

reasoningTokens: Schema.optionalWith(Schema.Number, { as: "Option" })

But the Bedrock provider's makeResponse function never maps a value for it:

// AmazonBedrockLanguageModel.ts makeResponse (lines 607-622)
usage: {
  inputTokens: raw.usage!.inputTokens!,
  outputTokens: raw.usage!.outputTokens!,
  cacheCreationInputTokens: (raw.usage as any).cacheCreationInputTokens ?? 0,
  cacheReadInputTokens: (raw.usage as any).cacheReadInputTokens ?? 0,
  // reasoningTokens is never set
},

The Bedrock Converse API does not return reasoning tokens as a separate field in its usage metrics — reasoning tokens are bundled into outputTokens. The Anthropic direct API does return them separately (as cache_creation_input_tokens and cache_read_input_tokens for caching, and model-specific fields for thinking).

Root Cause

Two related issues:

  1. Bedrock Converse API limitation: The Converse API's TokenUsage response only includes inputTokens, outputTokens, totalTokens, cacheReadInputTokens, and cacheWriteInputTokens. There is no reasoningTokens or thinkingTokens field. Reasoning tokens are included in outputTokens.

  2. Provider gap: The @effect/ai-amazon-bedrock provider doesn't attempt to derive reasoning tokens. A possible heuristic would be to count the tokens in the returned reasoningContent blocks, but this would require a tokenizer and wouldn't be precise.

Reproduction

import { Chat } from "@effect/ai"
import { AmazonBedrockLanguageModel } from "@effect/ai-amazon-bedrock"
import { Effect, Option } from "effect"

const program = Effect.gen(function* () {
  const chat = yield* Chat.fromPrompt([{ role: "system", content: "Think carefully." }])

  const response = yield* chat.generateText({
    prompt: "What is 2+2? Show your reasoning.",
  }).pipe(
    AmazonBedrockLanguageModel.withConfigOverride({
      additionalModelRequestFields: {
        thinking: { type: "enabled", budget_tokens: 4096 },
      },
    })
  )

  console.log("reasoningText:", response.reasoningText)
  // => "Let me think about this..." (populated correctly)

  console.log("reasoningTokens:", Option.getOrUndefined(response.usage.reasoningTokens))
  // => undefined (never populated)
})

Expected Behavior

Either:

  1. Populate reasoningTokens by deriving it from the Bedrock response (e.g., if the direct Anthropic API provides it via the x-amzn-bedrock-* headers or if Bedrock adds the field in the future), or
  2. Document that reasoningTokens is not available when using the Bedrock Converse API and that reasoning tokens are included in outputTokens, so consumers know not to rely on it for cost calculations.

Note: The Anthropic direct API (@effect/ai-anthropic) may have access to this data and could serve as a reference implementation.

Activity

  1. Chaoran-Huang commented on Jun 17, 2026

    @Chaoran-Huang
    ContributorAuthor

    Quick follow-up / correction on my own issue after going back through current main.

    The premise here is actually wrong: the Anthropic direct API doesn't break out reasoning tokens either, so it can't serve as a reference. Their Usage/BetaUsage schema (generated from Anthropic's OpenAPI spec) only has input_tokens, output_tokens, the cache fields and server_tool_use, and the docs are explicit that output_tokens already includes the thinking tokens. That's the same situation as Bedrock's TokenUsage. OpenAI is the only provider that reports it separately (output_tokens_details.reasoning_tokens), which is why it's the only one that fills in the field today.

    So populating/deriving reasoningTokens for Bedrock (or Anthropic) isn't really feasible — counting tokens off the reasoning blocks would just be a guess. I'm going with the documentation route instead.

    Opened #6278 to document this on Response.Usage.reasoningTokens and at the usage-mapping sites in both the Anthropic and Bedrock providers, so it's clear the field is intentionally unset for these providers and shouldn't be used for cost math.

  2. fubhy commented on Jul 9, 2026

    @fubhy
    Member

    Closing based on the reporter's follow-up: Bedrock Converse does not expose separate reasoning-token usage, so reasoningTokens cannot be populated accurately. Documentation clarification is tracked in #6278.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions