Skip to content

fix(agent): include SystemParts in token estimation and add reasoning guards#2014

Open
badgerbees wants to merge 1 commit intosipeed:mainfrom
badgerbees:fix/context-pruning-guards
Open

fix(agent): include SystemParts in token estimation and add reasoning guards#2014
badgerbees wants to merge 1 commit intosipeed:mainfrom
badgerbees:fix/context-pruning-guards

Conversation

@badgerbees
Copy link
Contributor

📝 Description

This PR fixes a deficiency in the token estimation logic for context pruning. Currently, the agent ignores SystemParts (structured system blocks used for cache-aware adapters and large instructions), leading to underestimated token counts and potential context window overflows (400 errors). It also streamlines the reasoning content path for better robustness.

🗣️ Type of Change

  • 🐞 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 📖 Documentation update
  • ⚡ Code refactoring (no functional changes, no api changes)

🤖 AI Code Generation

  • 🤖 Fully AI-generated (100% AI, 0% Human)
  • 🛠️ Mostly AI-generated (AI draft, Human verified/modified)
  • 👨‍💻 Mostly Human-written (Human lead, AI assisted or none)

🔗 Related Issue

N/A

📚 Technical Context (Skip for Docs)

  • Reference URL: N/A
  • Reasoning: In estimateMessageTokens, msg.SystemParts was being ignored. Large instructions or RAG indices kept in system messages would stay hidden from the budget checker, causing overflows on the next provider call.

🧪 Test Environment

  • Hardware: PC
  • OS: Windows 11
  • Model/Provider: Anthropic Sonnet 3.7
  • Channels: CLI / Any

📸 Evidence (Optional)

Click to view Logs/Screenshots
=== RUN   TestEstimateMessageTokens_SystemParts
--- PASS: TestEstimateMessageTokens_SystemParts (0.00s)
PASS
ok      github.com/sipeed/picoclaw/pkg/agent    0.340s

☑️ Checklist

  • My code/docs follow the style of this project.
  • I have performed a self-review of my own changes.
  • I have updated the documentation accordingly..

@sipeed-bot sipeed-bot bot added type: bug Something isn't working domain: agent go Pull requests that update go code labels Mar 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

domain: agent go Pull requests that update go code type: bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant