[PR #4710] feat: support ZAI token metadata, trigger compaction on idle sessions, add GLM system prompt #11081

Closed
opened 2026-02-16 18:15:52 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/4710

State: closed
Merged: No


Summary

Fix token extraction for ZAI/Anthropic API, improve auto-compaction timing for idle sessions, and add dedicated system prompt for GLM-4.6 model.

Changes

Token Extraction Fix (session/index.ts)

  • Problem: ZAI (Anthropic endpoint) returns token counts in metadata.anthropic.usage instead of the top-level usage object, causing the sidebar to show 0 tokens.
  • Solution: Extract tokens from metadata.anthropic.usage when top-level values are missing/zero.
  • Supports both camelCase (inputTokens) and snake_case (input_tokens) field names.
  • Also supports Bedrock metadata format for consistency.

Compaction Timing Fix (session/prompt.ts)

  • Problem: Auto-compaction check happened after the exit condition in the main loop, so idle sessions at high context usage would exit before compaction could trigger.
  • Solution: Move compaction check before the exit condition.
  • Added hasPendingCompaction guard to prevent infinite loops when a compaction task is already queued.

GLM System Prompt (session/prompt/glm.txt)

Added a dedicated system prompt optimized for GLM-4.6's capabilities:

  • Why? GLM-4.6 has distinct strengths (200K context, advanced reasoning, 30% better token efficiency, stronger agentic performance) that benefit from tailored instructions.
  • Concise structure - Leaner prompt (~57 lines) leverages GLM's token efficiency, leaving more context for actual work.
  • Reasoning-first approach - Added "Reasoning Approach" section to encourage systematic problem breakdown.
  • Tool-agnostic language - Avoids explicit tool names to prevent agents (like Plan) from attempting unauthorized operations.
  • Consistent style - Follows similar structure to anthropic.txt for maintainability.

Testing

  • Verified token counts display correctly in sidebar with ZAI endpoint
  • Confirmed auto-compaction triggers at ~84% context usage
  • Tested compaction works on both active and idle sessions
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/4710 **State:** closed **Merged:** No --- ## Summary Fix token extraction for ZAI/Anthropic API, improve auto-compaction timing for idle sessions, and add dedicated system prompt for GLM-4.6 model. ## Changes ### Token Extraction Fix (`session/index.ts`) - **Problem**: ZAI (Anthropic endpoint) returns token counts in `metadata.anthropic.usage` instead of the top-level `usage` object, causing the sidebar to show 0 tokens. - **Solution**: Extract tokens from `metadata.anthropic.usage` when top-level values are missing/zero. - Supports both camelCase (`inputTokens`) and snake_case (`input_tokens`) field names. - Also supports Bedrock metadata format for consistency. ### Compaction Timing Fix (`session/prompt.ts`) - **Problem**: Auto-compaction check happened *after* the exit condition in the main loop, so idle sessions at high context usage would exit before compaction could trigger. - **Solution**: Move compaction check *before* the exit condition. - Added `hasPendingCompaction` guard to prevent infinite loops when a compaction task is already queued. ### GLM System Prompt (`session/prompt/glm.txt`) Added a dedicated system prompt optimized for GLM-4.6's capabilities: - **Why?** GLM-4.6 has distinct strengths (200K context, advanced reasoning, 30% better token efficiency, stronger agentic performance) that benefit from tailored instructions. - **Concise structure** - Leaner prompt (~57 lines) leverages GLM's token efficiency, leaving more context for actual work. - **Reasoning-first approach** - Added "Reasoning Approach" section to encourage systematic problem breakdown. - **Tool-agnostic language** - Avoids explicit tool names to prevent agents (like Plan) from attempting unauthorized operations. - **Consistent style** - Follows similar structure to `anthropic.txt` for maintainability. ## Testing - Verified token counts display correctly in sidebar with ZAI endpoint - Confirmed auto-compaction triggers at ~84% context usage - Tested compaction works on both active and idle sessions
yindo added the pull-request label 2026-02-16 18:15:52 -05:00
yindo closed this issue 2026-02-16 18:15:52 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#11081