Skip to content

[BUG] Strands SDK + Anthropic Provider Incorrect Token Estimates #111

@tylersalminen

Description

@tylersalminen

Description

AnthropicTokenCounter._count_message_estimated does not properly count message content blocks generated by Strands SDK.

To Reproduce

  1. Wrap bedrock model with HeadroomStrandsModel configured with some transformer that is dependent on exceeding a model_limit threshold and an Anthropic model
  2. Generate Strands agent from model
  3. invoke agent with block content
  4. any transformer that depends on model_limit threshold being exceeded never applies it's transforms due to Anthropic tokenizer never including block contents in token estimate

Expected Behavior

Properly estimates tokens

Actual Behavior

Tokenizer does not account for messages not containing a "type" property leading to not counting message contents in token total.

Code Sample

from os import environ

from strands.models import BedrockModel
from strands.types.content import ContentBlock
from strands.agent import Agent
from headroom import HeadroomConfig
from headroom.config import IntelligentContextConfig
from headroom.integrations.strands import HeadroomStrandsModel
from json import dumps


environ["HEADROOM_MODEL_LIMITS"] = dumps(
	{
		"anthropic": {
			"context_limits": {
				"us.anthropic.claude-sonnet-4-6": 200000
			}
		}
	}
)

async def _test():

	model = HeadroomStrandsModel(
		config=HeadroomConfig(
			intelligent_context=IntelligentContextConfig(
				enabled=True
			)
		),
		wrapped_model=BedrockModel(
			model_id="us.anthropic.claude-sonnet-4-6"
		)
	)

	agent = Agent(
		model=model
	)

	await agent.invoke_async(
		[
			ContentBlock(
				text="Some question" * 1000
			)
		]
	)

	# IntelligentContextConfig is never utilized b/c
	# token count never includes content / system blocks

Environment

  • Headroom version: 0.5.2
  • Python version: v3.12.10
  • OS: Windows 11
  • LLM Provider: Anthropic

Additional Context

Example Strands SDK message block:

{'role': 'user', 'content': [{'text': 'test'}]}

See in debugger image below where will not possibly ever include message content blocks in token count due to type property not existing.
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions