Analyze Context (Total Cost Estimation)
Instructions
Scope: Analyze ALL components that contribute to the context window and cost. This includes:
- Conversation History: User messages, Assistant responses, Tool calls, and Tool outputs.
- System Context:
- System Prompts: The core instructions defining the agent's behavior.
- Active Rules: Content from
AGENTS.mdand any active.cursor/rules. - Skill/Tool Definitions: The descriptions of available tools and skills (often a significant overhead).
- Attached Context: Any
<open_and_recently_viewed_files>,<git_status>, or other automatic context blocks.
Estimation Method:
- Calculate the token count based on the number of characters for each component.
- Formula:
Token Count = Character Count * Multiplier - Multiplier: Use 0.3 (approx. 1 token per 3.3 characters).
Procedure:
- Conversation: Estimate chars in the visible chat history.
- System/Rules: Read
AGENTS.mdand any active rule files to estimate their size. - Overhead: Add a heuristic buffer for Tool/Skill definitions and hidden system prompts (typically 2000-5000 chars or ~1000 tokens depending on the number of tools).
- Total: Sum all components.
- Report: Provide a breakdown of the estimated usage.
Tools:
- Use the script for calculation:
deno run scripts/count_tokens.ts "TEXT_snippet" - Or manual calculation:
Total Chars * 0.3.
- Use the script for calculation:
Examples
User: "How many tokens are we using?" Action:
- Chat: ~5000 chars (User + AI messages).
- System:
AGENTS.md(3000 chars) + Rules (1000 chars). - Overhead: ~3000 chars (Tool definitions).
- Total Chars: 12,000.
- Calc:
12,000 * 0.3 = 3,600 tokens. - Report:
"Estimated Context Usage: ~3,600 tokens
- Conversation: ~1,500 tokens
- System & Rules: ~1,200 tokens
- Tool Definitions: ~900 tokens"