core

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2026 License: MIT Imports: 22 Imported by: 5

Documentation

Overview

Package core is a production-grade Go agent framework for building LLM-powered agents with structured output, tool use, streaming, and multi-provider support.

The core type is Agent[T], a generic agent that uses an LLM to produce typed output of type T. Agents can be configured with tools (via FuncTool), system prompts, output validators, and usage limits.

Sub-packages

Leaf functionality is organized into sub-packages:

  • core/orchestration: Multi-agent coordination (AgentTool, Handoff, Pipeline, ChainRun)
  • core/streamutil: Stream text helpers (StreamText, StreamTextDelta, StreamTextDebounced)
  • core/memory: Memory strategies (SlidingWindowMemory, TokenBudgetMemory, SummaryMemory)

Basic Usage

model := anthropic.New()
agent := core.NewAgent[MyOutput](model,
    core.WithSystemPrompt[MyOutput]("You are helpful."),
    core.WithTools[MyOutput](myTool),
)
result, err := agent.Run(ctx, "user prompt")

Providers

Gollem supports multiple LLM providers through the Model interface:

  • provider/anthropic: Anthropic Claude models
  • provider/openai: OpenAI GPT and O-series models
  • provider/vertexai: Google Gemini via Vertex AI
  • provider/vertexai_anthropic: Claude via Vertex AI

Tools

Use FuncTool to create type-safe tools from Go functions:

tool := core.FuncTool[MyParams]("name", "description",
    func(ctx context.Context, p MyParams) (string, error) { ... })

Streaming

Use RunStream for real-time token streaming:

stream, _ := agent.RunStream(ctx, "prompt")
for text, err := range stream.StreamText(true) { ... }

Package gollem provides a production-grade Go agent framework for building LLM-powered agents with structured outputs, tool use, streaming, and multi-provider support.

Index

Examples

Constants

View Source
const (
	CompactionStrategyAutoSummary         = "auto_summary"
	CompactionStrategyHistoryProcessor    = "history_processor"
	CompactionStrategyEmergencyTruncation = "emergency_truncation"
)

Compaction strategy constants identify which mechanism performed the compression.

View Source
const (
	// DefaultOutputToolName is the name of the synthetic tool used to extract structured output.
	DefaultOutputToolName = "final_result"
	// DefaultOutputToolDescription is the description for the output tool.
	DefaultOutputToolDescription = "The final response which ends this conversation"
)
View Source
const (
	// RuntimeEventTypeRunStarted marks a run-start lifecycle event.
	RuntimeEventTypeRunStarted = "run_started"
	// RuntimeEventTypeRunCompleted marks a run-complete lifecycle event.
	RuntimeEventTypeRunCompleted = "run_completed"
	// RuntimeEventTypeToolCalled marks a tool-start lifecycle event.
	RuntimeEventTypeToolCalled = "tool_called"
)

Variables

View Source
var ErrAgentRunClosed = errors.New("agent run closed before completion")

ErrAgentRunClosed is returned when an iterative run is closed before it reaches a terminal model result.

Functions

func BinaryContent

func BinaryContent(data []byte, mimeType string) string

BinaryContent creates a data: URI from raw bytes and MIME type.

func BuildInstructionPin added in v0.1.3

func BuildInstructionPin(messages []ModelMessage) string

BuildInstructionPin returns a compact reminder block that re-pins the core instructions from the conversation: - system prompts from the first message - original task/user instruction from the first message - latest user instruction from the conversation tail

This is used by context compression paths so critical instructions remain salient even after large middle sections are summarized away.

func ContextWithCompactionCallback added in v0.1.7

func ContextWithCompactionCallback(ctx context.Context, cb CompactionCallback) context.Context

ContextWithCompactionCallback returns a context carrying a compaction callback. The agent framework injects this so middleware (e.g., ContextOverflowMiddleware) can report emergency compression events to hooks.

func ContextWithRunID added in v0.1.7

func ContextWithRunID(ctx context.Context, runID string) context.Context

ContextWithRunID returns a context carrying the agent's RunID. The agent framework injects this automatically at the start of Run.

func ContextWithToolCallID added in v0.1.7

func ContextWithToolCallID(ctx context.Context, toolCallID string) context.Context

ContextWithToolCallID returns a context carrying the active tool call ID. The agent framework injects this before each tool handler execution.

func EstimateTokens added in v0.1.7

func EstimateTokens(messages []ModelMessage) int

EstimateTokens estimates the token count of messages using a simple word-based heuristic. Uses ~1.3 tokens per word as a rough approximation. Exported so that middleware (e.g., ContextOverflowMiddleware) can compute before/after token counts for compaction reporting.

func GetDeps

func GetDeps[D any](rc *RunContext) D

GetDeps extracts typed dependencies from RunContext. Panics if deps are nil or the wrong type.

func IntPtr

func IntPtr(n int) *int

IntPtr returns a pointer to the given int. Convenience for setting limit fields.

func IsObjectSchema

func IsObjectSchema(s Schema) bool

IsObjectSchema returns true if the schema describes an object type.

func MarshalMessages

func MarshalMessages(messages []ModelMessage) ([]byte, error)

MarshalMessages serializes a conversation ([]ModelMessage) to JSON.

func MarshalSnapshot

func MarshalSnapshot(snap *RunSnapshot) ([]byte, error)

MarshalSnapshot serializes a snapshot to JSON using the message serialization API.

func Publish

func Publish[E any](bus *EventBus, event E)

Publish sends an event to all matching subscribers synchronously.

func PublishAsync

func PublishAsync[E any](bus *EventBus, event E)

PublishAsync sends an event to subscribers asynchronously via the bus queue. Delivery preserves async event order per bus but does not wait for handlers to run.

func RunIDFromContext added in v0.1.7

func RunIDFromContext(ctx context.Context) string

RunIDFromContext extracts the current agent's RunID from the context. Returns "" if no RunID is set.

func Subscribe

func Subscribe[E any](bus *EventBus, handler func(E)) func()

Subscribe registers a handler for events of a specific type. Returns an unsubscribe function.

func ToolCallIDFromContext added in v0.1.7

func ToolCallIDFromContext(ctx context.Context) string

ToolCallIDFromContext extracts the active tool call ID from the context. Returns "" if no tool call ID is set.

func TruncateToolOutput added in v0.1.9

func TruncateToolOutput(content string, config TruncationConfig) string

TruncateToolOutput applies head/tail truncation to tool output text. It keeps the first 60% and last 40% of the token budget, dropping the middle with a marker indicating how many tokens were removed. Returns the original string if within budget.

func TryGetDeps

func TryGetDeps[D any](rc *RunContext) (D, bool)

TryGetDeps extracts typed dependencies, returning ok=false if not available or wrong type.

Types

type Agent

type Agent[T any] struct {
	// contains filtered or unexported fields
}

Agent is the central type for running LLM interactions with structured output.

func NewAgent

func NewAgent[T any](model Model, opts ...AgentOption[T]) *Agent[T]

NewAgent creates a new Agent with the given model and options.

Example
package main

import (
	"context"
	"fmt"

	"github.com/fugue-labs/gollem/core"
)

func main() {
	model := core.NewTestModel(core.TextResponse("Hello!"))
	agent := core.NewAgent[string](model,
		core.WithSystemPrompt[string]("You are helpful."),
	)

	result, err := agent.Run(context.Background(), "Hi")
	if err != nil {
		panic(err)
	}
	fmt.Println(result.Output)
}
Output:
Hello!

func (*Agent[T]) Clone

func (a *Agent[T]) Clone(opts ...AgentOption[T]) *Agent[T]

Clone creates a copy of the agent with additional options applied. The original agent is not modified.

func (*Agent[T]) ExecutionFeatures added in v0.2.0

func (a *Agent[T]) ExecutionFeatures() AgentExecutionFeatures

ExecutionFeatures returns a summary of optional execution features configured on the agent and its toolsets.

func (*Agent[T]) GetModel

func (a *Agent[T]) GetModel() Model

GetModel returns the agent's model.

func (*Agent[T]) GetTools

func (a *Agent[T]) GetTools() []Tool

GetTools returns the agent's direct tools (not including toolset tools).

func (*Agent[T]) Iter

func (a *Agent[T]) Iter(ctx context.Context, prompt string, opts ...RunOption) *AgentRun[T]

Iter starts an agent run that can be iterated step-by-step. The run lifecycle begins on the first call to Next. If the caller stops iterating before completion, it should call Close to end the run cleanly.

func (*Agent[T]) Override

func (a *Agent[T]) Override(model Model, opts ...AgentOption[T]) *Agent[T]

Override creates a test-scoped override of the agent. The original agent is not modified. The returned override has its own model and options but shares the same configuration.

func (*Agent[T]) Run

func (a *Agent[T]) Run(ctx context.Context, prompt string, opts ...RunOption) (*RunResult[T], error)

Run executes the agent loop synchronously and returns the final result.

Example
package main

import (
	"context"
	"fmt"

	"github.com/fugue-labs/gollem/core"
)

func main() {
	type Result struct {
		Answer string `json:"answer"`
	}

	model := core.NewTestModel(
		core.ToolCallResponse("final_result", `{"answer":"42"}`),
	)
	agent := core.NewAgent[Result](model)

	result, err := agent.Run(context.Background(), "What is the answer?")
	if err != nil {
		panic(err)
	}
	fmt.Println(result.Output.Answer)
}
Output:
42

func (*Agent[T]) RunBatch

func (a *Agent[T]) RunBatch(ctx context.Context, prompts []string, opts ...RunOption) []BatchResult[T]

RunBatch executes multiple prompts through the agent concurrently. Results are returned in the same order as prompts. Concurrency defaults to GOMAXPROCS but can be limited via WithBatchConcurrency.

func (*Agent[T]) RunStream

func (a *Agent[T]) RunStream(ctx context.Context, prompt string, opts ...RunOption) (*StreamResult[T], error)

RunStream executes the agent with streaming output.

Example
package main

import (
	"context"
	"fmt"

	"github.com/fugue-labs/gollem/core"
)

func main() {
	model := core.NewTestModel(core.TextResponse("streaming response"))
	agent := core.NewAgent[string](model)

	stream, err := agent.RunStream(context.Background(), "Hello")
	if err != nil {
		panic(err)
	}
	defer stream.Close()

	resp, err := stream.GetOutput()
	if err != nil {
		panic(err)
	}
	fmt.Println(resp.TextContent())
}
Output:
streaming response

func (*Agent[T]) RuntimeConfig added in v0.2.0

func (a *Agent[T]) RuntimeConfig() AgentRuntimeConfig[T]

RuntimeConfig returns a copy of the agent configuration needed to reproduce the supported execution subset outside core.Agent.Run.

func (*Agent[T]) WithTestModel

func (a *Agent[T]) WithTestModel(responses ...*ModelResponse) (*Agent[T], *TestModel)

WithTestModel is a convenience that creates an agent override with a TestModel pre-configured with the given responses.

type AgentExecutionFeatures added in v0.2.0

type AgentExecutionFeatures struct {
	DynamicSystemPrompts       int
	HistoryProcessors          int
	Toolsets                   int
	HasAgentToolsPrepare       bool
	ToolsWithPrepareFunc       int
	ToolsWithResultValidator   int
	ToolsRequiringApproval     int
	HasToolApprovalFunc        bool
	Hooks                      int
	InputGuardrails            int
	TurnGuardrails             int
	OutputValidators           int
	HasOutputRepair            bool
	GlobalToolResultValidators int
	RunConditions              int
	TraceExporters             int
	HasEventBus                bool
	HasAgentDeps               bool
	MessageInterceptors        int
	ResponseInterceptors       int
	HasKnowledgeBase           bool
	HasKnowledgeAutoStore      bool
	HasCostTracker             bool
	HasAutoContext             bool
	HasUsageQuota              bool
	RequestMiddleware          int
	StreamMiddleware           int
}

AgentExecutionFeatures summarizes the optional execution features configured on an agent. Extensions can use this to conservatively reject configurations they cannot faithfully support.

type AgentMiddleware

type AgentMiddleware struct {
	Request RequestMiddlewareFunc
	Stream  AgentStreamMiddleware
}

AgentMiddleware bundles request and stream wrappers behind one registration API. Either side may be nil.

func DualMiddleware added in v0.1.10

func DualMiddleware(request RequestMiddlewareFunc, stream AgentStreamMiddleware) AgentMiddleware

DualMiddleware registers both request and stream middleware with one value.

func LoggingMiddleware

func LoggingMiddleware(logger func(msg string)) AgentMiddleware

LoggingMiddleware logs model request/response summaries.

func MaxTokensMiddleware

func MaxTokensMiddleware(maxTokens int) AgentMiddleware

MaxTokensMiddleware limits the number of tokens per request via ModelSettings.

func RequestOnlyMiddleware added in v0.1.10

func RequestOnlyMiddleware(mw RequestMiddlewareFunc) AgentMiddleware

RequestOnlyMiddleware registers middleware only for Request/Run/Iter paths.

func StreamOnlyMiddleware added in v0.1.10

func StreamOnlyMiddleware(mw AgentStreamMiddleware) AgentMiddleware

StreamOnlyMiddleware registers middleware only for RequestStream/RunStream paths.

func TimingMiddleware

func TimingMiddleware(callback func(duration time.Duration)) AgentMiddleware

TimingMiddleware records request duration and calls a callback.

func (AgentMiddleware) IsZero added in v0.1.10

func (m AgentMiddleware) IsZero() bool

IsZero reports whether neither request nor stream middleware is configured.

type AgentOption

type AgentOption[T any] func(*Agent[T])

AgentOption configures the agent via functional options.

func WithAgentMiddleware

func WithAgentMiddleware[T any](mw AgentMiddleware) AgentOption[T]

WithAgentMiddleware adds middleware to the agent's model call chain. Middleware is applied in order (first registered = outermost wrapper).

func WithAgentStreamMiddleware added in v0.1.10

func WithAgentStreamMiddleware[T any](mw AgentStreamMiddleware) AgentOption[T]

WithAgentStreamMiddleware adds middleware only to the agent's streaming model call chain. Prefer WithAgentMiddleware(StreamOnlyMiddleware(...)) for new code.

func WithAutoContext

func WithAutoContext[T any](config AutoContextConfig) AgentOption[T]

WithAutoContext enables automatic context window management. When estimated tokens exceed MaxTokens, older messages are summarized.

func WithCostTracker

func WithCostTracker[T any](tracker *CostTracker) AgentOption[T]

WithCostTracker attaches a cost tracker to the agent. Run costs are available on RunResult.Cost after the run.

func WithDefaultToolTimeout

func WithDefaultToolTimeout[T any](d time.Duration) AgentOption[T]

WithDefaultToolTimeout sets a default timeout for all tools that don't have their own timeout configured.

func WithDeps

func WithDeps[T any](deps any) AgentOption[T]

WithDeps sets the dependency value for the agent run. The dependency is accessible via GetDeps[D](rc) in tools.

func WithDynamicSystemPrompt

func WithDynamicSystemPrompt[T any](fn SystemPromptFunc) AgentOption[T]

WithDynamicSystemPrompt adds a function that generates system prompts at runtime.

func WithEndStrategy

func WithEndStrategy[T any](s EndStrategy) AgentOption[T]

WithEndStrategy sets the end strategy for the agent.

func WithEventBus

func WithEventBus[T any](bus *EventBus) AgentOption[T]

WithEventBus attaches an event bus to the agent, accessible via RunContext.

func WithGlobalToolResultValidator

func WithGlobalToolResultValidator[T any](fn ToolResultValidatorFunc) AgentOption[T]

WithGlobalToolResultValidator adds a validator that runs on all tool results.

func WithHistoryProcessor

func WithHistoryProcessor[T any](proc HistoryProcessor) AgentOption[T]

WithHistoryProcessor adds a processor that transforms message history before each model request.

func WithHooks

func WithHooks[T any](hooks ...Hook) AgentOption[T]

WithHooks adds lifecycle hooks to the agent. Multiple hooks can be added; all fire in registration order.

func WithInputGuardrail

func WithInputGuardrail[T any](name string, fn InputGuardrailFunc) AgentOption[T]

WithInputGuardrail adds an input guardrail that runs before the agent loop.

func WithKnowledgeBase

func WithKnowledgeBase[T any](kb KnowledgeBase) AgentOption[T]

WithKnowledgeBase injects a knowledge system into the agent. When set, the agent will call Retrieve before each run and prepend any returned context as a system prompt.

func WithKnowledgeBaseAutoStore

func WithKnowledgeBaseAutoStore[T any]() AgentOption[T]

WithKnowledgeBaseAutoStore enables automatic storage of successful agent responses. When enabled along with a KnowledgeBase, the agent will call Store with the text content of the model's final response after a successful run.

func WithMaxConcurrency

func WithMaxConcurrency[T any](n int) AgentOption[T]

WithMaxConcurrency limits the number of tools that can execute concurrently.

func WithMaxRetries

func WithMaxRetries[T any](n int) AgentOption[T]

WithMaxRetries sets the maximum number of result validation retries.

func WithMaxTokens

func WithMaxTokens[T any](n int) AgentOption[T]

WithMaxTokens sets the max tokens for the agent.

func WithMessageInterceptor

func WithMessageInterceptor[T any](interceptor MessageInterceptor) AgentOption[T]

WithMessageInterceptor adds a pre-request message interceptor.

func WithModelSettings

func WithModelSettings[T any](s ModelSettings) AgentOption[T]

WithModelSettings sets the model settings for the agent.

func WithOutputOptions

func WithOutputOptions[T any](opts ...OutputOption) AgentOption[T]

WithOutputOptions sets output configuration options.

func WithOutputRepair

func WithOutputRepair[T any](repair RepairFunc[T]) AgentOption[T]

WithOutputRepair sets a repair function that is called when output parsing fails. The repair function gets one chance to fix the output before the normal retry flow.

func WithOutputValidator

func WithOutputValidator[T any](fn OutputValidatorFunc[T]) AgentOption[T]

WithOutputValidator adds an output validator to the agent.

func WithReasoningEffort

func WithReasoningEffort[T any](effort string) AgentOption[T]

WithReasoningEffort sets the reasoning effort level for OpenAI o-series models. Valid values: "low", "medium", "high".

func WithResponseInterceptor

func WithResponseInterceptor[T any](interceptor ResponseInterceptor) AgentOption[T]

WithResponseInterceptor adds a post-response interceptor.

func WithRunCondition

func WithRunCondition[T any](cond RunCondition) AgentOption[T]

WithRunCondition adds a run condition to the agent.

func WithSystemPrompt

func WithSystemPrompt[T any](prompt string) AgentOption[T]

WithSystemPrompt adds a system prompt to the agent.

func WithSystemPromptTemplate

func WithSystemPromptTemplate[T any](tmpl *PromptTemplate) AgentOption[T]

WithSystemPromptTemplate uses a template as a system prompt, rendered with variables from the RunContext.Deps (must be map[string]string or implement TemplateVars).

func WithTemperature

func WithTemperature[T any](t float64) AgentOption[T]

WithTemperature sets the temperature for the agent.

func WithThinkingBudget

func WithThinkingBudget[T any](budget int) AgentOption[T]

WithThinkingBudget enables extended thinking with the given token budget. Supported by Anthropic (direct and Vertex AI). When thinking is enabled, temperature is automatically stripped (Anthropic requirement).

func WithToolApproval

func WithToolApproval[T any](fn ToolApprovalFunc) AgentOption[T]

WithToolApproval sets the approval function for tools marked as requiring approval.

func WithToolChoice

func WithToolChoice[T any](choice *ToolChoice) AgentOption[T]

WithToolChoice sets the initial tool choice for model requests.

func WithToolChoiceAutoReset

func WithToolChoiceAutoReset[T any]() AgentOption[T]

WithToolChoiceAutoReset resets tool choice to "auto" after the first tool call. This prevents infinite loops when tool_choice is "required".

func WithToolOutputTruncation added in v0.1.9

func WithToolOutputTruncation[T any](config TruncationConfig) AgentOption[T]

WithToolOutputTruncation enables head/tail truncation of tool outputs before they are recorded into conversation history. This prevents large tool results (e.g., a grep returning thousands of lines) from bloating context on every subsequent model request. Off by default for backward compatibility.

func WithTools

func WithTools[T any](tools ...Tool) AgentOption[T]

WithTools adds tools to the agent.

func WithToolsPrepare

func WithToolsPrepare[T any](fn AgentToolsPrepareFunc) AgentOption[T]

WithToolsPrepare sets an agent-wide function that filters/modifies all tool definitions before each model request.

func WithToolsets

func WithToolsets[T any](toolsets ...*Toolset) AgentOption[T]

WithToolsets adds one or more toolsets to the agent.

func WithTraceExporter

func WithTraceExporter[T any](exporter TraceExporter) AgentOption[T]

WithTraceExporter adds a trace exporter to the agent. Tracing is automatically enabled when an exporter is set.

func WithTracing

func WithTracing[T any]() AgentOption[T]

WithTracing enables execution tracing. The trace is available on the RunResult after the run completes.

func WithTurnGuardrail

func WithTurnGuardrail[T any](name string, fn TurnGuardrailFunc) AgentOption[T]

WithTurnGuardrail adds a turn guardrail that runs before each model request.

func WithUsageLimits

func WithUsageLimits[T any](l UsageLimits) AgentOption[T]

WithUsageLimits sets the usage limits for the agent.

func WithUsageQuota

func WithUsageQuota[T any](quota UsageQuota) AgentOption[T]

WithUsageQuota sets hard usage limits for the agent run.

type AgentRun

type AgentRun[T any] struct {
	// contains filtered or unexported fields
}

AgentRun represents an in-progress agent execution that can be iterated step-by-step.

func (*AgentRun[T]) Close added in v0.3.0

func (ar *AgentRun[T]) Close() error

Close ends an iterative run before completion. Closing a started run emits the normal run-complete lifecycle notifications with ErrAgentRunClosed.

func (*AgentRun[T]) Done

func (ar *AgentRun[T]) Done() bool

Done returns true if the agent run has completed.

func (*AgentRun[T]) Messages

func (ar *AgentRun[T]) Messages() []ModelMessage

Messages returns the current message history.

func (*AgentRun[T]) Next

func (ar *AgentRun[T]) Next() (*ModelResponse, error)

Next executes one iteration of the agent loop (one model call + tool execution). Returns the ModelResponse for that step, or (nil, io.EOF) when done.

func (*AgentRun[T]) Result

func (ar *AgentRun[T]) Result() (*RunResult[T], error)

Result returns the final result after iteration completes. Returns error if not done.

type AgentRunError

type AgentRunError struct {
	Message string
}

AgentRunError is the base error for errors occurring during an agent run.

func (*AgentRunError) Error

func (e *AgentRunError) Error() string

type AgentRuntimeConfig added in v0.2.0

type AgentRuntimeConfig[T any] struct {
	SystemPrompts              []string
	DynamicSystemPrompts       []SystemPromptFunc
	HistoryProcessors          []HistoryProcessor
	Tools                      []Tool
	Hooks                      []Hook
	InputGuardrails            []InputGuardrailRuntime
	TurnGuardrails             []TurnGuardrailRuntime
	AgentToolsPrepare          AgentToolsPrepareFunc
	OutputSchema               *OutputSchema
	OutputValidators           []OutputValidatorFunc[T]
	OutputRepair               RepairFunc[T]
	ModelSettings              *ModelSettings
	UsageLimits                UsageLimits
	UsageQuota                 *UsageQuota
	MaxRetries                 int
	EndStrategy                EndStrategy
	MaxConcurrency             int
	DefaultToolTimeout         time.Duration
	ToolChoice                 *ToolChoice
	ToolChoiceAutoReset        bool
	ToolApprovalFunc           ToolApprovalFunc
	GlobalToolResultValidators []ToolResultValidatorFunc
	RequestMiddleware          []RequestMiddlewareFunc
	StreamMiddleware           []AgentStreamMiddleware
	MessageInterceptors        []MessageInterceptor
	ResponseInterceptors       []ResponseInterceptor
	RunConditions              []RunCondition
	TracingEnabled             bool
	TraceExporters             []TraceExporter
	ModelName                  string
	HasCostTracker             bool
	CostPricing                map[string]ModelPricing
	CostCurrency               string
	EventBus                   *EventBus
	AgentDeps                  any
	AutoContext                *AutoContextConfig
	KnowledgeBase              KnowledgeBase
	KnowledgeAutoStore         bool
}

AgentRuntimeConfig captures the configuration needed by alternative execution backends such as ext/temporal. It includes callback hooks so backends can execute them behind their own side-effect boundaries.

type AgentStreamFunc added in v0.1.10

type AgentStreamFunc func(
	ctx context.Context,
	messages []ModelMessage,
	settings *ModelSettings,
	params *ModelRequestParameters,
) (StreamedResponse, error)

AgentStreamFunc is the handler shape for streaming model requests.

type AgentStreamMiddleware added in v0.1.10

type AgentStreamMiddleware func(
	ctx context.Context,
	messages []ModelMessage,
	settings *ModelSettings,
	params *ModelRequestParameters,
	next AgentStreamFunc,
) (StreamedResponse, error)

AgentStreamMiddleware wraps a streaming model call within the agent loop. next is the actual streaming model call.

func LoggingStreamMiddleware added in v0.1.10

func LoggingStreamMiddleware(logger func(msg string)) AgentStreamMiddleware

LoggingStreamMiddleware logs streaming model request/response summaries. Prefer WithAgentMiddleware(LoggingMiddleware(...)) for new code.

func MaxTokensStreamMiddleware added in v0.1.10

func MaxTokensStreamMiddleware(maxTokens int) AgentStreamMiddleware

MaxTokensStreamMiddleware limits the number of tokens per streaming request via ModelSettings. Prefer WithAgentMiddleware(MaxTokensMiddleware(...)) for new code.

func TimingStreamMiddleware added in v0.1.10

func TimingStreamMiddleware(callback func(duration time.Duration)) AgentStreamMiddleware

TimingStreamMiddleware records total streaming duration and calls a callback when the stream completes or is closed. Prefer WithAgentMiddleware(TimingMiddleware(...)) for new code.

type AgentToolsPrepareFunc

type AgentToolsPrepareFunc func(ctx context.Context, rc *RunContext, tools []ToolDefinition) []ToolDefinition

AgentToolsPrepareFunc filters/modifies all tool definitions at once.

type AudioPart

type AudioPart struct {
	URL       string // audio URL or data: URI
	MIMEType  string // e.g., "audio/mp3", "audio/wav"
	Timestamp time.Time
}

AudioPart represents an audio input in a user message.

type AutoContextConfig

type AutoContextConfig struct {
	MaxTokens    int   // maximum token estimate before summarization
	KeepLastN    int   // number of recent messages to always keep (default: 4)
	SummaryModel Model // model to use for summarization (optional, uses agent model if nil)
}

AutoContextConfig configures automatic context window management.

type BatchResult

type BatchResult[T any] struct {
	Index  int           // position in the input slice
	Result *RunResult[T] // non-nil on success
	Err    error         // non-nil on failure
}

BatchResult holds the result of a single batch item.

type CallDeferred

type CallDeferred struct {
	Message string
}

CallDeferred is returned by tool handlers to indicate the tool call should be deferred for external resolution.

func (*CallDeferred) Error

func (e *CallDeferred) Error() string

type CompactionCallback added in v0.1.7

type CompactionCallback func(stats ContextCompactionStats)

CompactionCallback is a function that middleware can call to notify the agent's hook system about context compaction events.

func CompactionCallbackFromContext added in v0.1.7

func CompactionCallbackFromContext(ctx context.Context) CompactionCallback

CompactionCallbackFromContext extracts the compaction callback from context. Returns nil if no callback is set.

type ConsoleExporter

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

ConsoleExporter prints a human-readable trace summary to an io.Writer.

func NewConsoleExporter

func NewConsoleExporter(w io.Writer) *ConsoleExporter

NewConsoleExporter creates a trace exporter that writes to an io.Writer.

func (*ConsoleExporter) Export

func (e *ConsoleExporter) Export(_ context.Context, trace *RunTrace) error

type ContentFilterError

type ContentFilterError struct {
	UnexpectedModelBehavior
}

ContentFilterError indicates the provider's content filter was triggered.

type ContextCompactionStats added in v0.1.7

type ContextCompactionStats struct {
	// Strategy identifies the compaction mechanism (use CompactionStrategy* constants).
	Strategy string

	// MessagesBefore is the message count before compaction.
	MessagesBefore int
	// MessagesAfter is the message count after compaction.
	MessagesAfter int

	// EstimatedTokensBefore is the estimated token count before compaction.
	EstimatedTokensBefore int
	// EstimatedTokensAfter is the estimated token count after compaction.
	EstimatedTokensAfter int
}

ContextCompactionStats captures before/after state of a context compaction event.

type CostTracker

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

CostTracker tracks estimated costs across model requests.

func NewCostTracker

func NewCostTracker(pricing map[string]ModelPricing) *CostTracker

NewCostTracker creates a cost tracker with per-model pricing.

func (*CostTracker) CostBreakdown

func (ct *CostTracker) CostBreakdown() map[string]float64

CostBreakdown returns per-model cost breakdown.

func (*CostTracker) Pricing added in v0.2.0

func (ct *CostTracker) Pricing() map[string]ModelPricing

Pricing returns a copy of the configured per-model pricing table.

func (*CostTracker) Record

func (ct *CostTracker) Record(modelName string, usage RunUsage)

Record adds a cost entry based on model name and token usage.

func (*CostTracker) TotalCost

func (ct *CostTracker) TotalCost() float64

TotalCost returns the total estimated cost.

type DeferredToolRequest

type DeferredToolRequest struct {
	ToolName   string `json:"tool_name"`
	ToolCallID string `json:"tool_call_id"`
	ArgsJSON   string `json:"args_json"`
}

DeferredToolRequest represents a tool call that requires external resolution.

type DeferredToolResult

type DeferredToolResult struct {
	ToolName   string `json:"tool_name"`
	ToolCallID string `json:"tool_call_id"`
	Content    string `json:"content"`
	IsError    bool   `json:"is_error,omitempty"`
}

DeferredToolResult provides the result for a previously deferred tool call.

type DocumentPart

type DocumentPart struct {
	URL       string // document URL or data: URI
	MIMEType  string // e.g., "application/pdf"
	Title     string // optional display title
	Timestamp time.Time
}

DocumentPart represents a document input (PDF, etc.) in a user message.

type EndStrategy

type EndStrategy string

EndStrategy determines when to stop after finding a final result.

const (
	// EndStrategyEarly stops at the first valid result, skipping remaining tool calls.
	EndStrategyEarly EndStrategy = "early"
	// EndStrategyExhaustive processes all tool calls even after finding a result.
	EndStrategyExhaustive EndStrategy = "exhaustive"
)

type ErrDeferred

type ErrDeferred[T any] struct {
	Result RunResultDeferred[T]
}

ErrDeferred wraps a RunResultDeferred and is returned from Run when tool calls are deferred for external resolution.

func (*ErrDeferred[T]) Error

func (e *ErrDeferred[T]) Error() string

type EventBus

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

EventBus provides typed publish-subscribe for agent coordination.

func NewEventBus

func NewEventBus() *EventBus

NewEventBus creates a new event bus.

type FinishReason

type FinishReason string

FinishReason indicates why the model stopped generating.

const (
	FinishReasonStop          FinishReason = "stop"
	FinishReasonLength        FinishReason = "length"
	FinishReasonContentFilter FinishReason = "content_filter"
	FinishReasonToolCall      FinishReason = "tool_call"
	FinishReasonError         FinishReason = "error"
)

type GuardrailError

type GuardrailError struct {
	GuardrailName string
	Message       string
}

GuardrailError is returned when a guardrail rejects the input.

func (*GuardrailError) Error

func (e *GuardrailError) Error() string

type HistoryProcessor

type HistoryProcessor func(ctx context.Context, messages []ModelMessage) ([]ModelMessage, error)

HistoryProcessor transforms the message history before each model request. It receives context for operations that may need I/O (e.g., LLM summarization).

func NormalizeHistory added in v0.1.9

func NormalizeHistory() HistoryProcessor

NormalizeHistory returns a HistoryProcessor that cleans conversation history:

  • Removes orphaned ToolReturnParts that have no matching ToolCallPart in a preceding ModelResponse
  • Clears Images from ToolReturnParts in completed turns (all turns except the last ModelRequest), since images are useful in the turn they appear but waste tokens in subsequent turns
  • Removes ModelRequest messages that become empty after filtering

The returned processor does not modify the original slice.

type Hook

type Hook struct {
	// OnRunStart fires when an agent run begins.
	OnRunStart func(ctx context.Context, rc *RunContext, prompt string)
	// OnRunEnd fires when an agent run completes (success or error).
	OnRunEnd func(ctx context.Context, rc *RunContext, messages []ModelMessage, err error)
	// OnModelRequest fires before each model request.
	OnModelRequest func(ctx context.Context, rc *RunContext, messages []ModelMessage)
	// OnModelResponse fires after each model response.
	OnModelResponse func(ctx context.Context, rc *RunContext, response *ModelResponse)
	// OnToolStart fires before a tool executes.
	OnToolStart func(ctx context.Context, rc *RunContext, toolCallID string, toolName string, argsJSON string)
	// OnToolEnd fires after a tool completes.
	OnToolEnd func(ctx context.Context, rc *RunContext, toolCallID string, toolName string, result string, err error)
	// OnTurnStart fires at the beginning of each agent loop iteration (turn).
	OnTurnStart func(ctx context.Context, rc *RunContext, turnNumber int)
	// OnTurnEnd fires at the end of each agent loop iteration (turn).
	OnTurnEnd func(ctx context.Context, rc *RunContext, turnNumber int, response *ModelResponse)
	// OnGuardrailEvaluated fires after a guardrail (input or turn) is evaluated.
	OnGuardrailEvaluated func(ctx context.Context, rc *RunContext, name string, passed bool, err error)
	// OnOutputValidation fires after structured output validation completes.
	OnOutputValidation func(ctx context.Context, rc *RunContext, passed bool, err error)
	// OnOutputRepair fires when output repair is attempted.
	OnOutputRepair func(ctx context.Context, rc *RunContext, succeeded bool, err error)
	// OnRunConditionChecked fires when a run condition is evaluated and stops the run.
	OnRunConditionChecked func(ctx context.Context, rc *RunContext, stopped bool, reason string)
	// OnContextCompaction fires when the message history is compressed to
	// fit within the context window. This includes auto-summarization
	// (AutoContext) and emergency truncation (ContextOverflowMiddleware).
	OnContextCompaction func(ctx context.Context, rc *RunContext, stats ContextCompactionStats)
}

Hook provides callback functions for agent lifecycle events. Set only the fields you need — nil fields are skipped.

type ImagePart

type ImagePart struct {
	URL       string // image URL (https or data: URI with base64)
	MIMEType  string // e.g., "image/png", "image/jpeg"
	Detail    string // "auto", "low", "high" (optional)
	Timestamp time.Time
}

ImagePart represents an image input in a user message.

type IncompleteToolCall

type IncompleteToolCall struct {
	UnexpectedModelBehavior
}

IncompleteToolCall indicates the model hit its token limit while generating a tool call, resulting in malformed arguments.

type InputGuardrailFunc

type InputGuardrailFunc func(ctx context.Context, prompt string) (string, error)

InputGuardrailFunc validates or transforms the user prompt before the agent run begins. Return the (possibly modified) prompt, or error to reject.

func ContentFilter

func ContentFilter(blocked ...string) InputGuardrailFunc

ContentFilter rejects prompts containing any of the given substrings (case-insensitive).

func MaxPromptLength

func MaxPromptLength(maxChars int) InputGuardrailFunc

MaxPromptLength rejects prompts exceeding a character limit.

type InputGuardrailRuntime added in v0.2.0

type InputGuardrailRuntime struct {
	Name string
	Func InputGuardrailFunc
}

InputGuardrailRuntime captures a named input guardrail for alternative execution backends.

type InterceptResult

type InterceptResult struct {
	Action   MessageAction
	Messages []ModelMessage // used when Action == MessageModify
	Reason   string         // optional reason for drop/modify
}

InterceptResult is the result of intercepting a message.

type JSONFileExporter

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

JSONFileExporter writes traces as JSON files to a directory.

func NewJSONFileExporter

func NewJSONFileExporter(dir string) *JSONFileExporter

NewJSONFileExporter creates a trace exporter that writes JSON files.

func (*JSONFileExporter) Export

func (e *JSONFileExporter) Export(_ context.Context, trace *RunTrace) error

type JSONSchemaer

type JSONSchemaer interface {
	JSONSchema() Schema
}

JSONSchemaer allows types to provide their own JSON Schema. If a type implements this interface, SchemaFor will use its schema instead of generating one via reflection.

type KnowledgeBase

type KnowledgeBase interface {
	// Retrieve fetches context relevant to the given query.
	// Returns an empty string if no relevant context is found.
	Retrieve(ctx context.Context, query string) (string, error)

	// Store persists content (e.g., successful agent responses) for future retrieval.
	Store(ctx context.Context, content string) error
}

KnowledgeBase represents a system that can retrieve context relevant to a prompt and store insights from agent interactions. Implementations might include RAG systems, graph databases, memory services, or any external knowledge source.

type MessageAction

type MessageAction int

MessageAction represents what to do with a message after interception.

const (
	// MessageAllow passes the message through unchanged.
	MessageAllow MessageAction = iota
	// MessageDrop silently drops the message (skip this model request).
	MessageDrop
	// MessageModify replaces the messages with a modified version.
	MessageModify
)

type MessageInterceptor

type MessageInterceptor func(ctx context.Context, messages []ModelMessage) InterceptResult

MessageInterceptor intercepts messages before model requests.

func AuditLog

func AuditLog(logger func(direction string, messages []ModelMessage)) MessageInterceptor

AuditLog logs all messages to a callback before allowing them through.

func RedactPII

func RedactPII(pattern string, replacement string) MessageInterceptor

RedactPII replaces patterns matching the regex with a placeholder in user prompts.

type Model

type Model interface {
	// Request sends messages and returns a complete response.
	Request(ctx context.Context, messages []ModelMessage, settings *ModelSettings, params *ModelRequestParameters) (*ModelResponse, error)

	// RequestStream sends messages and returns an event stream for
	// incremental consumption. Providers that don't support streaming
	// should return an error.
	RequestStream(ctx context.Context, messages []ModelMessage, settings *ModelSettings, params *ModelRequestParameters) (StreamedResponse, error)

	// ModelName returns the identifier of the model (e.g., "claude-sonnet-4-5").
	ModelName() string
}

Model is the core interface every LLM provider must implement.

type ModelHTTPError

type ModelHTTPError struct {
	Message    string
	StatusCode int
	Body       string
	ModelName  string
	RetryAfter time.Duration // parsed from Retry-After header, if present
}

ModelHTTPError indicates an HTTP-level failure from a provider.

func (*ModelHTTPError) Error

func (e *ModelHTTPError) Error() string

type ModelMessage

type ModelMessage interface {
	// contains filtered or unexported methods
}

ModelMessage is the interface for all messages in a conversation. A message is either a ModelRequest (sent to the model) or a ModelResponse (received from the model).

func AutoCompressMessages added in v0.2.0

func AutoCompressMessages(ctx context.Context, messages []ModelMessage, config *AutoContextConfig, fallbackModel Model, tokenCount int) ([]ModelMessage, error)

AutoCompressMessages exposes the agent's auto-context compaction logic for alternative execution backends that need to preserve the same behavior.

func DecodeMessages added in v0.2.0

func DecodeMessages(envelopes []SerializedMessage) ([]ModelMessage, error)

DecodeMessages converts structured serialized messages back into model messages.

func UnmarshalMessages

func UnmarshalMessages(data []byte) ([]ModelMessage, error)

UnmarshalMessages deserializes JSON back into []ModelMessage.

type ModelPricing

type ModelPricing struct {
	InputTokenCost  float64 // cost per input token (e.g., 0.000003 for $3/1M)
	OutputTokenCost float64 // cost per output token
	CachedInputCost float64 // cost per cached input token read (0 = same as input)
	CacheWriteCost  float64 // cost per cache write token (0 = same as input)
}

ModelPricing defines per-token costs for a model.

type ModelRequest

type ModelRequest struct {
	Parts     []ModelRequestPart
	Timestamp time.Time
}

ModelRequest is a request message sent to the model.

type ModelRequestParameters

type ModelRequestParameters struct {
	// FunctionTools are the callable tools available to the model.
	FunctionTools []ToolDefinition

	// OutputMode determines how structured output is extracted.
	OutputMode OutputMode

	// OutputTools are synthetic tools for structured output extraction.
	OutputTools []ToolDefinition

	// OutputObject describes the output schema for native/prompted modes.
	OutputObject *OutputObjectDefinition

	// AllowTextOutput controls whether the model can return text directly.
	AllowTextOutput bool
}

ModelRequestParameters bundles tool definitions and output configuration for a request.

func (*ModelRequestParameters) AllToolDefs

func (p *ModelRequestParameters) AllToolDefs() []ToolDefinition

AllToolDefs returns all tool definitions (function + output) for the request.

type ModelRequestPart

type ModelRequestPart interface {
	// contains filtered or unexported methods
}

ModelRequestPart is the interface for all parts of a model request.

func DecodeRequestParts added in v0.2.0

func DecodeRequestParts(parts []SerializedPart) ([]ModelRequestPart, error)

DecodeRequestParts converts structured serialized request parts back into request parts.

type ModelResponse

type ModelResponse struct {
	Parts        []ModelResponsePart
	Usage        Usage
	ModelName    string
	FinishReason FinishReason
	Timestamp    time.Time
}

ModelResponse is the model's reply.

func DecodeModelResponse added in v0.2.0

func DecodeModelResponse(env *SerializedMessage) (*ModelResponse, error)

DecodeModelResponse converts a serialized response envelope back into a model response.

func MultiToolCallResponse

func MultiToolCallResponse(calls ...ToolCallPart) *ModelResponse

MultiToolCallResponse creates a ModelResponse with multiple ToolCallParts.

func TextResponse

func TextResponse(text string) *ModelResponse

TextResponse creates a simple ModelResponse with a TextPart.

func ToolCallResponse

func ToolCallResponse(toolName, argsJSON string) *ModelResponse

ToolCallResponse creates a ModelResponse with a ToolCallPart.

func ToolCallResponseWithID

func ToolCallResponseWithID(toolName, argsJSON, callID string) *ModelResponse

ToolCallResponseWithID creates a ToolCallResponse with a specific call ID.

func (ModelResponse) TextContent

func (m ModelResponse) TextContent() string

TextContent returns the concatenated text content from the response.

func (ModelResponse) ToolCalls

func (m ModelResponse) ToolCalls() []ToolCallPart

ToolCalls returns all ToolCallParts from the response.

type ModelResponsePart

type ModelResponsePart interface {
	// contains filtered or unexported methods
}

ModelResponsePart is the interface for all parts of a model response.

type ModelResponsePartDelta

type ModelResponsePartDelta interface {
	// contains filtered or unexported methods
}

ModelResponsePartDelta is the interface for incremental part updates.

type ModelResponseStreamEvent

type ModelResponseStreamEvent interface {
	// contains filtered or unexported methods
}

ModelResponseStreamEvent is the interface for streaming events.

type ModelRetryError

type ModelRetryError struct {
	Message string
}

ModelRetryError is returned by tool functions to request that the model retry with the given feedback message. The message is sent back to the model as a RetryPromptPart.

func NewModelRetryError

func NewModelRetryError(msg string) *ModelRetryError

NewModelRetryError creates a ModelRetryError with the given message.

func (*ModelRetryError) Error

func (e *ModelRetryError) Error() string

type ModelSettings

type ModelSettings struct {
	MaxTokens       *int        `json:"max_tokens,omitempty"`
	Temperature     *float64    `json:"temperature,omitempty"`
	TopP            *float64    `json:"top_p,omitempty"`
	ToolChoice      *ToolChoice `json:"tool_choice,omitempty"`
	ThinkingBudget  *int        `json:"thinking_budget,omitempty"`  // Anthropic extended thinking budget tokens
	ReasoningEffort *string     `json:"reasoning_effort,omitempty"` // OpenAI o-series: "low", "medium", "high"
}

ModelSettings holds provider-specific settings like temperature and max tokens.

type MultiExporter

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

MultiExporter fans out to multiple exporters.

func NewMultiExporter

func NewMultiExporter(exporters ...TraceExporter) *MultiExporter

NewMultiExporter creates an exporter that fans out to multiple exporters.

func (*MultiExporter) Export

func (e *MultiExporter) Export(ctx context.Context, trace *RunTrace) error

type OutputMode

type OutputMode string

OutputMode determines how structured output is extracted from the model.

const (
	// OutputModeText extracts output from the model's text response.
	OutputModeText OutputMode = "text"
	// OutputModeTool uses a synthetic tool call to extract structured output.
	OutputModeTool OutputMode = "tool"
	// OutputModeNative uses the provider's native structured output support.
	OutputModeNative OutputMode = "native"
)

type OutputObjectDefinition

type OutputObjectDefinition struct {
	Name        string
	Description string
	JSONSchema  Schema
	Strict      *bool
}

OutputObjectDefinition describes the desired output schema for structured output modes.

type OutputOption

type OutputOption func(*outputConfig)

OutputOption configures output behavior.

func WithOutputMode

func WithOutputMode(mode OutputMode) OutputOption

WithOutputMode forces a specific output mode.

func WithOutputToolDescription

func WithOutputToolDescription(desc string) OutputOption

WithOutputToolDescription sets the description of the output extraction tool.

func WithOutputToolName

func WithOutputToolName(name string) OutputOption

WithOutputToolName sets the name of the output extraction tool.

type OutputSchema

type OutputSchema struct {
	// Mode is the output extraction mode.
	Mode OutputMode

	// OutputTools contains the synthetic tool definitions for tool-based output.
	OutputTools []ToolDefinition

	// OutputObject describes the schema for native structured output.
	OutputObject *OutputObjectDefinition

	// AllowsText is true when text output is acceptable (e.g., T is string).
	AllowsText bool

	// OuterTypedDictKey is set when the output type is wrapped in an object.
	OuterTypedDictKey string
	// contains filtered or unexported fields
}

OutputSchema determines how to extract structured output from model responses.

type OutputValidatorFunc

type OutputValidatorFunc[T any] func(ctx context.Context, rc *RunContext, output T) (T, error)

OutputValidatorFunc validates and potentially transforms the output. Return a ModelRetryError to trigger a retry with feedback to the model.

type PartDeltaEvent

type PartDeltaEvent struct {
	Index int
	Delta ModelResponsePartDelta
}

PartDeltaEvent signals an incremental update to a response part.

type PartEndEvent

type PartEndEvent struct {
	Index int
}

PartEndEvent signals that a response part has finished streaming.

type PartStartEvent

type PartStartEvent struct {
	Index int
	Part  ModelResponsePart
}

PartStartEvent signals that a new response part has started streaming.

type PromptTemplate

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

PromptTemplate is a prompt with named variable placeholders using {{.VarName}} syntax. Uses Go's text/template under the hood for safe, familiar templating.

func MustTemplate

func MustTemplate(name, tmpl string) *PromptTemplate

MustTemplate is like NewPromptTemplate but panics on error. For use with constants.

func NewPromptTemplate

func NewPromptTemplate(name, tmpl string) (*PromptTemplate, error)

NewPromptTemplate creates a template from a Go template string. Returns error if the template is malformed.

func (*PromptTemplate) Format

func (t *PromptTemplate) Format(vars map[string]string) (string, error)

Format renders the template with the given variables.

func (*PromptTemplate) Partial

func (t *PromptTemplate) Partial(vars map[string]string) *PromptTemplate

Partial returns a new template with some variables pre-filled.

func (*PromptTemplate) Variables

func (t *PromptTemplate) Variables() []string

Variables returns the set of variable names used in the template. It parses the template tree to extract field names from actions.

type QuotaExceededError

type QuotaExceededError struct {
	Quota   UsageQuota
	Usage   RunUsage
	Message string
}

QuotaExceededError is returned when a usage quota is breached.

func (*QuotaExceededError) Error

func (e *QuotaExceededError) Error() string

type ReflectOption

type ReflectOption func(*reflectConfig)

ReflectOption configures the reflection loop.

func WithMaxReflections

func WithMaxReflections(n int) ReflectOption

WithMaxReflections sets the maximum number of reflection iterations (default: 3).

func WithReflectPrompt

func WithReflectPrompt(prompt string) ReflectOption

WithReflectPrompt sets a custom reflection prompt template. Use %s as placeholder for the output being reviewed.

type ReflectResult

type ReflectResult[T any] struct {
	// Output is the final (possibly corrected) output.
	Output T
	// Iterations is the number of reflection iterations performed.
	Iterations int
	// Usage is the combined usage across all iterations.
	Usage RunUsage
	// Accepted indicates whether the output was accepted by the validator.
	Accepted bool
}

ReflectResult contains the output of a reflection loop.

func RunWithReflection

func RunWithReflection[T any](
	ctx context.Context,
	agent *Agent[T],
	prompt string,
	validator func(ctx context.Context, output T) (feedback string, err error),
	opts ...ReflectOption,
) (*ReflectResult[T], error)

RunWithReflection runs the agent with a reflection/self-correction loop. After each run, the validator function checks the output. If the validator returns an error message, the agent is re-run with that feedback appended to the conversation. If the validator returns empty string, the output is accepted.

The validator receives the output and returns:

  • "" (empty string) if the output is acceptable
  • A non-empty feedback string describing what needs to be corrected

type RepairFunc

type RepairFunc[T any] func(ctx context.Context, raw string, parseErr error) (T, error)

RepairFunc takes malformed raw output and the parse/validation error, and returns a repaired output of type T. Typically calls a model to fix the output.

func ModelRepair

func ModelRepair[T any](model Model) RepairFunc[T]

ModelRepair creates a RepairFunc that sends the malformed output to a model with instructions to fix it. The model response is then parsed as T.

type RequestMiddlewareFunc added in v0.1.10

type RequestMiddlewareFunc func(
	ctx context.Context,
	messages []ModelMessage,
	settings *ModelSettings,
	params *ModelRequestParameters,
	next func(context.Context, []ModelMessage, *ModelSettings, *ModelRequestParameters) (*ModelResponse, error),
) (*ModelResponse, error)

RequestMiddlewareFunc wraps a non-streaming model call within the agent loop. next is the actual model call — middleware can modify inputs, outputs, or skip the call.

type ResponseInterceptor

type ResponseInterceptor func(ctx context.Context, response *ModelResponse) InterceptResult

ResponseInterceptor intercepts model responses.

type RetryPromptPart

type RetryPromptPart struct {
	Content    string // error message or validation feedback
	ToolName   string // the tool that should be retried (empty for result retry)
	ToolCallID string // the tool call ID being retried
	Timestamp  time.Time
}

RetryPromptPart tells the model to retry with feedback about what went wrong.

type RunCompletedEvent

type RunCompletedEvent struct {
	RunID       string
	ParentRunID string
	Success     bool
	Error       string
	StartedAt   time.Time
	CompletedAt time.Time
}

RunCompletedEvent is published when an agent run completes.

func NewRunCompletedEvent added in v0.3.0

func NewRunCompletedEvent(runID, parentRunID string, startedAt, completedAt time.Time, runErr error) RunCompletedEvent

NewRunCompletedEvent constructs a standardized run-complete event.

func (RunCompletedEvent) RuntimeEventType added in v0.3.0

func (e RunCompletedEvent) RuntimeEventType() string

func (RunCompletedEvent) RuntimeOccurredAt added in v0.3.0

func (e RunCompletedEvent) RuntimeOccurredAt() time.Time

func (RunCompletedEvent) RuntimeParentRunID added in v0.3.0

func (e RunCompletedEvent) RuntimeParentRunID() string

func (RunCompletedEvent) RuntimeRunID added in v0.3.0

func (e RunCompletedEvent) RuntimeRunID() string

type RunCondition

type RunCondition func(ctx context.Context, rc *RunContext, resp *ModelResponse) (stop bool, reason string)

RunCondition is a predicate checked after each model response. Return true to stop the run, with an optional reason message.

func And

func And(conditions ...RunCondition) RunCondition

And combines conditions — stop only if ALL conditions are met.

func MaxRunDuration

func MaxRunDuration(d time.Duration) RunCondition

MaxRunDuration stops the run after a time limit. The timer starts when the run begins, not when MaxRunDuration is called, so this works correctly even when used as an AgentOption or shared across multiple runs.

func Or

func Or(conditions ...RunCondition) RunCondition

Or combines conditions — stop if ANY condition is met.

func ResponseContains

func ResponseContains(fn func(*ModelResponse) bool) RunCondition

ResponseContains stops when any response matches the predicate.

func TextContains

func TextContains(substr string) RunCondition

TextContains stops when the model's text response contains a substring.

func ToolCallCount

func ToolCallCount(max int) RunCondition

ToolCallCount stops after max total tool calls. max must be positive.

type RunConditionError

type RunConditionError struct {
	Reason string
}

RunConditionError indicates an agent run was stopped by a run condition.

func (*RunConditionError) Error

func (e *RunConditionError) Error() string

type RunContext

type RunContext struct {
	Deps         any            // user-provided dependencies
	Usage        RunUsage       // current usage
	Prompt       string         // the user prompt
	Retry        int            // current retry count for this tool
	MaxRetries   int            // max retries configured
	ToolName     string         // name of the current tool
	ToolCallID   string         // ID of the current tool call
	Messages     []ModelMessage // conversation history (read-only)
	RunStep      int            // current step number
	RunID        string         // unique run ID
	ParentRunID  string         // parent run ID when this run was started by another run
	RunStartTime time.Time      // when the current run started
	EventBus     *EventBus      // event bus for agent coordination (nil if not configured)

	// Detach is closed by the UI layer to signal that the currently running
	// tool should move its work to the background and return immediately.
	// Tools that support detach (e.g., bash) select on this channel alongside
	// their blocking operation. Nil means detach is not supported.
	Detach <-chan struct{}
	// contains filtered or unexported fields
}

RunContext provides tools with access to agent run state.

func NewRunContext added in v0.2.0

func NewRunContext(base RunContext, toolStateGetter func() map[string]any, snapshotGetter func() *RunStateSnapshot) *RunContext

NewRunContext constructs a RunContext with optional tool-state and snapshot getters. It is primarily intended for extension packages that need to execute tools outside the core agent loop while preserving read-only access to run state.

func (*RunContext) RunStateSnapshot added in v0.2.0

func (rc *RunContext) RunStateSnapshot() *RunStateSnapshot

RunStateSnapshot returns a snapshot of the current run state when available. Nil is returned when the run context was not created by an active agent run.

func (*RunContext) ToolState added in v0.1.7

func (rc *RunContext) ToolState() map[string]any

ToolState returns a snapshot of all exported stateful-tool state currently available for this run context. Nil is returned when no state is available.

func (*RunContext) ToolStateByName added in v0.1.7

func (rc *RunContext) ToolStateByName(name string) (any, bool)

ToolStateByName returns exported state for a single stateful tool.

type RunCost

type RunCost struct {
	TotalCost float64            `json:"total_cost"`
	Breakdown map[string]float64 `json:"breakdown"`
	Currency  string             `json:"currency"`
}

RunCost returns the cost details for the run.

func EstimateRunCost added in v0.2.0

func EstimateRunCost(modelName string, usage RunUsage, pricing map[string]ModelPricing) *RunCost

EstimateRunCost calculates the run cost for a single model usage snapshot without mutating any external tracker state.

type RunOption

type RunOption func(*runConfig)

RunOption configures a specific run invocation.

func WithBatchConcurrency

func WithBatchConcurrency(n int) RunOption

WithBatchConcurrency sets the maximum number of concurrent batch executions.

func WithDeferredResults

func WithDeferredResults(results ...DeferredToolResult) RunOption

WithDeferredResults injects deferred tool results into the run. When provided, the results are sent as ToolReturnParts (or RetryPromptParts for error results) before the first model call.

func WithDetach added in v0.1.5

func WithDetach(ch <-chan struct{}) RunOption

WithDetach provides a channel that the UI layer can close to signal the currently executing tool to move its work to the background. Tools that support detach (e.g., bash) will select on this channel alongside their blocking operation. When closed, the tool adopts the running process into the background process pool and returns immediately.

func WithInitialRequestParts added in v0.1.1

func WithInitialRequestParts(parts ...ModelRequestPart) RunOption

WithInitialRequestParts appends additional parts to the first user request generated by Run/Iter. Useful for multimodal inputs such as ImagePart.

func WithMessages

func WithMessages(msgs ...ModelMessage) RunOption

WithMessages sets initial conversation history for the run. This is used to resume from checkpoints or continue conversations.

func WithRunDeps

func WithRunDeps(deps any) RunOption

WithRunDeps sets dependencies available to tools via RunContext.

func WithRunModelSettings

func WithRunModelSettings(s ModelSettings) RunOption

WithRunModelSettings overrides model settings for this run.

func WithRunUsageLimits

func WithRunUsageLimits(l UsageLimits) RunOption

WithRunUsageLimits overrides usage limits for this run.

func WithSnapshot

func WithSnapshot(snap *RunSnapshot) RunOption

WithSnapshot resumes a run from a snapshot. The agent continues from the snapshot's serialized run state rather than starting fresh.

func WithToolState added in v0.1.7

func WithToolState(state map[string]any) RunOption

WithToolState restores stateful tool state from a previous run. Use this with WithMessages to continue multi-turn conversations where tools like planning and invariants need to retain their state.

type RunResult

type RunResult[T any] struct {
	// Output is the parsed/validated output of type T.
	Output T
	// Messages is the full conversation history.
	Messages []ModelMessage
	// Usage is the aggregate token usage for this run.
	Usage RunUsage
	// RunID is the unique identifier for this run.
	RunID string
	// Trace is the execution trace when WithTracing is enabled. Nil otherwise.
	Trace *RunTrace
	// Cost is the estimated cost when a CostTracker is configured. Nil otherwise.
	Cost *RunCost
	// ToolState is the exported state of stateful tools at the end of this run.
	// Pass this to WithToolState() on the next Run() to restore tool state
	// across turns in a multi-turn conversation.
	ToolState map[string]any
}

RunResult is the outcome of a successful agent run.

func (*RunResult[T]) AllMessagesJSON

func (r *RunResult[T]) AllMessagesJSON() ([]byte, error)

AllMessagesJSON serializes the full conversation history of a RunResult to JSON.

type RunResultDeferred

type RunResultDeferred[T any] struct {
	DeferredRequests []DeferredToolRequest
	Messages         []ModelMessage
	Usage            RunUsage
}

RunResultDeferred is returned when an agent run ends with pending deferred tools.

type RunSnapshot

type RunSnapshot = RunStateSnapshot

RunSnapshot is kept as the public compatibility name for RunStateSnapshot.

func DecodeRunSnapshot added in v0.2.0

func DecodeRunSnapshot(snap *SerializedRunSnapshot) (*RunSnapshot, error)

DecodeRunSnapshot converts a structured serialized snapshot back into a run snapshot.

func Snapshot

func Snapshot(rc *RunContext) *RunSnapshot

Snapshot creates a serializable snapshot of the current agent run state. Call this from a hook (for example OnModelResponse) to capture mid-run state.

func UnmarshalSnapshot

func UnmarshalSnapshot(data []byte) (*RunSnapshot, error)

UnmarshalSnapshot deserializes a snapshot from JSON.

type RunStartedEvent

type RunStartedEvent struct {
	RunID       string
	ParentRunID string
	Prompt      string
	StartedAt   time.Time
}

RunStartedEvent is published when an agent run starts.

func NewRunStartedEvent added in v0.3.0

func NewRunStartedEvent(runID, parentRunID, prompt string, startedAt time.Time) RunStartedEvent

NewRunStartedEvent constructs a standardized run-start event.

func (RunStartedEvent) RuntimeEventType added in v0.3.0

func (e RunStartedEvent) RuntimeEventType() string

func (RunStartedEvent) RuntimeOccurredAt added in v0.3.0

func (e RunStartedEvent) RuntimeOccurredAt() time.Time

func (RunStartedEvent) RuntimeParentRunID added in v0.3.0

func (e RunStartedEvent) RuntimeParentRunID() string

func (RunStartedEvent) RuntimeRunID added in v0.3.0

func (e RunStartedEvent) RuntimeRunID() string

type RunState added in v0.2.0

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

RunState tracks mutable state across an agent run.

type RunStateSnapshot added in v0.2.0

type RunStateSnapshot struct {
	Messages        []ModelMessage `json:"-"` // excluded from default JSON
	Usage           RunUsage       `json:"usage"`
	LastInputTokens int            `json:"last_input_tokens"`
	Retries         int            `json:"retries"`
	ToolRetries     map[string]int `json:"tool_retries,omitempty"`
	RunID           string         `json:"run_id"`
	ParentRunID     string         `json:"parent_run_id,omitempty"`
	RunStep         int            `json:"run_step"`
	RunStartTime    time.Time      `json:"run_start_time"`
	Prompt          string         `json:"prompt"`
	ToolState       map[string]any `json:"tool_state,omitempty"`
	Timestamp       time.Time      `json:"timestamp"`
}

RunStateSnapshot captures the serializable state of an agent run at a point in time.

func (*RunStateSnapshot) Branch added in v0.2.0

func (s *RunStateSnapshot) Branch(modifier func(messages []ModelMessage) []ModelMessage) *RunSnapshot

Branch creates a modified copy of the snapshot for exploring alternate paths.

type RunTrace

type RunTrace struct {
	RunID     string        `json:"run_id"`
	Prompt    string        `json:"prompt"`
	StartTime time.Time     `json:"start_time"`
	EndTime   time.Time     `json:"end_time"`
	Duration  time.Duration `json:"duration"`
	Steps     []TraceStep   `json:"steps"`
	Usage     RunUsage      `json:"usage"`
	Success   bool          `json:"success"`
	Error     string        `json:"error,omitempty"`
}

RunTrace captures the full execution trace of an agent run.

type RunUsage

type RunUsage struct {
	Usage
	Requests  int `json:"requests"`
	ToolCalls int `json:"tool_calls"`
}

RunUsage tracks aggregate token counts across an entire agent run.

func (*RunUsage) IncrRequest

func (u *RunUsage) IncrRequest(other Usage)

IncrRequest adds a single request's usage and increments the request count.

func (*RunUsage) IncrRun

func (u *RunUsage) IncrRun(other RunUsage)

IncrRun adds another RunUsage to this one.

func (*RunUsage) IncrToolCall

func (u *RunUsage) IncrToolCall()

IncrToolCall increments the tool call count.

type RuntimeEvent added in v0.3.0

type RuntimeEvent interface {
	RuntimeEventType() string
	RuntimeRunID() string
	RuntimeParentRunID() string
	RuntimeOccurredAt() time.Time
}

RuntimeEvent is implemented by built-in runtime lifecycle events.

type Schema

type Schema = map[string]any

Schema represents a JSON Schema document.

func SchemaFor

func SchemaFor[T any]() Schema

SchemaFor generates a JSON Schema for Go type T using reflection.

type SerializedMessage added in v0.2.0

type SerializedMessage = messageEnvelope

SerializedMessage is the JSON-safe envelope form of a ModelMessage.

func EncodeMessages added in v0.2.0

func EncodeMessages(messages []ModelMessage) ([]SerializedMessage, error)

EncodeMessages converts model messages into their structured serialized form.

func EncodeModelResponse added in v0.2.0

func EncodeModelResponse(resp *ModelResponse) (*SerializedMessage, error)

EncodeModelResponse converts a model response into its serialized envelope form.

type SerializedPart added in v0.2.0

type SerializedPart = partEnvelope

SerializedPart is the JSON-safe envelope form of a request or response part.

func EncodeRequestParts added in v0.2.0

func EncodeRequestParts(parts []ModelRequestPart) ([]SerializedPart, error)

EncodeRequestParts converts request parts into their structured serialized form.

type SerializedRunSnapshot added in v0.2.0

type SerializedRunSnapshot struct {
	Messages        []SerializedMessage `json:"messages"`
	Usage           RunUsage            `json:"usage"`
	LastInputTokens int                 `json:"last_input_tokens"`
	Retries         int                 `json:"retries"`
	ToolRetries     map[string]int      `json:"tool_retries,omitempty"`
	RunID           string              `json:"run_id"`
	ParentRunID     string              `json:"parent_run_id,omitempty"`
	RunStep         int                 `json:"run_step"`
	RunStartTime    time.Time           `json:"run_start_time"`
	Prompt          string              `json:"prompt"`
	ToolState       map[string]any      `json:"tool_state,omitempty"`
	Timestamp       time.Time           `json:"timestamp"`
}

SerializedRunSnapshot is the structured JSON-safe form of a run snapshot.

func EncodeRunSnapshot added in v0.2.0

func EncodeRunSnapshot(snap *RunSnapshot) (*SerializedRunSnapshot, error)

EncodeRunSnapshot converts a run snapshot into its structured serialized form.

type StatefulTool

type StatefulTool interface {
	ExportState() (any, error)
	RestoreState(state any) error
}

StatefulTool is an optional interface that tools can implement to persist and restore their internal state across checkpoints.

type StaticKnowledgeBase

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

StaticKnowledgeBase is a simple in-memory KnowledgeBase for testing. It always returns the same context string on Retrieve and discards Store calls.

func NewStaticKnowledgeBase

func NewStaticKnowledgeBase(context string) *StaticKnowledgeBase

NewStaticKnowledgeBase creates a StaticKnowledgeBase that always returns the given context.

func (*StaticKnowledgeBase) Retrieve

func (kb *StaticKnowledgeBase) Retrieve(_ context.Context, _ string) (string, error)

Retrieve returns the static context string.

func (*StaticKnowledgeBase) Store

func (kb *StaticKnowledgeBase) Store(_ context.Context, content string) error

Store records the content for later inspection in tests.

func (*StaticKnowledgeBase) Stored

func (kb *StaticKnowledgeBase) Stored() []string

Stored returns all content that was stored via Store calls.

type StreamResult

type StreamResult[T any] struct {
	// contains filtered or unexported fields
}

StreamResult wraps a streaming model response and provides methods to consume the stream as text, events, or structured output.

func (*StreamResult[T]) Close

func (s *StreamResult[T]) Close() error

Close releases streaming resources.

func (*StreamResult[T]) GetOutput

func (s *StreamResult[T]) GetOutput() (*ModelResponse, error)

GetOutput consumes the entire stream and returns the final response.

func (*StreamResult[T]) Messages

func (s *StreamResult[T]) Messages() []ModelMessage

Messages returns a copy of the message history at the start of this stream.

func (*StreamResult[T]) Response

func (s *StreamResult[T]) Response() *ModelResponse

Response returns the ModelResponse built from data received so far.

func (*StreamResult[T]) Result added in v0.1.10

func (s *StreamResult[T]) Result() (*RunResult[T], error)

Result consumes the entire stream and returns the final typed run result.

func (*StreamResult[T]) StreamEvents

func (s *StreamResult[T]) StreamEvents() iter.Seq2[ModelResponseStreamEvent, error]

StreamEvents returns an iterator over raw stream events.

func (*StreamResult[T]) StreamText

func (s *StreamResult[T]) StreamText(delta bool) iter.Seq2[string, error]

StreamText returns an iterator that yields text content from the stream. If delta is true, yields incremental text chunks. If delta is false, yields cumulative text.

type StreamedResponse

type StreamedResponse interface {
	// Next returns the next stream event. Returns io.EOF when the stream is done.
	Next() (ModelResponseStreamEvent, error)

	// Response returns the ModelResponse built from data received so far.
	Response() *ModelResponse

	// Usage returns usage information gathered so far.
	Usage() Usage

	// Close cleans up resources (HTTP connections, etc.).
	Close() error
}

StreamedResponse is the interface for streaming model responses.

type SystemPromptFunc

type SystemPromptFunc func(ctx context.Context, runCtx *RunContext) (string, error)

SystemPromptFunc generates a system prompt dynamically using RunContext.

type SystemPromptPart

type SystemPromptPart struct {
	Content   string
	Timestamp time.Time
}

SystemPromptPart provides system-level instructions to the model.

type TemplateVars

type TemplateVars interface {
	TemplateVars() map[string]string
}

TemplateVars provides template variable values. Implement on your deps type.

type TestModel

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

TestModel is a mock Model for testing agents without real LLM calls. It returns canned responses in sequence and records all calls for assertions.

func NewTestModel

func NewTestModel(responses ...*ModelResponse) *TestModel

NewTestModel creates a TestModel with canned responses.

func (*TestModel) Calls

func (m *TestModel) Calls() []TestModelCall

Calls returns the recorded calls for assertions.

func (*TestModel) ModelName

func (m *TestModel) ModelName() string

func (*TestModel) Request

func (m *TestModel) Request(ctx context.Context, messages []ModelMessage, settings *ModelSettings, params *ModelRequestParameters) (*ModelResponse, error)

func (*TestModel) RequestStream

func (m *TestModel) RequestStream(ctx context.Context, messages []ModelMessage, settings *ModelSettings, params *ModelRequestParameters) (StreamedResponse, error)

func (*TestModel) Reset

func (m *TestModel) Reset()

Reset clears the call history and resets the response index.

func (*TestModel) SetName

func (m *TestModel) SetName(name string)

SetName sets the model name (useful in tests that need distinct model names).

type TestModelCall

type TestModelCall struct {
	Messages   []ModelMessage
	Settings   *ModelSettings
	Parameters *ModelRequestParameters
}

TestModelCall records a call made to the test model.

type TextPart

type TextPart struct {
	Content string
}

TextPart contains text content from the model.

type TextPartDelta

type TextPartDelta struct {
	ContentDelta string
}

TextPartDelta is an incremental text chunk.

type ThinkingPart

type ThinkingPart struct {
	Content   string
	Signature string // provider-specific signature
}

ThinkingPart contains the model's chain-of-thought reasoning.

type ThinkingPartDelta

type ThinkingPartDelta struct {
	ContentDelta string
}

ThinkingPartDelta is an incremental thinking chunk.

type Tool

type Tool struct {
	Definition       ToolDefinition
	Handler          ToolHandler
	MaxRetries       *int                    // nil = use agent default
	RequiresApproval bool                    // if true, the agent's ToolApprovalFunc must approve before execution
	PrepareFunc      ToolPrepareFunc         // if set, called before each model request to filter/modify this tool
	Stateful         StatefulTool            // if set, state is saved/restored with checkpoints
	ResultValidator  ToolResultValidatorFunc // if set, validates tool results before passing to model
	Timeout          time.Duration           // if > 0, tool execution is limited to this duration
}

Tool is a registered tool with its definition and handler.

func FuncTool

func FuncTool[P any](name, description string, fn any, opts ...ToolOption) Tool

FuncTool creates a Tool from a typed Go function using reflection for schema generation. The function must have one of these signatures:

func(ctx context.Context, params P) (R, error)
func(ctx context.Context, rc *RunContext, params P) (R, error)

P is the parameters struct type whose fields are converted to a JSON Schema. R is the return type that will be serialized to JSON.

Example
package main

import (
	"context"
	"fmt"

	"github.com/fugue-labs/gollem/core"
)

func main() {
	type WeatherParams struct {
		City string `json:"city" description:"City name"`
	}

	tool := core.FuncTool[WeatherParams]("get_weather", "Get weather for a city",
		func(_ context.Context, params WeatherParams) (string, error) {
			return "Sunny in " + params.City, nil
		},
	)

	fmt.Println(tool.Definition.Name)
	fmt.Println(tool.Definition.Description)
}
Output:
get_weather
Get weather for a city

type ToolApprovalFunc

type ToolApprovalFunc func(ctx context.Context, toolName string, argsJSON string) (bool, error)

ToolApprovalFunc is called before executing a tool that requires approval. Return true to approve, false to deny (sends denial message back to model).

type ToolCallPart

type ToolCallPart struct {
	ToolName   string
	ArgsJSON   string // raw JSON arguments
	ToolCallID string
	// Metadata carries provider-specific opaque data that must be round-tripped
	// (e.g., Gemini 3.x thought signatures). Providers set this on parse and
	// read it back when serializing tool calls in conversation history.
	Metadata map[string]string
}

ToolCallPart represents the model requesting a tool call.

func (ToolCallPart) ArgsAsMap

func (p ToolCallPart) ArgsAsMap() (map[string]any, error)

ArgsAsMap deserializes the tool call arguments into a map.

type ToolCallPartDelta

type ToolCallPartDelta struct {
	ArgsJSONDelta string
}

ToolCallPartDelta is an incremental tool call argument chunk.

type ToolCalledEvent

type ToolCalledEvent struct {
	RunID       string
	ParentRunID string
	ToolCallID  string
	ToolName    string
	ArgsJSON    string
	CalledAt    time.Time
}

ToolCalledEvent is published when a tool call starts.

func NewToolCalledEvent added in v0.3.0

func NewToolCalledEvent(runID, parentRunID, toolCallID, toolName, argsJSON string, calledAt time.Time) ToolCalledEvent

NewToolCalledEvent constructs a standardized tool-start event.

func (ToolCalledEvent) RuntimeEventType added in v0.3.0

func (e ToolCalledEvent) RuntimeEventType() string

func (ToolCalledEvent) RuntimeOccurredAt added in v0.3.0

func (e ToolCalledEvent) RuntimeOccurredAt() time.Time

func (ToolCalledEvent) RuntimeParentRunID added in v0.3.0

func (e ToolCalledEvent) RuntimeParentRunID() string

func (ToolCalledEvent) RuntimeRunID added in v0.3.0

func (e ToolCalledEvent) RuntimeRunID() string

type ToolChoice

type ToolChoice struct {
	// Mode is "auto" (default), "required" (must use any tool), or "none" (no tools).
	Mode string `json:"mode,omitempty"`
	// ToolName forces use of a specific tool (when Mode is empty or "auto").
	ToolName string `json:"tool_name,omitempty"`
}

ToolChoice controls how the model selects tools.

func ToolChoiceAuto

func ToolChoiceAuto() *ToolChoice

ToolChoiceAuto lets the model decide whether to use tools.

func ToolChoiceForce

func ToolChoiceForce(toolName string) *ToolChoice

ToolChoiceForce forces use of a specific tool by name.

func ToolChoiceNone

func ToolChoiceNone() *ToolChoice

ToolChoiceNone prevents tool use.

func ToolChoiceRequired

func ToolChoiceRequired() *ToolChoice

ToolChoiceRequired forces the model to use a tool.

type ToolDefinition

type ToolDefinition struct {
	Name              string   `json:"name"`
	Description       string   `json:"description,omitempty"`
	ParametersSchema  Schema   `json:"parameters_schema"`
	Kind              ToolKind `json:"kind"`
	Strict            *bool    `json:"strict,omitempty"`
	Sequential        bool     `json:"sequential,omitempty"`
	OuterTypedDictKey string   `json:"outer_typed_dict_key,omitempty"`
}

ToolDefinition describes a tool for the model.

type ToolHandler

type ToolHandler func(ctx context.Context, rc *RunContext, argsJSON string) (any, error)

ToolHandler is the function that executes a tool.

type ToolKind

type ToolKind string

ToolKind classifies tool types.

const (
	// ToolKindFunction is a regular callable tool.
	ToolKindFunction ToolKind = "function"
	// ToolKindOutput is a synthetic tool used to extract structured output.
	ToolKindOutput ToolKind = "output"
)

type ToolOption

type ToolOption func(*toolConfig)

ToolOption configures a tool via functional options.

func WithRequiresApproval

func WithRequiresApproval() ToolOption

WithRequiresApproval marks a tool as requiring human approval before execution.

func WithToolMaxRetries

func WithToolMaxRetries(n int) ToolOption

WithToolMaxRetries sets the maximum retries for a tool.

func WithToolResultValidator

func WithToolResultValidator(fn ToolResultValidatorFunc) ToolOption

WithToolResultValidator sets a result validator on a tool.

func WithToolSequential

func WithToolSequential(seq bool) ToolOption

WithToolSequential marks a tool as requiring sequential execution.

func WithToolStrict

func WithToolStrict(strict bool) ToolOption

WithToolStrict enables strict JSON Schema validation for the tool.

func WithToolTimeout

func WithToolTimeout(d time.Duration) ToolOption

WithToolTimeout sets a maximum execution time for a tool. If the tool exceeds the timeout, it returns context.DeadlineExceeded.

type ToolPrepareFunc

type ToolPrepareFunc func(ctx context.Context, rc *RunContext, def ToolDefinition) *ToolDefinition

ToolPrepareFunc is called before each model request to decide if a tool should be included. Return the (possibly modified) definition to include it, or nil to exclude.

type ToolResultValidatorFunc

type ToolResultValidatorFunc func(ctx context.Context, rc *RunContext, toolName string, result string) error

ToolResultValidatorFunc validates a tool's return value before it becomes a ToolReturnPart in the conversation. Return error to retry the tool call (the error message is sent to the model as a RetryPromptPart).

type ToolResultWithImages added in v0.1.3

type ToolResultWithImages struct {
	Text   string
	Images []ImagePart
}

ToolResultWithImages allows tools to return images alongside text. When a tool handler returns this type, the agent extracts the images and attaches them to the ToolReturnPart so providers can serialize them as multimodal tool results.

type ToolReturnPart

type ToolReturnPart struct {
	ToolName   string
	Content    any // string or structured data (serialized to JSON)
	ToolCallID string
	Timestamp  time.Time
	Images     []ImagePart // optional images to include in tool result
}

ToolReturnPart is the result of a tool call sent back to the model.

type Toolset

type Toolset struct {
	Name                 string
	Tools                []Tool
	Hooks                []Hook
	DynamicSystemPrompts []SystemPromptFunc
}

Toolset groups tools for modular management.

func NewToolset

func NewToolset(name string, tools ...Tool) *Toolset

NewToolset creates a named toolset.

type TraceExporter

type TraceExporter interface {
	Export(ctx context.Context, trace *RunTrace) error
}

TraceExporter exports a completed RunTrace to an external system.

type TraceStep

type TraceStep struct {
	Kind      TraceStepKind `json:"kind"`
	Timestamp time.Time     `json:"timestamp"`
	Duration  time.Duration `json:"duration"`
	Data      any           `json:"data"`
}

TraceStep captures a single step in the agent execution.

type TraceStepKind

type TraceStepKind string

TraceStepKind identifies the type of trace step.

const (
	TraceModelRequest  TraceStepKind = "model_request"
	TraceModelResponse TraceStepKind = "model_response"
	TraceToolCall      TraceStepKind = "tool_call"
	TraceToolResult    TraceStepKind = "tool_result"
	TraceGuardrail     TraceStepKind = "guardrail"
)

type TruncationConfig added in v0.1.9

type TruncationConfig struct {
	// MaxTokens is the maximum estimated tokens per tool output.
	// Content exceeding this budget is truncated with a head/tail strategy.
	// Default: 16000 (~64KB of text).
	MaxTokens int
}

TruncationConfig controls how tool outputs are truncated before being recorded into conversation history.

func DefaultTruncationConfig added in v0.1.9

func DefaultTruncationConfig() TruncationConfig

DefaultTruncationConfig returns the default truncation settings.

type TurnGuardrailFunc

type TurnGuardrailFunc func(ctx context.Context, rc *RunContext, messages []ModelMessage) error

TurnGuardrailFunc validates messages before each model request within the run loop. Return error to abort the run. This catches issues that develop during multi-turn conversations.

func MaxTurns

func MaxTurns(n int) TurnGuardrailFunc

MaxTurns aborts the run if it exceeds N model requests.

type TurnGuardrailRuntime added in v0.2.0

type TurnGuardrailRuntime struct {
	Name string
	Func TurnGuardrailFunc
}

TurnGuardrailRuntime captures a named turn guardrail for alternative execution backends.

type UnexpectedModelBehavior

type UnexpectedModelBehavior struct {
	Message string
	Body    string // raw response body, if available
}

UnexpectedModelBehavior indicates the model responded in an unexpected way.

func (*UnexpectedModelBehavior) Error

func (e *UnexpectedModelBehavior) Error() string

type Usage

type Usage struct {
	InputTokens      int            `json:"input_tokens,omitempty"`
	OutputTokens     int            `json:"output_tokens,omitempty"`
	CacheWriteTokens int            `json:"cache_write_tokens,omitempty"`
	CacheReadTokens  int            `json:"cache_read_tokens,omitempty"`
	Details          map[string]int `json:"details,omitempty"`
}

Usage tracks token counts for a single model request.

func (*Usage) Incr

func (u *Usage) Incr(other Usage)

Incr adds another Usage to this one in place.

func (Usage) TotalTokens

func (u Usage) TotalTokens() int

TotalTokens returns the sum of input and output tokens.

type UsageLimitExceeded

type UsageLimitExceeded struct {
	Message string
}

UsageLimitExceeded is returned when usage exceeds configured limits.

func (*UsageLimitExceeded) Error

func (e *UsageLimitExceeded) Error() string

type UsageLimits

type UsageLimits struct {
	RequestLimit      *int
	InputTokensLimit  *int
	OutputTokensLimit *int
	TotalTokensLimit  *int
	ToolCallsLimit    *int // Maximum total tool calls per run.
}

UsageLimits defines constraints on model usage. Nil fields mean no limit.

func DefaultUsageLimits

func DefaultUsageLimits() UsageLimits

DefaultUsageLimits returns limits with RequestLimit=50.

func (UsageLimits) CheckBeforeRequest

func (l UsageLimits) CheckBeforeRequest(usage RunUsage) error

CheckBeforeRequest checks if the next request would exceed the request limit.

func (UsageLimits) CheckTokens

func (l UsageLimits) CheckTokens(usage RunUsage) error

CheckTokens checks if token usage exceeds any configured token limits.

func (UsageLimits) CheckToolCalls

func (l UsageLimits) CheckToolCalls(usage RunUsage) error

CheckToolCalls checks if tool call usage exceeds the configured limit.

func (UsageLimits) HasTokenLimits

func (l UsageLimits) HasTokenLimits() bool

HasTokenLimits returns true if any token limit is configured.

type UsageQuota

type UsageQuota struct {
	MaxRequests     int // max model requests (0 = unlimited)
	MaxTotalTokens  int // max total tokens across all requests (0 = unlimited)
	MaxInputTokens  int // max input/prompt tokens (0 = unlimited)
	MaxOutputTokens int // max output/completion tokens (0 = unlimited)
}

UsageQuota defines hard limits that terminate a run when exceeded.

type UserError

type UserError struct {
	Message string
}

UserError represents a developer usage mistake.

func (*UserError) Error

func (e *UserError) Error() string

type UserPromptPart

type UserPromptPart struct {
	Content   string
	Timestamp time.Time
}

UserPromptPart contains the user's input.

Directories

Path Synopsis
Package memory provides history processing strategies for managing conversation context in agents, including sliding window, token budget, and summary-based memory.
Package memory provides history processing strategies for managing conversation context in agents, including sliding window, token budget, and summary-based memory.
Package orchestration provides multi-agent coordination patterns including agent-as-tool delegation, handoff pipelines, chain composition, and general-purpose pipeline steps.
Package orchestration provides multi-agent coordination patterns including agent-as-tool delegation, handoff pipelines, chain composition, and general-purpose pipeline steps.
Package streamutil provides helper functions for consuming streamed text responses from agents, including delta mode, accumulated mode, and debounced streaming.
Package streamutil provides helper functions for consuming streamed text responses from agents, including delta mode, accumulated mode, and debounced streaming.

Jump to

Keyboard shortcuts

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