feat(llm): add prompt-cache observability and wiki cache-friendly prompts

Normalize provider-specific cache counters into TokenUsage, propagate call
purpose and prefix fingerprints through context for logging and Langfuse,
and restructure wiki page-modify prompts with warmup/singleflight to improve
prefix cache hit rates during reduce batches.
This commit is contained in:
wizardchen
2026-07-23 18:00:35 +08:00
committed by lyingbug
parent 6c794f64cf
commit 6fb858105a
36 changed files with 877 additions and 175 deletions
+19 -10
View File
@@ -25,15 +25,18 @@ func (l *langfuseChat) Chat(ctx context.Context, messages []Message, opts *ChatO
return l.inner.Chat(ctx, messages, opts)
}
purpose, prefixFingerprint := types.LLMCallMetadataFromContext(ctx)
genCtx, gen := mgr.StartGeneration(ctx, langfuse.GenerationOptions{
Name: "chat.completion",
Model: l.inner.GetModelName(),
Input: buildLangfuseMessages(messages),
ModelParameters: buildLangfuseModelParams(opts),
Metadata: map[string]interface{}{
"model_id": l.inner.GetModelID(),
"streaming": false,
"has_tools": opts != nil && len(opts.Tools) > 0,
"model_id": l.inner.GetModelID(),
"streaming": false,
"has_tools": opts != nil && len(opts.Tools) > 0,
"call_purpose": purpose,
"prompt_prefix_fingerprint": prefixFingerprint,
},
})
@@ -57,15 +60,18 @@ func (l *langfuseChat) ChatStream(ctx context.Context, messages []Message, opts
return l.inner.ChatStream(ctx, messages, opts)
}
purpose, prefixFingerprint := types.LLMCallMetadataFromContext(ctx)
genCtx, gen := mgr.StartGeneration(ctx, langfuse.GenerationOptions{
Name: "chat.completion.stream",
Model: l.inner.GetModelName(),
Input: buildLangfuseMessages(messages),
ModelParameters: buildLangfuseModelParams(opts),
Metadata: map[string]interface{}{
"model_id": l.inner.GetModelID(),
"streaming": true,
"has_tools": opts != nil && len(opts.Tools) > 0,
"model_id": l.inner.GetModelID(),
"streaming": true,
"has_tools": opts != nil && len(opts.Tools) > 0,
"call_purpose": purpose,
"prompt_prefix_fingerprint": prefixFingerprint,
},
})
@@ -211,10 +217,13 @@ func convertUsage(u *types.TokenUsage) *langfuse.TokenUsage {
return nil
}
return &langfuse.TokenUsage{
Input: u.PromptTokens,
Output: u.CompletionTokens,
Total: u.TotalTokens,
Unit: "TOKENS",
Input: u.PromptTokens,
Output: u.CompletionTokens,
Total: u.TotalTokens,
CacheRead: u.CacheReadTokens,
CacheWrite: u.CacheWriteTokens,
CacheMiss: u.CacheMissTokens,
Unit: "TOKENS",
}
}