llm

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 8 Imported by: 0

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.

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 {
	// contains filtered or unexported fields
}

FileSink appends one JSON object per line (JSONL) to an open file. 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.

func (*FileSink) Close

func (s *FileSink) Close() error

Close releases the underlying file.

func (*FileSink) Record

func (s *FileSink) Record(r TraceRecord)

Record writes one JSONL line. Encoding/IO errors are dropped — tracing must never break a run.

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