fix(ai): report the configured budget in context clamp warnings

The warning was computed after the runtime config had already been clamped,
so configured and effective values were identical and the clamp warning was lost.
This commit is contained in:
Sycun committed 2026-10-21 00:16:30 +08:00
1 parent 0be25994a3
commit 0d97169d07
4 files changed
+77 -8

No files matched your search

+14 -6
View File
@@ -17,23 +17,31 @@ import (
var contextBudgetWarned sync.Map
func logContextBudgetWarnings(oa config.OpenAIConfig, logger *zap.Logger, phase string) {
// logContextBudgetWarnings reports the clamp applied by WithEffectiveContextBudget.
// configured must be the pre-clamp channel config so the warning set and the
// configured value stay meaningful.
func logContextBudgetWarnings(configured, effective config.OpenAIConfig, logger *zap.Logger, phase string) {
if logger == nil {
return
}
warnings := oa.ContextBudgetWarnings()
warnings := configured.ContextBudgetWarnings()
if len(warnings) == 0 {
return
}
key := fmt.Sprintf("%d|%d|%s", oa.EffectiveContextWindow(), oa.MaxTotalTokens, strings.Join(warnings, ","))
key := fmt.Sprintf("%d|%d|%d|%s",
configured.EffectiveContextWindow(),
configured.MaxTotalTokens,
effective.MaxTotalTokens,
strings.Join(warnings, ","),
)
if _, loaded := contextBudgetWarned.LoadOrStore(key, struct{}{}); loaded {
return
}
logger.Warn("model context budget adjusted",
zap.String("phase", phase),
zap.Int("context_window", oa.EffectiveContextWindow()),
zap.Int("configured_max_total_tokens", oa.MaxTotalTokens),
zap.Int("effective_max_total_tokens", oa.EffectiveMaxTotalTokens()),
zap.Int("context_window", configured.EffectiveContextWindow()),
zap.Int("configured_max_total_tokens", configured.MaxTotalTokens),
zap.Int("effective_max_total_tokens", effective.MaxTotalTokens),
zap.Strings("warnings", warnings),
)
}
@@ -11,6 +11,9 @@ import (
"github.com/cloudwego/eino/adk"
"github.com/cloudwego/eino/adk/middlewares/summarization"
"github.com/cloudwego/eino/schema"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
)
func TestIsEinoContextOverflowError(t *testing.T) {
@@ -89,6 +92,62 @@ func TestBuildBudgetedSummarizationModelInputTruncatesOversizedLatestRound(t *te
}
}
func TestLogContextBudgetWarningsKeepsConfiguredValue(t *testing.T) {
t.Parallel()
core, logs := observer.New(zapcore.WarnLevel)
logger := zap.New(core)
configured := config.OpenAIConfig{
ContextWindow: 131073,
ContextProfile: "local",
MaxTotalTokens: 120000,
}
effective := configured
effective.MaxTotalTokens = configured.EffectiveMaxTotalTokens()
logContextBudgetWarnings(configured, effective, logger, "test")
entries := logs.All()
if len(entries) != 1 {
t.Fatalf("warn entries = %d, want 1", len(entries))
}
fields := entries[0].ContextMap()
if got, ok := fields["configured_max_total_tokens"].(int64); !ok || got != 120000 {
t.Fatalf("configured_max_total_tokens = %#v, want 120000", fields["configured_max_total_tokens"])
}
if got, ok := fields["effective_max_total_tokens"].(int64); !ok || got != 78643 {
t.Fatalf("effective_max_total_tokens = %#v, want 78643", fields["effective_max_total_tokens"])
}
warnings := warningCodes(fields["warnings"])
if !hasWarningCode(warnings, config.ContextWarningMaxTotalClamped) {
t.Fatalf("warnings = %#v, want clamp warning", fields["warnings"])
}
}
func warningCodes(value interface{}) []string {
switch v := value.(type) {
case []string:
return v
case []interface{}:
out := make([]string, 0, len(v))
for _, item := range v {
if code, ok := item.(string); ok {
out = append(out, code)
}
}
return out
default:
return nil
}
}
func hasWarningCode(items []string, want string) bool {
for _, item := range items {
if item == want {
return true
}
}
return false
}
func TestLocalTokenCounterSafetyMargin(t *testing.T) {
t.Parallel()
input := &summarization.TokenCounterInput{Messages: []adk.Message{
+2 -1
View File
@@ -45,13 +45,14 @@ func RunEinoSingleChatModelAgent(
if ma == nil {
return nil, fmt.Errorf("eino single: multi_agent 配置为空")
}
configuredOpenAI := appCfg.OpenAI
appCfg = appCfg.WithEffectiveContextBudget()
{
maCopy := *ma
maCopy.EinoMiddleware = appCfg.MultiAgent.EinoMiddleware
ma = &maCopy
}
logContextBudgetWarnings(appCfg.OpenAI, logger, "eino_single")
logContextBudgetWarnings(configuredOpenAI, appCfg.OpenAI, logger, "eino_single")
runtimeUserMessage := prepareLatestUserMessageForModel(userMessage, appCfg, &ma.EinoMiddleware, conversationID, logger)
+2 -1
View File
@@ -84,13 +84,14 @@ func RunDeepAgent(
return nil, fmt.Errorf("multiagent: 配置或 Agent 为空")
}
configuredOpenAI := appCfg.OpenAI
appCfg = appCfg.WithEffectiveContextBudget()
if ma != nil {
maCopy := *ma
maCopy.EinoMiddleware = appCfg.MultiAgent.EinoMiddleware
ma = &maCopy
}
logContextBudgetWarnings(appCfg.OpenAI, logger, "deep")
logContextBudgetWarnings(configuredOpenAI, appCfg.OpenAI, logger, "deep")
runtimeUserMessage := prepareLatestUserMessageForModel(userMessage, appCfg, &ma.EinoMiddleware, conversationID, logger)