Skip to content

write_file echoes back the entire file the model just wrote #762

Closed

Description

@will-lamerton

Problem

source/tools/file-ops/write-file.tsx:48-64 reads the file back after writing and returns every line of it, line-number prefixed, as the tool result:

// Generate full file contents to show the model the current file state
let fileContext = '\n\nFile contents after write:\n';
for (let i = 0; i < lines.length; i++) {
    const lineNumStr = String(i + 1).padStart(4, ' ');
    const line = lines[i] || '';
    fileContext += `${lineNumStr}: ${line}\n`;
}

This is pure duplication. The model just sent that exact content as the tool call arguments. We pay for the same bytes twice in the same conversation, and the second copy is about 6 characters per line more expensive than the first because of the line-number prefix.

There is no cap, so the cost scales linearly with file size.

Impact

Measured on files in this repo, the echo alone costs roughly:

  • source/app/App.tsx (791 lines): ~8,500 tokens
  • source/hooks/useAppState.tsx (447 lines): ~4,500 tokens
  • source/tools/tool-manager.ts (414 lines): ~3,650 tokens

And because tool results stay in the conversation, that cost is re-sent on every subsequent step of the agent loop.

Fix

Replace the echo with a confirmation line:

return `File ${action} successfully (${lineCount} lines).`;

This is losslessly removing 100% of the echo. The model cannot learn anything from the echo that it did not already know, since it authored the content.

Worth keeping the read-back itself (it verifies the write succeeded) and just not returning the content.

Notes for a first-time contributor

Small, isolated change in one function. Existing tests are in source/tools/file-ops/write-file.spec.tsx and will need their expected-result assertions updated. Run with pnpm run test:ava source/tools/file-ops/write-file.spec.tsx.

Activity

  1. added
    enhancement
    good first issue
    performance
    on Aug 3, 2026
  2. will-lamerton commented on Aug 3, 2026

    @will-lamerton
    MemberAuthor

    Part of #772.

  3. yashkhatri012 commented on Aug 8, 2026

    @yashkhatri012

    I'd like to work on this. Can you assign it to me?

  4. added a commit that references this issue on Aug 9, 2026
    2a72da5
  5. added
    area:providers
    on Oct 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:providersenhancementgood first issueperformance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions