Skip to content

Token counting in Auto-compact uses provider metadata#3788

Merged
katzdave merged 12 commits intomainfrom
dkatz/token-counting-quickfix
Aug 4, 2025
Merged

Token counting in Auto-compact uses provider metadata#3788
katzdave merged 12 commits intomainfrom
dkatz/token-counting-quickfix

Conversation

@katzdave
Copy link
Collaborator

@katzdave katzdave commented Aug 1, 2025

Still a slight discrepancy in the auto-compact completed message (a few hundred tokens) from estimating the system prompt, but the triggering logic is now from the metadata + aligned to the context window indicator.

…ng-quickfix

* 'main' of github.com:block/goose:
  Ensure more client (#3787)
  fix(ui): extension command text overflow (#3785)
session_metadata: Option<&crate::session::storage::SessionMetadata>,
) -> Result<CompactionCheckResult> {
// Get threshold from config or use override
let config = Config::global();
Copy link
Collaborator

Choose a reason for hiding this comment

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

let's kill the environment variables - this function should just have the threshold passed in. some caller, somewhere may use the environment variable for now. but not this deep into things

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Where do we draw the line what is/isn't allowed to read the global config? I do agree with you that it would be more clear if this parameter got passed in but if there are too many params it leads to a lot of clutter.

let context_limit = estimate_target_context_limit(provider);
let context_limit = provider.get_model_config().context_limit();

// Try to use actual token counts from session metadata first
Copy link
Collaborator

Choose a reason for hiding this comment

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

remove comment

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

let (current_tokens, token_source) = match session_metadata.and_then(|m| m.total_tokens) {
Some(tokens) => (tokens as usize, "session metadata"),
None => {
// Fall back to estimated counts
Copy link
Collaborator

Choose a reason for hiding this comment

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

remove

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

};

// Calculate usage ratio
let usage_ratio = current_tokens as f64 / context_limit as f64;
Copy link
Collaborator

Choose a reason for hiding this comment

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

should we guard against division by zero here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

provider.get_model_config().context_limit() has a default value of 128k so can't get 0 div here.

tokens_after: Some(tokens_after),
tokens_before: Some(tokens_before + SYSTEM_PROMPT_TOKEN_OVERHEAD + TOOLS_TOKEN_OVERHEAD),
tokens_after: Some(tokens_after + SYSTEM_PROMPT_TOKEN_OVERHEAD + TOOLS_TOKEN_OVERHEAD),
})
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems wrong; we know how many tokens we had before (sometimes estimated, but for most models we should get a result from them directly). So we also know what the overhead is; the actual tokens before - tokens_before as calculated here. then we can add that to tokens_after for the real value

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is because the summary prompt doesn't include the system prompt on either end.

So this estimate is pretty reasonable. #3721 adds the return values from the summarizer so will switch to those instead on there.

@@ -530,4 +558,137 @@ mod tests {
.set_param("GOOSE_AUTO_COMPACT_THRESHOLD", serde_json::Value::from(0.3))
Copy link
Collaborator

Choose a reason for hiding this comment

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

this will create side effects when it fails

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It gets unset at the end, but yes definitely can see why these can get leaky.

let messages = vec![create_test_message("Hello"), create_test_message("World")];

let result = check_compaction_needed(&agent, &messages, Some(0.3))
let result = check_compaction_needed(&agent, &messages, Some(0.3), None)
Copy link
Collaborator

Choose a reason for hiding this comment

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

do we ever test the meta_data in these calls? I'd maybe kill a few of these tests and have that does

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

…ng-quickfix

* 'main' of github.com:block/goose: (26 commits)
  docs: Add YouTube link to Git MCP Tutorial (#3831)
  feat: more robust client initialization for the app (#3830)
  Build app bundles on release branches always (#3789)
  fix param order of debug_conversation_fixer (#3796)
  Fix directory switcher not working in active chat sessions and file browser not defaulting to current session directory path (#3791)
  File completion in CLI (#3822)
  docs: Dynamic linux install buttons (#3810)
  tests: Add missing `#[serial]` to two tests (#3816)
  Chore: apply more clippy rules to prevent from code complexity (#3813)
  chore(mcp): Add helpers to parse parameters (#2821)
  feat: enable docusaurus respectPrefersColorScheme (#3746)
  fix session resume in new window (#3800)
  Add settings field documentation to recipe guides (#3809)
  chore(deps): bump on-headers and compression in /documentation (#3532)
  fix(ui): refresh provider related issues (#3385)
  feat: Add comprehensive Linux build support (#3673)
  developer: Optimize text_editor_view a bit (#3781)
  Override session name generator for ollama provider (#3710)
  docs: fix markdown for cognee tutorial (#3801)
  chore: Upgrade node (#3756)
  ...
@katzdave katzdave merged commit 63f4374 into main Aug 4, 2025
11 checks passed
@katzdave katzdave deleted the dkatz/token-counting-quickfix branch August 4, 2025 19:45
michaelneale added a commit that referenced this pull request Aug 4, 2025
* main: (34 commits)
  Token counting in Auto-compact uses provider metadata (#3788)
  docs: Add YouTube link to Git MCP Tutorial (#3831)
  feat: more robust client initialization for the app (#3830)
  Build app bundles on release branches always (#3789)
  fix param order of debug_conversation_fixer (#3796)
  Fix directory switcher not working in active chat sessions and file browser not defaulting to current session directory path (#3791)
  File completion in CLI (#3822)
  docs: Dynamic linux install buttons (#3810)
  tests: Add missing `#[serial]` to two tests (#3816)
  Chore: apply more clippy rules to prevent from code complexity (#3813)
  chore(mcp): Add helpers to parse parameters (#2821)
  feat: enable docusaurus respectPrefersColorScheme (#3746)
  fix session resume in new window (#3800)
  Add settings field documentation to recipe guides (#3809)
  chore(deps): bump on-headers and compression in /documentation (#3532)
  fix(ui): refresh provider related issues (#3385)
  feat: Add comprehensive Linux build support (#3673)
  developer: Optimize text_editor_view a bit (#3781)
  Override session name generator for ollama provider (#3710)
  docs: fix markdown for cognee tutorial (#3801)
  ...
michaelneale added a commit that referenced this pull request Aug 5, 2025
* main:
  Token counting in Auto-compact uses provider metadata (#3788)
  docs: Add YouTube link to Git MCP Tutorial (#3831)
  feat: more robust client initialization for the app (#3830)
  Build app bundles on release branches always (#3789)
  fix param order of debug_conversation_fixer (#3796)
  Fix directory switcher not working in active chat sessions and file browser not defaulting to current session directory path (#3791)
  File completion in CLI (#3822)
michaelneale added a commit that referenced this pull request Aug 5, 2025
* main: (56 commits)
  Token counting in Auto-compact uses provider metadata (#3788)
  docs: Add YouTube link to Git MCP Tutorial (#3831)
  feat: more robust client initialization for the app (#3830)
  Build app bundles on release branches always (#3789)
  fix param order of debug_conversation_fixer (#3796)
  Fix directory switcher not working in active chat sessions and file browser not defaulting to current session directory path (#3791)
  File completion in CLI (#3822)
  docs: Dynamic linux install buttons (#3810)
  tests: Add missing `#[serial]` to two tests (#3816)
  Chore: apply more clippy rules to prevent from code complexity (#3813)
  chore(mcp): Add helpers to parse parameters (#2821)
  feat: enable docusaurus respectPrefersColorScheme (#3746)
  fix session resume in new window (#3800)
  Add settings field documentation to recipe guides (#3809)
  chore(deps): bump on-headers and compression in /documentation (#3532)
  fix(ui): refresh provider related issues (#3385)
  feat: Add comprehensive Linux build support (#3673)
  developer: Optimize text_editor_view a bit (#3781)
  Override session name generator for ollama provider (#3710)
  docs: fix markdown for cognee tutorial (#3801)
  ...
kathawthorne added a commit to kathawthorne/goose that referenced this pull request Aug 5, 2025
…-files

* upstream/main: (150 commits)
  fix: replace glob/grep tool with shell (block#3834)
  docs: Add Youtube Link to dev.to tutorial (block#3869)
  Changed app settings configuration form to match settings panels (block#3829)
  Tell the user to hit compact (block#3851)
  Pin @mcp-ui/client in package.json (block#3860)
  blog for mcp-jupyter server (block#3059)
  docs: Adding dev.to Tutorial & Update CLI Component (block#3828)
  Detect client disconnects and cancel tool calls (block#3782)
  Suppress ansi with pipes (block#3775)
  Fix leaky env variable causing flaky test (block#3761)
  Update gemini error msg (block#3847)
  Generic retry and error parsing (block#3558)
  Clear the current line on ctrl-c in line with other tools (block#3764)
  chore: upgrade morph to use new model with instruction (block#3745)
  add CODEOWNERS file with /documentation owners (block#3840)
  Token counting in Auto-compact uses provider metadata (block#3788)
  docs: Add YouTube link to Git MCP Tutorial (block#3831)
  feat: more robust client initialization for the app (block#3830)
  Build app bundles on release branches always (block#3789)
  fix param order of debug_conversation_fixer (block#3796)
  ...

# Conflicts:
#	crates/goose-mcp/src/developer/mod.rs
kathawthorne added a commit to kathawthorne/goose that referenced this pull request Aug 5, 2025
…e-editable-displayable-title

* upstream/main: (134 commits)
  fix: optimise reading large file content (block#3767)
  fix: replace glob/grep tool with shell (block#3834)
  docs: Add Youtube Link to dev.to tutorial (block#3869)
  Changed app settings configuration form to match settings panels (block#3829)
  Tell the user to hit compact (block#3851)
  Pin @mcp-ui/client in package.json (block#3860)
  blog for mcp-jupyter server (block#3059)
  docs: Adding dev.to Tutorial & Update CLI Component (block#3828)
  Detect client disconnects and cancel tool calls (block#3782)
  Suppress ansi with pipes (block#3775)
  Fix leaky env variable causing flaky test (block#3761)
  Update gemini error msg (block#3847)
  Generic retry and error parsing (block#3558)
  Clear the current line on ctrl-c in line with other tools (block#3764)
  chore: upgrade morph to use new model with instruction (block#3745)
  add CODEOWNERS file with /documentation owners (block#3840)
  Token counting in Auto-compact uses provider metadata (block#3788)
  docs: Add YouTube link to Git MCP Tutorial (block#3831)
  feat: more robust client initialization for the app (block#3830)
  Build app bundles on release branches always (block#3789)
  ...
michaelneale added a commit that referenced this pull request Aug 5, 2025
* main: (33 commits)
  fix: optimise reading large file content (#3767)
  fix: replace glob/grep tool with shell (#3834)
  docs: Add Youtube Link to dev.to tutorial (#3869)
  Changed app settings configuration form to match settings panels (#3829)
  Tell the user to hit compact (#3851)
  Pin @mcp-ui/client in package.json (#3860)
  blog for mcp-jupyter server (#3059)
  docs: Adding dev.to Tutorial & Update CLI Component (#3828)
  Detect client disconnects and cancel tool calls (#3782)
  Suppress ansi with pipes (#3775)
  Fix leaky env variable causing flaky test (#3761)
  Update gemini error msg (#3847)
  Generic retry and error parsing (#3558)
  Clear the current line on ctrl-c in line with other tools (#3764)
  chore: upgrade morph to use new model with instruction (#3745)
  add CODEOWNERS file with /documentation owners (#3840)
  Token counting in Auto-compact uses provider metadata (#3788)
  docs: Add YouTube link to Git MCP Tutorial (#3831)
  feat: more robust client initialization for the app (#3830)
  Build app bundles on release branches always (#3789)
  ...
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.

2 participants