modelusage

package
v2.24.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: GPL-3.0 Imports: 6 Imported by: 0

Documentation

Overview

Package modelusage keeps what a step's calls to models use while the step runs. A step's context carries its meter, and every model call made with that context adds to it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SubRunSource

func SubRunSource(runID string) string

SubRunSource names a child run as the source of the usage its parent step reports, so each child keeps one entry however often it reports.

func WithMeter

func WithMeter(ctx context.Context, m *Meter) context.Context

WithMeter returns a context whose model calls add to m. A nil m hides any meter ctx carries, so calls made with the returned context count nowhere.

Types

type Flusher

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

Flusher reports a meter's usage while its step runs, at most once an interval and only after the usage changed since the last report, so a step that calls no model never reports.

func (*Flusher) Stop

func (f *Flusher) Stop()

Stop ends the reports. When it returns, no report is under way and none will start.

type Meter

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

Meter adds up a step's calls to models, on top of what the step's earlier attempts used. Its methods do nothing on a nil meter, so an executor run without one needs no checks.

func FromContext

func FromContext(ctx context.Context) *Meter

FromContext returns the meter ctx carries, or nil.

func NewMeter

func NewMeter(base *ir.StepModelUsage) *Meter

NewMeter returns a meter that starts from what the step used before, which may be nil.

func (*Meter) Add

func (m *Meter) Add(models []ir.ModelUsage)

Add adds usage the step's own work spent elsewhere, such as the body of a foreach item, once that work is done.

func (*Meter) MarkNotReported

func (m *Meter) MarkNotReported(tool string)

MarkNotReported records that the step ran tool, whose model usage cannot be read.

func (*Meter) RecordLLMUsage

func (m *Meter) RecordLLMUsage(record llm.UsageRecord)

RecordLLMUsage adds one call.

func (*Meter) SetReported

func (m *Meter) SetReported(source string, models []ir.ModelUsage)

SetReported records source's latest total, as a coding agent's session reports it, replacing the one before. A source the step resumes keeps its entry, so what it used before is not counted twice.

func (*Meter) Snapshot

func (m *Meter) Snapshot() *ir.StepModelUsage

Snapshot returns what the step has used so far, or nil when it has used nothing and reported nothing.

func (*Meter) StartFlusher

func (m *Meter) StartFlusher(every time.Duration, report func(*ir.StepModelUsage)) *Flusher

StartFlusher has report called with the meter's usage, at most once every interval, after the meter records a call. Stop it before the step ends.

Jump to

Keyboard shortcuts

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