opencode icon indicating copy to clipboard operation
opencode copied to clipboard

Hitting rate limit of 100k input tokens but opencode says it's 73k

Open pinoniq opened this issue 1 year ago • 25 comments

Set up using Anthropic, using Claude 4 Sonnet with a high tier capped at 100k input tokens.

OpenCode is trying to make a unit-test work that has a total of 400 LOC's and testing a file with 370LOC's.

It's sending more then 100k Tokens, but the interface shows 73k?

Image

Is there a way to figure out why the input tokens are so high?

pinoniq avatar Jun 15 '25 11:06 pinoniq

I think it's the tokens/minute and not so much the tokens per request that I'm hitting. Which is weird cause I shouldn't have a limit any where near 100K. Will check with anthropic.

pinoniq avatar Jun 15 '25 11:06 pinoniq

Having said that. If I just upload the 2 files I'm no where near the token limit. Is there a way to see what opencode's input to claude is?

pinoniq avatar Jun 15 '25 11:06 pinoniq

are there others in your org using this same api key?

adamdotdevin avatar Jun 15 '25 12:06 adamdotdevin

I'm on a rate-limit of 40.000 tok/m, getting this when trying to edit 2 files that are about 2.000 tokens combined (calculated here). Positive I'm the only one on this account and using the api key. Can we check what is being sent to the api?

maxstevens-nl avatar Jun 15 '25 15:06 maxstevens-nl

I am on the Max 5x plan, and I have consistently hit the rate limit after a single prompt. And they haven't been asking to do anything crazy. Am I doing something wrong? Is there a way I can look up my exact usage to see how I'm using it so quickly?

On the other hand, claude code seems to perform normally and I don't hit the rate limits that fast

mhamza15 avatar Jun 15 '25 15:06 mhamza15

we must be sending something that's way too big in the system prompt

can you turn on OPENCODE_AUTO_SHARE=1 as an env var and then do a session where you hit the limit?

send me the link at the top after

thdxr avatar Jun 15 '25 16:06 thdxr

also the data for each session is in ~/.local/share/opencode/project/ - can lookup the messages there and they should have the system prompt used in the metadata

thdxr avatar Jun 15 '25 17:06 thdxr

we figured out the issue - we put a list of all files in the directory into the system prompt and under estimated how big this gets

reworking this to be more token efficient (it's not respecting git ignore etc) and also limiting how much gets put there

will fix tonight

thdxr avatar Jun 15 '25 19:06 thdxr

I'm trying out your project for the first time and recently purchased $10 worth of Claude credits. I attempted to use it with the API key on a Neovim plugin I'm experimenting with, which consists of a single file containing 160 lines of code. However, I'm encountering an issue where I consistently run into a rate limit, regardless of what I ask the plugin to do.

I'm unsure whether this is due to an error on my part or if it's a potential bug. Interestingly, when I directly post the entire file to Claude's chat, it processes everything smoothly, and I'm able to ask questions about it without any issues.

If anyone could provide guidance on this matter or clarify if I'm missing something, it would be greatly appreciated.

saljic avatar Jun 15 '25 19:06 saljic

@saljic can you do the above instructions for OPENCODE_AUTO_SHARE

thdxr avatar Jun 16 '25 00:06 thdxr

i just pushed some optimizations i v0.1.54 - let me know if that's better

i have more optimizations in mind but i'm wondering if i can stop here - there's some tradeoffs going further

thdxr avatar Jun 16 '25 01:06 thdxr

@saljic can you do the above instructions for OPENCODE_AUTO_SHARE

Here it is!

https://dev.opencode.ai/s/7bdef922

saljic avatar Jun 16 '25 05:06 saljic

@adamdottv I'm the only one using this API key.

@thdxr I think it's the test files. If I use the Claude Tokenizer, it gives me 10k tokens for all files in the directory currently working in.

If I include all the test files, thats 30k tokens. So it's 40k Tokens for all files.

And so if all these files are sent. if you are in Tier 3, Claude 4 sonnet hits the rate limit if you send over 2 requests per minute.

My test files are ~400-600 LOC's

I'm however also seeing this:

"toolu_01AveLXBFPeQHdaj3rq61w3t": {
        "error": true,
        "message": "Error: oldString not found in file. Make sure it matches exactly, including whitespace and line breaks",
        "title": "Error: oldString not found in file. Make sure it matches exactly, including whitespace and line breaks",
        "time": {
          "start": 1750057847822,
          "end": 1750057847824
        }
      },
      "toolu_011UntvqRxbrEziUAZGo1T31": {
        "error": true,
        "message": "Error: oldString not found in file. Make sure it matches exactly, including whitespace and line breaks",
        "title": "Error: oldString not found in file. Make sure it matches exactly, including whitespace and line breaks",
        "time": {
          "start": 1750057860275,
          "end": 1750057860276
        }
      },
      "toolu_01HQtSBFUhRuqSLw99pMhLSz": {
        "error": true,
        "message": "TypeError: undefined is not an object (evaluating 'B.parts.findLast((X)=>X.type===\"text\").text')",
        "title": "TypeError: undefined is not an object (evaluating 'B.parts.findLast((X)=>X.type===\"text\").text')",
        "time": {
          "start": 1750057876007,
          "end": 1750057882269
        }
      }

pinoniq avatar Jun 16 '25 07:06 pinoniq

Those errors seem to be related to CRLF vs LF. It's using \n in the oldString, but the file is in CRLF. Could these be triggering the multiple requests per minute hitting the rate limit since it keep retrying?

pinoniq avatar Jun 16 '25 07:06 pinoniq

Sent you a DM on discord with more info

pinoniq avatar Jun 16 '25 07:06 pinoniq

Just tried sending a simple edit prompt and got the error almost instantly ...

AI_RetryError: Failed after 3 attempts. Last error: This request would exceed the rate limit for your organization (02fef4c9-3f78-46e1-8640- 8a4a1d352400) of 20,000 input tokens per minute. For details, refer to: https://docs.anthropic.com/en/api/rate-limits. You can see the response headers for current usage. Please reduce the prompt length or the maximum tokens requested, or try again later. You may also contact sales at https://www.anthropic.com/contact-sales to discuss your options for a rate limit increase.

Also adding a share link: https://dev.opencode.ai/s/f14d598f

Certainly didn't happen on the pre 0.1.x versions - of that I'm certain.

@thdxr feel free to ask for any other logs and such... I'm seeing that opencode's hitting the Anthropics servers with 13k input tokens in rapid successions and only responding with around 800 tokens... (the test file is only 130 tokens as per this site mentioned earlier)

Image

Even the opencode convo says only 14k tokens (but even that is UNREASONABLY high for no good reason)

akuuly-7 avatar Jun 16 '25 14:06 akuuly-7

ok did some more analysis - the updated tool definitions extracted from claude code are taking up ~ 10k tokens

looking deeper to see if there's something i messed up here

thdxr avatar Jun 16 '25 15:06 thdxr

can you try v0.1.55?

we inspected claude code again and they also send ~ 10K tokens in tool call descriptions. main difference is they have prompt caching turned on (we thought we had it on but we didn't)

with that the token counts dropped and anthropic docs say cached input tokens don't count to rate limit

thdxr avatar Jun 16 '25 15:06 thdxr

sorry i mean v0.1.56 - had a typo

thdxr avatar Jun 16 '25 16:06 thdxr

can you try v0.1.55?we inspected claude code again and they also send ~ 10K tokens in tool call descriptions. main difference is they have prompt caching turned on (we thought we had it on but we didn't)with that the token counts dropped and anthropic docs say cached input tokens don't count to rate limit

haha! that was it :) didn't even pop into my mind that caching wasn't turned on and it makes total sense it wouldn't work without it :D great find!

akuuly-7 avatar Jun 16 '25 16:06 akuuly-7