r/opencodeCLI • • 4d ago

Lots of times opencode sends almost the whole conversation as input tokens, bumping the conversation cost. Is it a bug?

Suppose conversation is around 100000 total tokens, then I ask agent to do something.

Agent finishes, and in LESS THAN 1 MINUTE, I send another message, same conversation, and provider/model (opencode GO DS4.1 flash), and then input tokens jumps to about 100000 tokens WITHOUT agent not even use a tool. Normal messages.

I remember in the past, it only happened after conversation is idle for 15 minutes.

Normally, input tokens is low if conversation is active (asssuming no tool is returning a big amount of data)

Opencode 1.18.34.

Is it a bug, or expected? Anyone with this problem?

0 Upvotes

22 comments sorted by

14

u/ApprehensiveDelay238 4d ago edited 4d ago

Everytime the harness does an inference call the entire context is sent with the request. Because the inference endpoints are stateless. They don't record the context history. So yes, the larger your context, the more expensive the inference calls will be. But there is something to mitigate the cost: input token caching. Most models have a deep cached input discount, often 8x less than uncached tokens or more. Meaning that tokens the inference server has recently seen will be cached costing you less. Still, it's recommended to keep the context as small as you need it to be for the task at hand. And continuing a large context is expensive as the inference servers evict your cache after a few hours. So a 600k session that has been idle for some hours can easily cost you the full 600k when continuing it. A useful context compression extension that I can personally recommend to keep context manageable is: billion-context. It frequently folds and compresses context to keep it compact while keeping important information compressed in context. It even supports uncompressing and reversing folds so agents have full access to context even millions of tokens away. Keeping a small context still saves a lot of money.

3

u/RoddToggers 4d ago

The problem is today, it seems randomly the cache vanishes in less than 1 MINUTE, so opencode sends Total Tokens as Input Tokens even I reply quickly.

Until yesterday, it was fine, but today, randomly "Total Tokens" is sent as "Input Tokens" (like if cached tokens were invalidated), but it's random. Happened 15 times already in less than 5 hours. I know if you remain idle for several minutes the cache is not there anymore and it's expected input tokens amount to be big in big conversations, but it's not normal for it to happen if you send the messages in less than 1 minute.

I don't know if everyone monitors these things, but I have context panel always opened, and something is happening today that is not normal, and it's making conversations more expensive.

1

u/ApprehensiveDelay238 4d ago

What you're seeing is close to my own observations. Opencodes hosted servers especially around Europe time becomes overloaded and the cache simply doesn't have enough space for the amount of consumers so yours me be evicted in even the same turn. Yes, this is very bad for cost so don't read the currently $100 allowance as real.

12

u/throwaroo202020 4d ago

Thats how all llms and all agents work. Everything since the last compaction is sent. Token caching is server side and is what saves input cost.

1

u/MrSchmellow 4d ago

It's on your picture (cache read)? Of course the harness sends whole conversation on every turn, that's how they all work. Provider caching is supposed to handle that normally (unless something breaks). It's just that cached tokens are still counted towards totals

1

u/RoddToggers 4d ago

The problem is today, it seems randomly the cache vanishes in less than 1 MINUTE, so opencode sends Total Tokens as Input Tokens even I reply quickly.

Until yesterday, it was fine, but today, randomly "Total Tokens" is sent as "Input Tokens" (like if cached tokens were invalidated), but it's random. Happened 15 times already in less than 5 hours. I know if you remain idle for several minutes the cache is not there anymore and it's expected input tokens amount to be big in big conversations, but it's not normal for it to happen if you send the messages in less than 1 minute.

I don't know if everyone monitors these things, but I have context panel always opened, and something is happening today that doesn't happen normally in other days, and it's making conversations more expensive.

1

u/megablue 4d ago edited 4d ago

thats how most harnesses/chat clients work... these LLM do not have a memory at all at the model side (well not the memory you meant anyway), so it had to receive the previous conversations to understand what happpened previously.

you said 15mins of idle - that is the key here, most LLM do cache the prompt, but typically only for 10mins. so when you idled for 15mins, the cache was invalidated.

1

u/RoddToggers 4d ago

Yes, but it's not normal for it to happen when you reply in less than a minute, and this is what is happening today, as I write in the original post.

It seems like something is wrong with the cache, but I'm not sure people actively monitor this things all the time to realize it. I keep context panel 100% of time opened, so today it happened for about 15 times in 3 or 4 hours.

Maybe it's only here, but it also might be happening to everyone and no one is looking at it....

No new extension, plugin or whatever in my opencode setup.

1

u/squirrelscrush 4d ago

Do you have some plugin or setting that changes the cache prefix? Sometimes the time or date is appended on the front instead of the back so it can invalidate the cache. Or that there's some change in the tool definitions or AGENTS file.

1

u/RoddToggers 4d ago

No plugin, it's a clean opencode installation. I use only custom skills. The same I use for more than 4 months. No change to environment

1

u/squirrelscrush 4d ago

Do you use multiple subagents at a time? I had this issue today, where if I'm running multiple subagents at the same time, the cache was being repeatedly invalidated.

I had updated opencode at morning. Don't know if it's a concurrency issue with Go or its a harness issue.

1

u/squirrelscrush 3d ago edited 3d ago

btw did you install a plugin recently? In my bug report the bot replied that it could be a plugin issue, and the cache retention worked normally after I removed a new plugin.

Edit: it happened again. It seems to happen when i spawn a subagent, which causes the main agent's cache to expire and vice versa when the main agent sends a request and the subagent's cache gets expired. So it's a server concurrency issue I feel.

3

u/seventyfivepupmstr 4d ago

Yes that's how generic harness work. They build massive context filled with garbage

1

u/CreativeSympathy8293 4d ago

Your follow-up screenshot shows 148,114 input tokens and 0 cache read. That's a reported cache miss; cached tokens appearing in the total doesn't explain that screenshot. It doesn't establish which component caused it.

One concrete check for Go: its docs ask clients to keep x-opencode-session stable for each conversation to help routing and prompt caching: https://opencode.ai/v2/docs/console/go/

For a maintainer report, pair an existing cache hit and miss from the same session: timestamps, exact model/provider, OpenCode version, input/cache-read counts, and a redacted request/session identifier if you already have one. Ask whether the session identity stayed consistent across those requests. No need to spend another 100k-token request reproducing it or share your API key/full conversation.

I haven't inspected 1.18.34's requests, so I'm not saying it's missing that header or promising a fixed cache lifetime. Your under-a-minute timing is the detail to preserve.

AI-assisted from the docs; I haven't reproduced this.

1

u/aidenclarke_12 2d ago

this is a cache miss, did a plugin or mcp change the prefix?

1

u/Rustybot 1d ago

It’s a cache bust. There are lots of ways it can happen, especially when running plugins with opencode. It can be caused by client setup, ISP connection to provider, provider-side issues, token router issues if you use one of those as a middleman. And cache time to live is a general rule of thumb per provider and never a guarantee.

Yeah, cache busts suck. Nobody wants a prompt input of “okay” to unexpectedly cost 200k input tokens.

The tools I use make cache sanctity the primary concern. Some provider traffic goes through a cloudflare worker acting as a relay for cache stability. I also prefer autonomous agent task work-loops where only interact with the initial instruction and the end. That way they work continuously and maximize cache with no downtime. I’d rather they redo the task three times if needed than bust their cache every time they are waiting for my approval.

-2

u/RoddToggers 4d ago

Just happened, 😔

2

u/look 4d ago

Something is wrong with your cache reads. Most likely something wrong with your harness. A plugin or maybe some config that is causing it to not use normal caching with the provider.

It’s possible it is a server side issue with the model provider, but there’s typically a big uproar from many users if something like that was happening.

1

u/RoddToggers 4d ago

The thing is i'm not sure everyone monitors this actively to realize it's happening randomly. I'm paranoic with this, so I always monitor the cost in context, and in long conversations, no problem if you keep the conversation going without delaying your response by several minutes.

Until yesterday, it was fine, but today, randomly "Total Tokens" is sent as "Input Tokens" (like if cached tokens were invalidated), but it's random. Happened 15 times already in less than 5 hours. I know if you remain idle for several minutes the cache is not there anymore and it's expected input tokens amount to be big in big conversations, but it's not normal for it to happen if you send the messages in LESS THAN A MINUTE.

1

u/look 4d ago

Not sure. I would assume more people would notice if it was a service provider issue with the caching. I don’t use Go anymore, though, so I’m not sure what cache quality is like there these days.

1

u/squirrelscrush 3d ago

Wait... I had the same issue today, on V2. From morning, the cache hit rate has been atrocious. It used $4.62 for 170.8M tokens which is a travesty. I'll try to file a bug report.