BerriAI/litellm · error · ValueError
Error counting field
Error message
Error counting field '{field_name}': {e} What it means
Token counting error wrapper: while counting tokens for a named field of an Anthropic tool_use/tool_result block, an exception occurred; it is wrapped with the field name so callers can locate the offending part of the message instead of getting a bare failure.
Solutions
- Fix the malformed field named in the error; its token counting failed.
- Validate the message content types before counting tokens.
Defensive patterns
Strategy: try-catch
When it happens
Trigger: Thrown at litellm/litellm_core_utils/token_counter.py:681 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18).
Data as JSON: /api/errors/fca3eea2737b5c1a.
Report an issue: GitHub.
Appendix: source
Thrown at litellm/litellm_core_utils/token_counter.py:681
field_value = content.get(field_name)
if field_value is None:
continue
try:
if isinstance(field_value, str):
tokens += count_function(field_value)
elif isinstance(field_value, list):
tokens += _count_content_list(
count_function,
field_value,
use_default_image_token_count,
default_token_count,
)
elif isinstance(field_value, dict):
tokens += count_function(str(field_value))
except Exception as e:
if default_token_count is not None:
return default_token_count
raise ValueError(f"Error counting field '{field_name}': {e}")
return tokens
def _count_content_list(
count_function: TokenCounterFunction,
content_list: OpenAIMessageContent,
use_default_image_token_count: bool,
default_token_count: int | None,
) -> int:
"""
Recursively count tokens from a list of content blocks.
"""
try:
num_tokens = 0
for c in content_list:
if isinstance(c, str):
num_tokens += count_function(c)
elif c["type"] == "text":View on GitHub (pinned to 77b7c6c40c)