feat: enhance token estimation and context limit handling in strategy configurations

This commit is contained in:
Dean
2026-03-27 14:31:56 +08:00
committed by shinchan-zhai
parent 6cb6c31b34
commit 95e76f6a56
7 changed files with 78 additions and 56 deletions
+18 -15
View File
@@ -55,24 +55,27 @@ func GetFullDecisionWithStrategy(ctx *Context, mcpClient mcp.AIClient, engine *S
engineConfig := engine.GetConfig()
engineConfig.ClampLimits()
// Token estimation check — warn or block if exceeding all known model limits
// Token estimation check — block if exceeding the specific model's context limit
estimate := engineConfig.EstimateTokens()
allExceed := true
anyWarning := false
for _, ml := range estimate.ModelLimits {
if ml.UsagePct <= 100 {
allExceed = false
}
if ml.UsagePct >= 80 {
anyWarning = true
}
// Determine context limit for the specific model being used
contextLimit := 131072 // safe default (strictest common limit)
var providerName string
if embedder, ok := mcpClient.(mcp.ClientEmbedder); ok {
base := embedder.BaseClient()
providerName = base.Provider
contextLimit = store.GetContextLimitForClient(base.Provider, base.Model)
}
if allExceed && len(estimate.ModelLimits) > 0 {
logger.Errorf("🚫 Token estimate %d exceeds ALL known model context limits — blocking analysis", estimate.Total)
return nil, fmt.Errorf("estimated %d tokens exceeds all known model context limits; reduce coins, timeframes, or K-line count", estimate.Total)
if estimate.Total > contextLimit {
logger.Errorf("🚫 Token estimate %d exceeds %s context limit %d — blocking analysis",
estimate.Total, providerName, contextLimit)
return nil, fmt.Errorf("estimated %d tokens exceeds model context limit of %d; reduce coins, timeframes, or K-line count",
estimate.Total, contextLimit)
}
if anyWarning {
logger.Infof("⚠️ Token estimate %d — approaching context limits for some models", estimate.Total)
if estimate.Total*100/contextLimit >= 80 {
logger.Infof("⚠️ Token estimate %d — approaching %s context limit %d",
estimate.Total, providerName, contextLimit)
}
// 1. Fetch market data using strategy config