llm

package
v0.2.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 14, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

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

type CostCalculator func(model string, promptTokens, compTokens int) (float64, bool)

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)
}

func NewLLMHook

func NewLLMHook(opts LLMHookOptions) *LLMHook

func (*LLMHook) AsAnyHook

func (h *LLMHook) AsAnyHook() action.AnyHook

AsAnyHook exposes the LLM telemetrist as an active Kernel action hook.

func (*LLMHook) Emit

func (h *LLMHook) Emit(ctx context.Context, event observe.Event)

Emit implements kernel/observe.Sink so LLM telemetry can plug into observe.Hook directly.

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

type TokenCostProvider interface {
	Model() string
	PromptTokens() int
	CompletionTokens() int
}

TokenCostProvider is implemented by standard LLM responses.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL