(messages: Message[])
| 66 | Drop: |
| 67 | - Greetings, pleasantries, acknowledgments |
| 68 | - The agent's intermediate narrations ("let me look at..." / "now I'll...") |
| 69 | - Long tool outputs (replace with "[examined X]" if relevant) |
| 70 | - Code blocks longer than 3 lines (replace with a 1-line description) |
| 71 | |
| 72 | Output format: plain markdown, 200-500 tokens TOTAL. Start with "${SUMMARY_PREFIX}". |
| 73 | DO NOT explain what you're doing — just produce the summary.`; |
| 74 | |
| 75 | export function estimateTokens(messages: Message[]): number { |
| 76 | let total = 0; |
| 77 | for (const m of messages) { |
| 78 | const content: any = m.content; |
| 79 | if (typeof content === 'string') total += countTokens(content); |
| 80 | else if (Array.isArray(content)) { |
| 81 | for (const part of content) { |
| 82 | if (typeof part === 'object' && part && 'text' in part && typeof part.text === 'string') { |
| 83 | total += countTokens(part.text); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | if ('tool_calls' in m && Array.isArray((m as any).tool_calls)) { |
| 88 | for (const tc of (m as any).tool_calls) { |
| 89 | total += countTokensJson(tc); |
| 90 | } |
| 91 | } |
| 92 | // Per-message overhead (role tags, delimiters) — ~4 tokens each in chat formats. |
no test coverage detected