Documentation
¶
Overview ¶
Package llm provides OpenTelemetry and Prometheus instrumentation for LLM calls.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CachedTokenCostProvider ¶
type CachedTokenCostProvider interface {
TokenCostProvider
CachedPromptTokens() int
}
CachedTokenCostProvider is implemented by modern LLM responses supporting prompt caching (DeepSeek V3, Claude 3.5, GPT-4o).
type CostCalculator ¶
CostCalculator calculates basic cost in USD.
type DetailedCostCalculator ¶
type DetailedCostCalculator func(model string, promptTokens, cachedTokens, compTokens int) (float64, bool)
DetailedCostCalculator calculates exact cost factoring in prompt cache discounts.
type LLMHook ¶
type LLMHook struct {
// contains filtered or unexported fields
}
Example ¶
package main
import (
"context"
"log/slog"
"github.com/nexssp/kernel/action"
"github.com/nexssp/observability/llm"
)
type MyResponse struct {
model string
prompt int
comp int
}
func (r MyResponse) Model() string { return r.model }
func (r MyResponse) PromptTokens() int { return r.prompt }
func (r MyResponse) CompletionTokens() int { return r.comp }
func main() {
// Suppose you have an LLM response type that implements TokenCostProvider.
// Define cost calculator (e.g., using ai/llm.CalculateCost).
costCalc := func(model string, prompt, comp int) (float64, bool) {
// Example pricing: $0.01 per 1k tokens for both.
return (float64(prompt) + float64(comp)) * 0.00001, true
}
// Create hook.
hook := llm.NewLLMHook(llm.LLMHookOptions{
CostCalculator: costCalc,
Logger: slog.Default(),
})
// Register hook with an action (pseudo-code).
// act.AddAnyHook(hook.AsAnyHook())
// Simulate execution.
ctx := context.Background()
meta := &action.Meta{Name: "my_action"}
res := MyResponse{model: "gpt-4", prompt: 100, comp: 50}
// The hook's After method would be called automatically by the kernel.
hook.AsAnyHook().After(ctx, nil, res, nil, meta)
}
Output:
func NewLLMHook ¶
func NewLLMHook(opts LLMHookOptions) *LLMHook
type LLMHookOptions ¶
type LLMHookOptions struct {
Metrics *Metrics
CostCalculator CostCalculator
DetailedCostCalculator DetailedCostCalculator
Logger *slog.Logger
}
type Metrics ¶
type Metrics struct {
PromptTokensTotal *prometheus.CounterVec
CachedPromptTokensTotal *prometheus.CounterVec
CompletionTokensTotal *prometheus.CounterVec
CostUSDTotal *prometheus.CounterVec
LLMDurationSeconds *prometheus.HistogramVec
}
func NewMetrics ¶
func NewMetrics(reg prometheus.Registerer, namespace, subsystem string) *Metrics
type TokenCostProvider ¶
TokenCostProvider is implemented by standard LLM responses.
Click to show internal directories.
Click to hide internal directories.