Skip to content

/context estimate can account for more tokens than the context window #13239

Description

@Shizoqua

What happened?

Before the first model reply (or after a /model switch or resume, when /context has no provider token count), the estimated breakdown can add up to more than the context window. With tools.codeModeOnly and an MCP server whose tools are always loaded, the MCP schemas are billed in the MCP tools row even though they are not in the declared tool list, so the rows, free space and autocompact buffer overshoot the window by the size of those schemas.

Reproduced with the fixture from the existing charges the builtin-clamp deficit to the mcp row, not to messages test, using the estimated path (total: 0):

window=200000 accounted=200128

What did you expect to happen?

The estimated breakdown should account for exactly the window, the same as the provider-count path. That path already charges the deficit to the MCP row (the existing test above); the estimated path skips it.

Client information

Built from main at c5b0e1f (0.24.7), Linux. Not platform specific.

Login information

N/A, no model call is needed to reproduce.

Anything else we need to know?

The overshoot equals the MCP schema tokens that are not in the declared tools, so it can be thousands of tokens with a large MCP server. The fix is to apply the existing clamp in both branches of the breakdown. I'd like to send a small fix with a test.

Activity

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

    category/cliCommand line interface and interactionpriority/P3Low - Minor, cosmetic, nice-to-fix issuesroadmap/context-performanceRoadmap: Context and performancescope/commandsCommand implementationscope/token-managementToken handling and limitstype/bugSomething isn't working as expected

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions