Why Does Claude count_tokens Reject a File ID?
Claude’s token counter rejects an image passed as a file_id. That image reference can be valid for message creation. For the count, pass the original image bytes as inline Base64. If you already made that change, check the final JSON: in our Python SDK test, stale extra_body settings silently put the file reference back.
What left the Python SDK?
We captured the request bodies from Anthropic’s Python SDK 2.6.0 using a local mock transport. The mock recorded the outgoing JSON and ended the call. No Claude service received a request.
| Call | Input supplied | Image source in the request |
|---|---|---|
messages.create | File reference | file, same ID |
messages.count_tokens | Same file reference | file, same ID |
messages.count_tokens | Inline image bytes | base64, exact bytes preserved |
messages.count_tokens | Inline image plus stale extra_body.messages | file, stale ID restored |
The SDK forwarded the file reference to either endpoint without looking up the file. When we supplied Base64, the captured data decoded to the same bytes we supplied. In the last case, adding extra_body.messages was the only change. The final JSON carried that stale list in place of the inline one.
What to check before treating this as a Claude usage limit
For this input, the documented response is invalid_request_error. An API rate-limit error has its own error type. Save the exact endpoint and error type, then check source.type in the outgoing count request. If it still says file after the conversion, remove the stale messages override and inspect the resulting body again.
Download the local check or read the captured requests. The capture measures request construction. It contains no token count, quota reading, or service acceptance test.