( chatHistory: ChatHistoryItem[], model: ModelConfig, )
| 175 | * @returns The estimated total token count |
| 176 | */ |
| 177 | export function countChatHistoryTokens( |
| 178 | chatHistory: ChatHistoryItem[], |
| 179 | model: ModelConfig, |
| 180 | ): number { |
| 181 | let totalTokens = 0; |
| 182 | |
| 183 | for (const historyItem of chatHistory) { |
| 184 | totalTokens += countChatHistoryItemTokens(historyItem, model); |
| 185 | } |
| 186 | |
| 187 | // Add some overhead for message structure (roughly 3 tokens per message) |
| 188 | totalTokens += chatHistory.length * 3; |
| 189 | |
| 190 | return totalTokens; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Calculate the percentage of context used |
no test coverage detected