Skip to content

Reduce context usage for create_or_update_file tool#2027

Open
tommaso-moro wants to merge 1 commit intomainfrom
tommy/create-or-update-file-context
Open

Reduce context usage for create_or_update_file tool#2027
tommaso-moro wants to merge 1 commit intomainfrom
tommy/create-or-update-file-context

Conversation

@tommaso-moro
Copy link
Contributor

@tommaso-moro tommaso-moro commented Feb 17, 2026

Closes: #2028

Summary

This PR reduces the tokens used by the create_or_update_file tool. When tested it showed a ~69% reduction in tokens usage.

It does so by using the minimal types pattern (which is already used elsewhere in the codebase for the same reason) to reduce the payload that is sent back to the model when the tool is used. Specifically, some irrelevant fields are removed, and nested objects are largely optimized by keeping only the strictly necessary fields.

Tests & Metrics

Results: tokens consumption went from 614 to 201, achieving a ~69% reduction in tokens usage.

Tokens were measured using the OpenAI Tokenizer

Fields preserved

name, path, sha, size, html_url (content); sha, message, html_url, author.name, author.email, author.date (commit)

Fields dropped

type, encoding, content, target (content); url, git_url, download_url, submodule_git_url (content API URLs); committer (commit — duplicates author in most cases), tree, parents, verification, node_id, url, comment_count (commit)

Before

Old payload (614 tokens)
{
 "content": {
   "type": "file",
   "size": 25,
   "name": "README.md",
   "path": "README.md",
   "sha": "2ff31689ecfd4f3296a0cab5f3a69c14e6d6a71a",
   "url": "https://api.github.com/repos/tommaso-moro/test-mcp-stuff/contents/README.md?ref=master",
   "git_url": "https://api.github.com/repos/tommaso-moro/test-mcp-stuff/git/blobs/2ff31689ecfd4f3296a0cab5f3a69c14e6d6a71a",
   "html_url": "https://github.com/tommaso-moro/test-mcp-stuff/blob/master/README.md",
   "download_url": "https://raw.githubusercontent.com/tommaso-moro/test-mcp-stuff/master/README.md"
 },
 "commit": {
   "sha": "af913d0cb94587abfc3203bc004fb81ca0ea5736",
   "author": {
     "date": "2026-02-17T16:06:58Z",
     "name": "Tommaso Moro",
     "email": "37270480+tommaso-moro@users.noreply.github.com"
   },
   "committer": {
     "date": "2026-02-17T16:06:58Z",
     "name": "Tommaso Moro",
     "email": "37270480+tommaso-moro@users.noreply.github.com"
   },
   "message": "Add \"hello world\" to README.md",
   "tree": {
     "sha": "8f92333e6427529dc323e62ab3e8d53db1df0ee7"
   },
   "parents": [
     {
       "sha": "1a5dc5d233f591b1420e84a39cfc0688cb5a6813",
       "html_url": "https://github.com/tommaso-moro/test-mcp-stuff/commit/1a5dc5d233f591b1420e84a39cfc0688cb5a6813",
       "url": "https://api.github.com/repos/tommaso-moro/test-mcp-stuff/git/commits/1a5dc5d233f591b1420e84a39cfc0688cb5a6813"
     }
   ],
   "html_url": "https://github.com/tommaso-moro/test-mcp-stuff/commit/af913d0cb94587abfc3203bc004fb81ca0ea5736",
   "url": "https://api.github.com/repos/tommaso-moro/test-mcp-stuff/git/commits/af913d0cb94587abfc3203bc004fb81ca0ea5736",
   "verification": {
     "verified": false,
     "reason": "unsigned"
   },
   "node_id": "C_kwDOPODl29oAKGFmOTEzZDBjYjk0NTg3YWJmYzMyMDNiYzAwNGZiODFjYTBlYTU3MzY"
 }
}

After

New payload: 201 tokens
{
 "content": {
   "name": "README.md",
   "path": "README.md",
   "sha": "3b18e512dba79e4c8300dd08aeb37f8e728b8dad",
   "size": 12,
   "html_url": "https://github.com/tommaso-moro/test-mcp-stuff/blob/master/README.md"
 },
 "commit": {
   "sha": "1a5dc5d233f591b1420e84a39cfc0688cb5a6813",
   "message": "Update README.md with hello world",
   "html_url": "https://github.com/tommaso-moro/test-mcp-stuff/commit/1a5dc5d233f591b1420e84a39cfc0688cb5a6813",
   "author": {
     "name": "Tommaso Moro",
     "email": "37270480+tommaso-moro@users.noreply.github.com",
     "date": "2026-02-17T16:05:27Z"
   }
 }
}

Why

The full github.RepositoryContentResponse payload returned by the GitHub API is unnecessarily verbose for model reasoning. The embedded Commit object contains deeply nested structures — tree, parents (which are themselves full Commit objects with their own URLs), verification (signature details), and a committer that almost always duplicates author — none of which are useful to the model after a file create/update. The Content object also includes multiple API URL variants (url, git_url, download_url, submodule_git_url) that duplicate information derivable from html_url. Since create_or_update_file is one of the top 3 most-used tools, this overhead is incurred on a large fraction of all tool calls.

What changed

  • Added MinimalFileContentResponse, MinimalFileContent, and MinimalFileCommit types to minimal_types.go, following the existing MinimalCommit/MinimalIssue/MinimalPullRequest pattern
  • Added convertToMinimalFileContentResponse converter function
  • Updated CreateOrUpdateFile to return MinimalFileContentResponse via MarshalledTextResult instead of raw json.Marshal(fileContent)
  • Updated tests to assert against MinimalFileContentResponse fields

MCP impact

  • No tool or API changes
  • Tool schema or behavior changed
  • New tool added

Prompts tested (tool changes only)

Security / limits

  • No security or limits impact
  • Auth / permissions considered
  • Data exposure, filtering, or token/size limits considered

Tool renaming

  • I am renaming tools as part of this PR (e.g. a part of a consolidation effort)
    • I have added the new tool aliases in deprecated_tool_aliases.go
  • I am not renaming tools as part of this PR

Note: if you're renaming tools, you must add the tool aliases. For more information on how to do so, please refer to the official docs.

Lint & tests

  • Linted locally with ./script/lint
  • Tested locally with ./script/test

Docs

  • Not needed
  • Updated (README / docs / examples)

@tommaso-moro tommaso-moro marked this pull request as ready for review February 17, 2026 16:18
@tommaso-moro tommaso-moro requested a review from a team as a code owner February 17, 2026 16:18
Copilot AI review requested due to automatic review settings February 17, 2026 16:18
@tommaso-moro tommaso-moro changed the title optimize context usage Reduce context usage for create_or_update_file tool Feb 17, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR reduces token usage for the create_or_update_file tool by returning a trimmed/minimal response payload instead of the full github.RepositoryContentResponse, aligning with the repository’s existing “minimal types” approach.

Changes:

  • Introduces MinimalFileContentResponse (+ related minimal structs) and a converter in minimal_types.go.
  • Updates CreateOrUpdateFile to return the minimal response via MarshalledTextResult (and in the warning path).
  • Updates Test_CreateOrUpdateFile to unmarshal and assert against the new minimal response type.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
pkg/github/repositories.go Returns minimized JSON payload for create_or_update_file results (including warning flow).
pkg/github/minimal_types.go Adds minimal file content/commit response types + conversion helper.
pkg/github/repositories_test.go Adjusts create/update file tests to validate the new minimal response type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reduce context usage for create_or_update_file tool

1 participant