opencode icon indicating copy to clipboard operation
opencode copied to clipboard

Why must the read tool be executed before the edit tool

Open mooneal opened this issue 2 months ago • 24 comments

Question

I've read the description of the edit tool and found it states: "You must use your Read tool at least once in the conversation before editing." I wonder, if a file is created by AI and its content is already present in the context, would using the Read tool again unnecessarily increase token costs?

mooneal avatar Nov 17 '25 08:11 mooneal

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

  • #4405: Exact same title and question about read tool requirement before edit tool and token cost concerns

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

github-actions[bot] avatar Nov 17 '25 08:11 github-actions[bot]

@mooneal I think you accidentally created the same issue twice (#4405).

veracioux avatar Nov 17 '25 19:11 veracioux

@mooneal we should handle that case, currently unhandled should be an easy fix

rekram1-node avatar Nov 18 '25 06:11 rekram1-node

Couldn't reproduce. Are we sure it happens?

Image

Write tool seems to mark the file as read after creating it: https://github.com/sst/opencode/blob/5d25758400e546775f7300dc883b0e039df303e9/packages/opencode/src/tool/write.ts#L75

renanliberato avatar Nov 19 '25 18:11 renanliberato

Thanks for jumping in @renanliberato

@mooneal can you provide reproduction steps?

Main thing I can think of is that we store the file read time in memory so If the llm reads a file, I quit, restart, then it tries to edit, it will be forced to read again (annoyingly)

rekram1-node avatar Nov 20 '25 06:11 rekram1-node

Happens for me too, can we just have an option of disabling it? as tokens are being wasted. The error "You must use your Read tool at least once in the conversation before editing." appears when the model is attempting to edit a file again. I simply use Opencode as usual; I don't have a replication step. look at the attached image, the time diff between last read and edited is 0.2 seconds

Image

Hoodgail avatar Dec 02 '25 09:12 Hoodgail

Yeah we could add some flag to disable that, I think often you'll find it saving tokens since the LLM is forced to re-read and makes it less likely to corrupt the file.

Ofc that's the idea behind it at least

rekram1-node avatar Dec 02 '25 16:12 rekram1-node

took a stab at this.

rodnnnney avatar Dec 04 '25 05:12 rodnnnney

Oh, across sessions, nice @rodnnnney

renanliberato avatar Dec 05 '25 14:12 renanliberato