{"record":{"id":"40af4d8b096b3a75","repo":"BerriAI/litellm","slug":"counttokens-processing-error-e-40af4d","errorCode":null,"errorMessage":"CountTokens processing error: {e}","messagePattern":"CountTokens processing error: (.+?)","errorType":"exception","errorClass":"AnthropicError","httpStatus":500,"severity":"error","filePath":"litellm/llms/azure_ai/anthropic/count_tokens/handler.py","lineNumber":124,"sourceCode":"\n            verbose_logger.debug(\"Azure AI Anthropic response: %s\", azure_response)\n\n            # Return Anthropic-compatible response directly - no transformation needed\n            return azure_response\n\n        except AnthropicError:\n            # Re-raise Anthropic exceptions as-is\n            raise\n        except httpx.HTTPStatusError as e:\n            # HTTP errors - preserve the actual status code\n            verbose_logger.error(\"HTTP error in CountTokens handler: %s\", e)\n            raise AnthropicError(\n                status_code=e.response.status_code,\n                message=e.response.text,\n            )\n        except Exception as e:\n            verbose_logger.error(\"Error in CountTokens handler: %s\", e)\n            raise AnthropicError(\n                status_code=500,\n                message=f\"CountTokens processing error: {e}\",\n            )\n","sourceCodeStart":106,"sourceCodeEnd":128,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/azure_ai/anthropic/count_tokens/handler.py#L106-L128","documentation":"This is the catch-all branch of the Azure Anthropic CountTokens passthrough handler: any exception that is not an AnthropicError and not an httpx.HTTPStatusError (those preserve the real status code) is wrapped in AnthropicError(500, 'CountTokens processing error: {e}'). It usually masks a bug or an unexpected client-side failure (JSON decode, connection, type error) rather than an Anthropic API rejection.","triggerScenarios":"POST to the azure-anthropic count_tokens route where the upstream returns a non-JSON body, the connection resets mid-response, or a code bug (e.g. unexpected response shape) raises; the original exception's message is embedded after the colon.","commonSituations":"Proxy in front of Azure Foundry returning HTML error pages (WAF, auth gateway); intermittent network drops; mismatch between deployed litellm version and Anthropic response schema after an API update.","solutions":["Read the text after 'CountTokens processing error:' — it is str(e) of the underlying exception and names the true fault.","Enable verbose logging (litellm.verbose = True / LITELLM_LOG=DEBUG) to see the full traceback logged just before the wrap.","If a gateway/WAF sits in front of Azure, whitelist the count_tokens path and check its logs for the same request.","Reproduce with a direct httpx call to the Azure anthropic endpoint to isolate whether litellm or the network is at fault; if litellm, upgrade — this path frequently fixes schema bugs."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":"def is_count_tokens_wrap(e: BaseException) -> bool:\n    return type(e).__name__ == 'AnthropicError' and str(e).startswith('CountTokens processing error')","tryCatchPattern":"try:\n    resp = count_tokens_via_azure(payload)\nexcept Exception as e:\n    if is_count_tokens_wrap(e):\n        logger.exception('underlying cause: %s', e)  # real exception is embedded\n        return None  # count-tokens is advisory; degrade gracefully\n    raise","preventionTips":["Treat token counting as best-effort: never hard-fail a chat request when count_tokens errors.","Keep litellm updated — this wrapper frequently hides schema bugs fixed in newer releases.","Log the embedded cause; it names the real fault (JSON decode, network, type)."],"tags":["azure","anthropic","count-tokens","passthrough","wrapped-exception"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}