(messages: Message[])
| 162 | * Pads estimate by 4/3 to be conservative since we're approximating |
| 163 | */ |
| 164 | export function estimateMessageTokens(messages: Message[]): number { |
| 165 | let totalTokens = 0 |
| 166 | |
| 167 | for (const message of messages) { |
| 168 | if (message.type !== 'user' && message.type !== 'assistant') { |
| 169 | continue |
| 170 | } |
| 171 | |
| 172 | if (!Array.isArray(message.message.content)) { |
| 173 | continue |
| 174 | } |
| 175 | |
| 176 | for (const block of message.message.content) { |
| 177 | if (block.type === 'text') { |
| 178 | totalTokens += roughTokenCountEstimation(block.text) |
| 179 | } else if (block.type === 'tool_result') { |
| 180 | totalTokens += calculateToolResultTokens(block) |
| 181 | } else if (block.type === 'image' || block.type === 'document') { |
| 182 | totalTokens += IMAGE_MAX_TOKEN_SIZE |
| 183 | } else if (block.type === 'thinking') { |
| 184 | // Match roughTokenCountEstimationForBlock: count only the thinking |
| 185 | // text, not the JSON wrapper or signature (signature is metadata, |
| 186 | // not model-tokenized content). |
| 187 | totalTokens += roughTokenCountEstimation(block.thinking) |
| 188 | } else if (block.type === 'redacted_thinking') { |
| 189 | totalTokens += roughTokenCountEstimation(block.data) |
| 190 | } else if (block.type === 'tool_use') { |
| 191 | // Match roughTokenCountEstimationForBlock: count name + input, |
| 192 | // not the JSON wrapper or id field. |
| 193 | totalTokens += roughTokenCountEstimation( |
| 194 | block.name + jsonStringify(block.input ?? {}), |
| 195 | ) |
| 196 | } else { |
| 197 | // server_tool_use, web_search_tool_result, etc. |
| 198 | totalTokens += roughTokenCountEstimation(jsonStringify(block)) |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | // Pad estimate by 4/3 to be conservative since we're approximating |
| 204 | return Math.ceil(totalTokens * (4 / 3)) |
| 205 | } |
| 206 | |
| 207 | export type PendingCacheEdits = { |
| 208 | trigger: 'auto' |
no test coverage detected