fix(tokens): count attached images in Estimate (~1500 tokens each)
This commit is contained in:
1 parent
8c9bc2963d
commit
22f4329f44
2 files changed
+22
No files matched your search
@@ -21,6 +21,10 @@ func Estimate(msgs []llm.Message, systemPrompt string, tools []llm.ToolDef) int
|
||||
for _, tc := range m.ToolCalls {
|
||||
total += len(tc.ID) + len(tc.Name) + len(tc.Input)
|
||||
}
|
||||
// Images are not part of Content (Images is json:"-"); approximate
|
||||
// each attached image's token cost. perImageTokens is expressed in
|
||||
// tokens, so multiply by 4 to survive the final divide-by-4.
|
||||
total += len(m.Images) * perImageTokens * 4
|
||||
}
|
||||
for _, t := range tools {
|
||||
total += len(t.Name) + len(t.Description) + len(t.Parameters)
|
||||
@@ -33,6 +37,12 @@ func Estimate(msgs []llm.Message, systemPrompt string, tools []llm.ToolDef) int
|
||||
// since the final divide-by-4 maps it back to tokens.
|
||||
const perMessageOverhead = 3
|
||||
|
||||
// perImageTokens approximates the token cost of one attached image. Vision
|
||||
// models bill images by tiles/resolution; ~1500 tokens is a deliberately
|
||||
// conservative flat estimate so image-heavy sessions trigger preventive
|
||||
// compaction rather than hitting a reactive overflow.
|
||||
const perImageTokens = 1500
|
||||
|
||||
// ContextWindow returns the maximum input tokens for the given
|
||||
// "provider/model" identifier.
|
||||
//
|
||||
|
||||
Reference in new issue
Block a user