opencode icon indicating copy to clipboard operation
opencode copied to clipboard

[Feature Request] Add User Controls for Context Management

Open ddbaron opened this issue 1 year ago • 19 comments

Context for request There are two main ways developers use Opencode:

  1. AI-assisted programming — working side-by-side with the model as a “pair programmer.” In this mode, reliability and precision are critical, and performance drops when the context window grows beyond ~30–40k tokens.
  2. Agentic workflows — where the model uses tools autonomously (searching files, pulling in directories, etc.). Here, having a very large context is fine because the model is orchestrating tasks rather than producing line-by-line code.

Problem The challenge is that Opencode’s current agentic defaults tend to balloon the context window to 150k+ tokens very quickly. This works fine for tool-driven workflows, but when switching into pair programming mode, it hurts performance. Opencode provides little manual control.

Proposal Add lightweight context management commands so users can actively manage the session when needed (inspired by Aider):

  • /drop — remove specific files from context
  • /list — show all files currently in context
  • /tokens — display token usage breakdown (chat history, prompts, each file, repo metadata, etc.)
  • /clear — clear chat history only (keep other context)
  • /reset — reset the session (maybe equivalent to the existing /new session)

Impact

  • Maintains Opencode’s smart defaults for agentic workflows.
  • Empowers power users doing AI-assisted programming to keep the model operating in its “sweet spot” (~30–40k tokens), reducing hallucinations and improving code reliability.
  • Creates a clear differentiator vs. Claude Code and other assistants that don’t offer this level of fine-grained control.
  • dual-mode advantage (“best of both worlds: autonomous agentic + precise pair programming”)

Priority Moderate: Low engineering complexity, high impact for advanced users.

ddbaron avatar Aug 16 '25 12:08 ddbaron

This issue might be a duplicate of existing issues. Please check:

  • #1349: Specifically requests a /tokens command showing token usage per file (similar to what Aider provides)
  • #1014: Requests context editing capabilities
  • #1789: Requests a reset tool for reverting to previous messages
  • #883: Requests showing details about context sources and token usage

Feel free to ignore if none of these address your specific case.

github-actions[bot] avatar Aug 16 '25 12:08 github-actions[bot]

interesting idea, haven't seen this mentioned before

rekram1-node avatar Aug 16 '25 13:08 rekram1-node

this is something we can think about once we're moved to opentui - i think it's an interesting idea just needs thinking around ux

thdxr avatar Aug 17 '25 03:08 thdxr

Hi, thanks for considering.

Is opentui eminent? If not, should I consider writing a PR for the current UI in the meantime? The UX is definitely the biggest challenge to get right (needs to feel and works like OC), as I think the backend would easily support these features with minimal modifications.

ddbaron avatar Aug 17 '25 14:08 ddbaron

I think we can break this proposal down into two components:

  1. Methods to get transparency into the current token usage (e.g. the /tokens command proposed by OP)

  2. Methods to control the token usage (e.g. the other commands proposed by OP)

It would be great if we could prioritize #1 first, which should be much easier than #2.

nikhilmaddirala avatar Aug 18 '25 04:08 nikhilmaddirala

Claude Code have a nice feature for it. Image

Image

westito avatar Oct 10 '25 18:10 westito

Proposal Add lightweight context management commands so users can actively manage the session when needed (inspired by Aider):

* `...`
  • /add — insert specific files into context

A related configuration variable (agent file) is suggestive:

add_context: raw, tree-sitter

taqtiqa-mark avatar Oct 12 '25 23:10 taqtiqa-mark

@taqtiqa-mark we already have mechanisms to add files to context, you can directly @ a file and it will directly inject the context into session, you can also @ a dir to get a list of the files / dirs in it

rekram1-node avatar Oct 13 '25 14:10 rekram1-node

I'll focus further comments in #2965.

Relevant here is that /add should be a built in command, independent of how an agent is configured (import_at: true,false, where current default behavior is consistent with import_at: false, and that may/will change).

I believe they are categorically distinct:

  • /add: A built in command. Importing text into a LLM's Context "buffer"
  • @: A DSL-style of convention, with configurable behavior that can vary by agent/sub-agent. Importing text into a LLM's Prompt "buffer"

Can you clarify where that understanding is off?

taqtiqa-mark avatar Oct 14 '25 01:10 taqtiqa-mark

this doesnt have to do with agents im talking about the prompt input box, when typing in your prompt furing a chat flow, you can introduce anything to context like a file or dir using @ it will bring up a modal and you hit enter to select the correct file, the contents of it will be inserted into context

so a /add command seems to do the same thing in the same place no?

rekram1-node avatar Oct 14 '25 01:10 rekram1-node

this doesnt have to do with agents im talking about the prompt input box

This issue is clearly targeted at "User Controls for Context Management". Can we keep the discussion about @ and the prompt text to the issue #2965?

so a /add command seems to do the same thing in the same place no?

No. As I stated above the effect of @ will be configurable, and concern "Prompt text", obtained from the config hierarchy. The effect of /add should always be adding text to the "Context text", obtained from the project hierarchy.

These concern fundamentally different sources and destinations.

Or have I misunderstood, and OC makes no distinction between Prompt text and Context text?

taqtiqa-mark avatar Oct 14 '25 01:10 taqtiqa-mark

@ it will bring up a modal and you hit enter to select the correct file

Yes you can implement this. How does scripting or | piping work? I now have to use expect?

I really don't understand the emphasis on overloading @ with so many different behaviors.

To my mind the need for the user friction (manual interaction with a dialogue) you described is a direct consequence of this overloading. That fine, everything is a tradeoff.... but what is the benefit we get for introducing this friction?

taqtiqa-mark avatar Oct 14 '25 01:10 taqtiqa-mark

This issue is clearly targeted at https://github.com/sst/opencode/issues/1990. Can we keep the discussion about @ and the prompt text to the issue https://github.com/sst/opencode/issues/2965?

I'm just trying to respond to your comments :) I think this behavior already exists

Maybe you can outline an example of what /add would do?

Like /add and then what the llm would see as a result?

So i can follow better?

Or have I misunderstood, and OC makes no distinction between Prompt text and Context text?

When you add a file to context the content of it isn't literally injected, there is a hidden tool call we present to the llm with the file content as a result (similar to other tools like claude code) in this case I think you would refer to it as "context text"? Maybe you can outline the distinction you meant if I misunderstand

Here is the behavior I am referring to for reference you can add any file or directory to context:

https://github.com/user-attachments/assets/73c72250-52b5-4364-92e7-2b2cb63ac056

rekram1-node avatar Oct 14 '25 02:10 rekram1-node

Maybe you can outline an example of what /add would do

What I stated above:

  • /add: A built in command. Importing text into a LLM's Context "buffer"

"Start with the doc's you wished you had":

/add

Add a file from the project folder hierarchy to the Context Text submitted to the LLM. In code generation scenarios the Context Text is often program files, debug logs, etc. In other scenarios it could be a document the Prompt Text instructs the LLM to edit or evaluate, etc.

Example

    /add src/user/create.ts
    /add test/e2e/create_user.feature
    /add log/debug/test/e2e/create_user.log

Like /add and then what the llm would see as a result?

From, for example the OpenAI docs:

{text from @ files here (assuming @@ files are inserted}

Text: """
{text from /add files here}
"""

I'd also add some detail to the docs to set out the context/prompt distinctions, something like:

The **Prompt Text** is the immediate trigger for a response, while the **Context Text** provides the necessary background and framework for that response...

taqtiqa-mark avatar Oct 14 '25 02:10 taqtiqa-mark

gotcha that makes more sense

rekram1-node avatar Oct 14 '25 02:10 rekram1-node

Here is the behavior I am referring to for reference you can add any file or directory to context:

That is largely what I expected. I still believe the overloaded and implicit behavior for @ is a terrible idea - sorry.

The popup file selection should still be able to work with \add, and also with \drop

taqtiqa-mark avatar Oct 14 '25 03:10 taqtiqa-mark

Claude Code have a nice feature for it. Image

Image

Working on agentic workflows with subagents this view would help me a ton to understand the context usage and how I can optimize it. Further it helps a lot for understanding the basics of the context window and why it is so important. It could be really good for users that just started with using opencode and could be a feature why they stick with it.

bf-frieder avatar Nov 17 '25 08:11 bf-frieder

In the spirit of calling out excellent engineering and design....

I've found that the Pi Coding Agent is more robust and, for me has none of the issues that I've encountered in OC, or has an elegant way to resolve them.... I'm dropping out of OC issues:

taqtiqa-mark avatar Feb 06 '26 01:02 taqtiqa-mark

To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

github-actions[bot] avatar Jun 02 '26 03:06 github-actions[bot]