llm

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package llm defines the export format for model-call telemetry. It contains no provider wrappers or agent plugins.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RecordBatch

func RecordBatch(batch export.Batch, sink Sink, meta Metadata)

RecordBatch exports the model-attempt events recorded by AgentCore. An attempt with a captured "llm.trace" payload becomes a full record; without one (payload capture is opt-in) the record is built from the attempt's operational attributes: model, usage, cost, latency and outcome, but no messages.

func TraceIDFrom

func TraceIDFrom(ctx context.Context) string

TraceIDFrom reads the correlation id set by WithTraceID ("" when absent, e.g. a classifier call made outside any run).

func WithTraceID

func WithTraceID(ctx context.Context, id string) context.Context

WithTraceID tags ctx with a correlation id the host stamps onto every TraceRecord produced under it. The consumer (e.g. the Runner) sets it to its run id just before driving the loop; an empty id is a no-op.

Types

type FileSink

type FileSink struct {
	// OnError, when set before the first Record, receives each write failure.
	OnError func(error)
	// contains filtered or unexported fields
}

FileSink appends one JSON object per line (JSONL) to a file only its owner can read, since records hold prompts and responses. Safe for concurrent runs. Close it when the process shuts down.

func NewFileSink

func NewFileSink(path string) (*FileSink, error)

NewFileSink opens (creating/appending) a JSONL trace file at path with mode 0600, tightening an existing file to it.

func (*FileSink) Close

func (s *FileSink) Close() error

Close releases the underlying file.

func (*FileSink) Dropped added in v0.2.0

func (s *FileSink) Dropped() int64

Dropped counts records that failed to write.

func (*FileSink) Record

func (s *FileSink) Record(r TraceRecord)

Record writes one JSONL line. A failure never breaks the run: it is counted in Dropped and passed to OnError.

type Metadata

type Metadata struct {
	TraceID, SessionKey string
	Depth               int
	PricingKnown        bool
}

type MultiSink

type MultiSink []Sink

MultiSink fans one record out to several sinks (e.g. file + stdout).

func (MultiSink) Record

func (m MultiSink) Record(r TraceRecord)

type Sink

type Sink interface {
	Record(TraceRecord)
}

Sink receives a TraceRecord per LLM call. Implementations fan out to a file, stdout, or an analytics store. It never computes or changes usage.

type SinkFunc

type SinkFunc func(TraceRecord)

SinkFunc adapts a function to a Sink.

func (SinkFunc) Record

func (f SinkFunc) Record(r TraceRecord)

type TraceRecord

type TraceRecord struct {
	// NativeTrace holds the authoritative native request, response, and spans.
	// Messages and Response remain a display projection for legacy consumers.
	NativeTrace json.RawMessage `json:"native_trace,omitempty"`
	TraceID     string          `json:"trace_id,omitempty"` // correlation id (the run id), set via WithTraceID
	// SessionKey identifies WHICH agent made the call — the run's own session,
	// or a spawned child's derived session. A parent and its children share one
	// provider and one ctx chain, so without this their calls are one
	// undifferentiated stream. Empty outside a durable run.
	SessionKey string `json:"session_key,omitempty"`
	// Depth is the delegation depth of the caller: 0 for the top-level run.
	Depth     int                `json:"depth,omitempty"`
	Timestamp time.Time          `json:"timestamp"`
	Provider  string             `json:"provider"`
	Model     string             `json:"model"`
	Messages  []protocol.Message `json:"messages"`           // the request sent to the model
	Tools     []string           `json:"tools,omitempty"`    // tool names advertised this turn
	Response  string             `json:"response,omitempty"` // assistant text returned
	// ReasoningBlocks carries opaque provider replay state (for example,
	// Anthropic signed thinking). It is recorded separately from Response so it
	// can be replayed without ever rendering it as user-visible assistant text.
	ReasoningBlocks []protocol.ReasoningBlock `json:"reasoning_blocks,omitempty"`
	ToolCalls       []protocol.ToolCall       `json:"tool_calls,omitempty"` // tool calls the model requested
	StopReason      string                    `json:"stop_reason,omitempty"`
	Usage           protocol.Usage            `json:"usage"` // tokens + computed CostUSD
	LatencyMS       int64                     `json:"latency_ms"`
	Streamed        bool                      `json:"streamed"`
	Err             string                    `json:"error,omitempty"`
}

TraceRecord is one observed LLM call: what was sent, what came back, what it cost, and how long it took. It is the durable, debuggable unit behind an agent run — the "message sent to the LLM + est. fee" trace. Emitted once per Chat or streamed turn.

func ParseNative

func ParseNative(raw json.RawMessage, meta Metadata) (TraceRecord, error)

ParseNative projects native telemetry for storage/display. NativeTrace keeps the original request and response; projections must never restore history.

Jump to

Keyboard shortcuts

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