{"record":{"id":"ae0aeb83eb3fc5a6","repo":"BoundaryML/baml","slug":"failed-to-get-cached-input-tokens-w","errorCode":null,"errorMessage":"failed to get cached input tokens: %w","messagePattern":"failed to get cached input tokens: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"engine/language_client_go/pkg/rawobjects_usage.go","lineNumber":58,"sourceCode":"\nfunc (u *usage) OutputTokens() (int64, error) {\n\tresult, err := raw_objects.CallMethod(u, \"output_tokens\", nil)\n\tif err != nil {\n\t\treturn 0, fmt.Errorf(\"failed to get output tokens: %w\", err)\n\t}\n\n\ttokens, ok := result.(int64)\n\tif !ok {\n\t\treturn 0, fmt.Errorf(\"unexpected type for output tokens: %T\", result)\n\t}\n\n\treturn tokens, nil\n}\n\nfunc (u *usage) CachedInputTokens() (int64, error) {\n\tresult, err := raw_objects.CallMethod(u, \"cached_input_tokens\", nil)\n\tif err != nil {\n\t\treturn 0, fmt.Errorf(\"failed to get cached input tokens: %w\", err)\n\t}\n\n\ttokens, ok := result.(int64)\n\tif !ok {\n\t\treturn 0, fmt.Errorf(\"unexpected type for cached input tokens: %T\", result)\n\t}\n\n\treturn tokens, nil\n}\n","sourceCodeStart":40,"sourceCodeEnd":68,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/language_client_go/pkg/rawobjects_usage.go#L40-L68","documentation":"CachedInputTokens() invokes 'cached_input_tokens' on the raw usage object; on failure it wraps the error with 'failed to get cached input tokens: %w'. Cached-token stats may not exist for all providers or call states.","triggerScenarios":"Requesting cached input tokens from a provider/model that doesn't report cache stats, or from an incomplete/failed call's usage object.","commonSituations":"Reading cache metrics for providers without prompt caching; non-OpenAI models where cached_input_tokens is unsupported; premature usage reads during streaming.","solutions":["Confirm the provider/model reports cached input tokens before reading","Treat this error as 'not available' and fall back gracefully (e.g. skip caching metrics)","Read usage only after call/stream completion","Check the wrapped root error for the actual cause"],"exampleFix":"// before\ncached, err := usage.CachedInputTokens()\nif err != nil { return err }\n// after\ncached, err := usage.CachedInputTokens()\nif err != nil {\n    cached = 0 // provider may not report cache stats\n}","handlingStrategy":"fallback","validationCode":"// no cheap pre-check; cached stats availability depends on provider/model","typeGuard":null,"tryCatchPattern":"cached, err := usage.CachedInputTokens()\nif err != nil {\n    cached = 0 // treat as unavailable\n}","preventionTips":["Assume cache metrics are optional across providers","Don't hard-fail aggregation on cached-token errors","Confirm model supports prompt caching before reading","Read usage only after completion"],"tags":["go","usage","cached-tokens"],"backgroundTag":"unexpected-api-response-shape","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}