usage

package
v0.7.9 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package usage decorates an elelem.Driver so every call emits LLM metrics and best-effort persists one llm_usage row.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithAttribution

func WithAttribution(
	ctx context.Context,
	chatID, userID uuid.UUID,
) context.Context

WithAttribution returns a context carrying the owning chat + user id, so a usage row recorded during a Stream call under this context is attributed to them. The chat loop stamps this before invoking the wrapped client.

func Wrap

func Wrap(
	inner elelem.Driver,
	m *metrics.Metrics,
	stage string,
	persist bool,
) elelem.Driver

Types

type Recorder

type Recorder struct {
	// contains filtered or unexported fields
}

Recorder wraps an elelem.Driver with metrics and usage persistence.

func (*Recorder) Capabilities

func (r *Recorder) Capabilities(model elelem.Model) elelem.Capabilities

Capabilities passes through to the inner driver.

func (*Recorder) Complete

func (r *Recorder) Complete(
	ctx context.Context,
	req elelem.DriverRequest,
	onDelta func(elelem.Delta) error,
) (elelem.Usage, error)

Complete does the same for the non-streaming call, which elelem takes when streaming is off or the provider cannot stream.

It must be recorded identically: the tokens are spent and the row is owed either way, and a decorator that only instrumented Stream would make a backend's usage silently vanish from the metrics the moment someone set WithStreaming(false).

func (*Recorder) ListModels

func (r *Recorder) ListModels(ctx context.Context) ([]string, error)

ListModels passes through to the inner client.

func (*Recorder) Stream

func (r *Recorder) Stream(
	ctx context.Context,
	req elelem.DriverRequest,
	onDelta func(elelem.Delta) error,
) (elelem.Usage, error)

Stream runs the inner streaming call, records metrics + a usage row, and passes the result through unchanged.

func (*Recorder) TokenCounter

func (r *Recorder) TokenCounter() elelem.TokenCounter

Jump to

Keyboard shortcuts

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