llm

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2026 License: Apache-2.0 Imports: 12 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 Hook added in v0.3.0

type Hook 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() {
	costCalc := func(_ string, prompt, comp int) (float64, bool) {
		// Example pricing: $0.01 per 1k tokens for both.
		return (float64(prompt) + float64(comp)) * 0.00001, true
	}

	hook := llm.NewLLMHook(llm.HookOptions{
		CostCalculator: costCalc,
		Logger:         slog.Default(),
	})

	ctx := context.Background()
	meta := &action.Meta{Name: "my_action"}
	res := MyResponse{model: "gpt-4", prompt: 100, comp: 50}

	hook.AsAnyHook().After(ctx, nil, res, nil, meta)
}

func NewLLMHook

func NewLLMHook(opts HookOptions) *Hook

func (*Hook) AsAnyHook added in v0.3.0

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

AsAnyHook exposes the LLM telemetrist as an active Kernel action hook. The hook drops itself at build time for actions whose response type does not implement TokenCostProvider, so non-LLM actions pay zero cost.

func (*Hook) Emit added in v0.3.0

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

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

type HookOptions added in v0.3.0

type HookOptions 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