fix(usage): enforce caps on auxiliary llm calls

This commit is contained in:
Goon committed 2026-05-24 11:13:03 +07:00
1 parent e4acb147d6
commit d64a31ebdb
39 files changed
+701 -157

No files matched your search

+17 -3
View File
@@ -9,6 +9,7 @@ import (
"github.com/nextlevelbuilder/goclaw/internal/providers"
"github.com/nextlevelbuilder/goclaw/internal/store"
usagecaps "github.com/nextlevelbuilder/goclaw/internal/usage/caps"
)
// ExtractionResult holds entities and relations extracted from text.
@@ -22,6 +23,7 @@ type Extractor struct {
provider providers.Provider
model string
minConfidence float64
usageCaps *usagecaps.Service
}
// NewExtractor creates a new Extractor with the given provider, model, and confidence threshold.
@@ -32,6 +34,11 @@ func NewExtractor(provider providers.Provider, model string, minConfidence float
return &Extractor{provider: provider, model: model, minConfidence: minConfidence}
}
// SetUsageCapService enables cost enforcement for LLM extraction calls.
func (e *Extractor) SetUsageCapService(s *usagecaps.Service) {
e.usageCaps = s
}
const maxChunkChars = 12000
// Extract calls the LLM to extract entities and relations from text.
@@ -72,7 +79,11 @@ func (e *Extractor) extractChunk(ctx context.Context, text string) (*ExtractionR
},
}
resp, err := e.provider.Chat(ctx, req)
resp, err := e.usageCaps.Chat(ctx, e.provider, req, usagecaps.ChatOptions{
ModelID: e.model,
Purpose: "knowledge-graph-extract",
MaxOutputTokens: 8192,
})
if err != nil {
return nil, fmt.Errorf("kg extraction LLM call: %w", err)
}
@@ -85,7 +96,11 @@ func (e *Extractor) extractChunk(ctx context.Context, text string) (*ExtractionR
text = text[:retryMaxChars] + "\n\n[...truncated]"
}
req.Messages[1].Content = text
resp, err = e.provider.Chat(ctx, req)
resp, err = e.usageCaps.Chat(ctx, e.provider, req, usagecaps.ChatOptions{
ModelID: e.model,
Purpose: "knowledge-graph-extract-retry",
MaxOutputTokens: 8192,
})
if err != nil {
return nil, fmt.Errorf("kg extraction LLM retry: %w", err)
}
@@ -286,4 +301,3 @@ func stripCodeBlock(s string) string {
}
return strings.TrimSpace(s)
}