Documentation
¶
Index ¶
- Constants
- func IsRetryable(err error) bool
- type Agent
- func (a *Agent) NewSession(id string) *Session
- func (a *Agent) Note(_ context.Context, note string) (Message, error)
- func (a *Agent) Run(ctx context.Context, input string) (*Result, error)
- func (a *Agent) RunMessages(ctx context.Context, msgs ...Message) (*Result, error)
- func (a *Agent) RunMessagesStream(ctx context.Context, msgs ...Message) (EventStream, error)
- func (a *Agent) RunStream(ctx context.Context, input string) (EventStream, error)
- type Capabilities
- type Capable
- type Compactor
- type ContentBlock
- type ConversationStore
- type DocumentBlock
- type Error
- type ErrorCode
- type Event
- type EventStream
- type EventType
- type Hooks
- type ImageBlock
- type ImageSource
- type Message
- type Option
- func WithCompactor(c Compactor, thresholdTokens int) Option
- func WithConversationStore(s ConversationStore) Option
- func WithHooks(h Hooks) Option
- func WithMaxIterations(n int) Option
- func WithMaxParallelTools(n int) Option
- func WithMaxTokens(n int) Option
- func WithModel(model string) Option
- func WithProvider(p Provider) Option
- func WithRetryPolicy(rp RetryPolicy) Option
- func WithStreamingFallback(mode StreamingFallbackMode) Option
- func WithSystemPrompt(sp *SystemPrompt) Option
- func WithThinking(cfg ThinkingConfig) Option
- func WithToolChoice(tc ToolChoice) Option
- func WithTools(tools ...RegisteredTool) Option
- type Provider
- type RegisteredTool
- type Request
- type Response
- type Result
- type RetryPolicy
- type Role
- type Session
- type SourceKind
- type StopReason
- type StreamingFallbackMode
- type StreamingProvider
- type SystemBlock
- type SystemPrompt
- func (s *SystemPrompt) Add(text string) *SystemPrompt
- func (s *SystemPrompt) AddCacheable(text string) *SystemPrompt
- func (s *SystemPrompt) AddFunc(fn func(ctx context.Context) (string, error)) *SystemPrompt
- func (s *SystemPrompt) AddTemplate(tmpl string, data any) *SystemPrompt
- func (s *SystemPrompt) Render(ctx context.Context) ([]SystemBlock, error)
- type SystemUpdater
- type TextBlock
- type ThinkingBlock
- type ThinkingConfig
- type ThinkingMode
- type TokenCounter
- type Tool
- type ToolCall
- type ToolChoice
- type ToolChoiceMode
- type ToolResult
- type ToolResultBlock
- type ToolSet
- type ToolUseBlock
- type Usage
Constants ¶
const DefaultMaxIterations = 25
DefaultMaxIterations bounds how many model round-trips a single Run may take before it gives up with ErrMaxIterations. It exists specifically so a misbehaving tool or an unexpectedly chatty model can't loop forever; combine with a context deadline for a wall-clock bound as well.
Variables ¶
This section is empty.
Functions ¶
func IsRetryable ¶
IsRetryable reports whether err is (or wraps) an *Error marked Retryable.
Types ¶
type Agent ¶
type Agent struct {
// contains filtered or unexported fields
}
Agent binds a Provider, model, system prompt, and tool set into a bounded tool-use loop. Agent is safe for concurrent use by multiple goroutines once constructed (no field is mutated after New returns) — the common case of one *Agent shared across many HTTP requests works without external locking.
func New ¶
New builds an Agent from the given options. WithProvider is effectively required — an Agent with no provider returns an error from every Run call rather than panicking.
func (*Agent) NewSession ¶
NewSession returns a Session bound to id, using a.store for persistence.
func (*Agent) Note ¶
Note delivers an operator instruction mid-conversation via Session.Send's next call. If the provider implements SystemUpdater, the instruction uses the provider's native mechanism (preserving any cached prefix); otherwise it is queued as a synthetic reminder block prepended to the next user turn, so the same call works across every provider with best-available fidelity.
Note is a placeholder for the mid-conversation-system-message feature described in the design document (Phase 4); the current implementation covers the SystemUpdater-aware path and is expected to grow the synthetic-fallback queuing in a follow-up change.
func (*Agent) Run ¶
Run sends input as a single user turn (with no prior history) and runs the tool-use loop until the model produces a final answer.
func (*Agent) RunMessages ¶
RunMessages runs the tool-use loop starting from an explicit message history (e.g. a prior conversation loaded from a Session/ConversationStore).
func (*Agent) RunMessagesStream ¶
RunMessagesStream is the streaming counterpart to RunMessages.
func (*Agent) RunStream ¶
RunStream is the streaming counterpart to Run: it starts from a single user turn and returns one logical EventStream for the entire run, including any tool round-trips — the caller sees a single stream from first token to final answer no matter how many provider calls or tool executions happen underneath.
type Capabilities ¶
type Capabilities struct {
Streaming bool
Tools bool
ParallelToolCalls bool
Vision bool
Documents bool
Thinking bool
SystemCaching bool
MidConversationSystem bool
MaxContextTokens int // 0 = unknown/unbounded
MaxOutputTokens int // 0 = unknown
}
Capabilities describes what a Provider supports. Zero values are always conservative ("not supported" / "unknown"), matching the Capabilities{} returned by CapabilitiesOf for a Provider that doesn't implement Capable.
func CapabilitiesOf ¶
func CapabilitiesOf(p Provider) Capabilities
CapabilitiesOf returns p's declared capabilities, or a zero-value Capabilities{} if p does not implement Capable. Callers should always go through this function rather than a raw type assertion, so behavior stays consistent for minimal providers that skip Capable entirely.
type Capable ¶
type Capable interface {
Capabilities() Capabilities
}
Capable is implemented by providers that want to declare what they support, so Agent can validate configuration early — e.g. reject WithThinking() against a provider with no thinking support at construction time, instead of surfacing a confusing error from the wire on the first call.
type Compactor ¶ added in v0.1.1
Compactor reduces a conversation's message history, e.g. to stay under a token budget. Implementations are free to summarize, truncate, or drop messages; the returned slice replaces history for the upcoming turn and is what Session.Send persists afterward. Left pluggable deliberately: some providers have native server-side compaction (out of scope to reimplement here), others need a client-side summarization pass — the interface accommodates either without Session knowing which. See NewWindowCompactor for a dependency-free reference implementation.
func NewWindowCompactor ¶ added in v0.1.1
NewWindowCompactor returns a Compactor that keeps only the most recent maxMessages messages, dropping the rest. It is tool-pairing-aware: if the naive cut point would keep a ToolResultBlock whose originating ToolUseBlock falls outside the window, that message (and any others like it at the front of the kept slice) is dropped too — every first-class provider rejects a tool result with no matching call in the same request, so keeping an orphaned one would break the next turn rather than merely lose context.
This is a blunt strategy: dropped messages are gone, not summarized. It needs no extra model call and no dependencies, making it a reasonable default and a template for a smarter (e.g. summarizing) Compactor.
type ContentBlock ¶
type ContentBlock interface {
// contains filtered or unexported methods
}
ContentBlock is a closed sum type covering every content shape a provider-agnostic Message can carry: text, images, documents, tool calls, tool results, and extended-thinking content. New concrete types are added only inside this package; application code consumes ContentBlock through a type switch.
type ConversationStore ¶
type ConversationStore interface {
Load(ctx context.Context, sessionID string) ([]Message, error)
Save(ctx context.Context, sessionID string, msgs []Message) error
}
ConversationStore persists a session's message history between calls. Implement this to back Session with Redis, Postgres, a file, etc.; the zero-config default is NewInMemoryStore.
func NewInMemoryStore ¶
func NewInMemoryStore() ConversationStore
NewInMemoryStore returns a ConversationStore backed by a process-local map. Fine for CLIs, tests, and single-process use; not shared across processes or persisted across restarts.
type DocumentBlock ¶
type DocumentBlock struct {
Source ImageSource
Title string
}
DocumentBlock carries a document (e.g. a PDF) for models that support document understanding.
type Error ¶
type Error struct {
Code ErrorCode
Provider string // the Provider.Name() that produced this error, if any
Message string
// Retryable indicates whether retrying the same request may succeed.
Retryable bool
// RetryAfter is honored when the provider supplied an explicit delay
// (e.g. a 429 Retry-After header); zero means "use the configured
// RetryPolicy's computed backoff instead".
RetryAfter time.Duration
// Cause is the wrapped original error, including the provider SDK's own
// error type where applicable.
Cause error
}
Error is the unified error type returned by Provider implementations and by the Agent run loop. Use errors.As to recover one from a wrapped error.
type ErrorCode ¶
type ErrorCode string
ErrorCode is a provider-agnostic classification of what went wrong. Every first-class provider adapter maps its vendor-specific error taxonomy onto this common set so application code can write one switch regardless of which provider is behind an Agent.
const ( ErrAuthentication ErrorCode = "authentication_error" ErrPermission ErrorCode = "permission_error" ErrInvalidRequest ErrorCode = "invalid_request_error" ErrRateLimited ErrorCode = "rate_limit_error" ErrOverloaded ErrorCode = "overloaded_error" ErrContextExceeded ErrorCode = "context_length_exceeded" ErrRefusal ErrorCode = "refusal" ErrProviderInternal ErrorCode = "provider_error" ErrMaxIterations ErrorCode = "max_iterations_exceeded" ErrStreamUnsupported ErrorCode = "streaming_unsupported" ErrNotFound ErrorCode = "not_found_error" ErrUnknown ErrorCode = "unknown_error" )
type Event ¶
type Event struct {
Type EventType
TextDelta string
ThinkingDelta string
ToolCall *ToolCall // set on tool_call_* and tool_result events
ToolResult *ToolResult // set on tool_result
Response *Response // set on message_done
Result *Result // set on run_done
}
Event is one item in an EventStream. Only the fields relevant to Type are populated; the rest are zero values.
type EventStream ¶
EventStream is returned by StreamingProvider.Stream and Agent.RunStream. Next returns io.EOF once the stream is exhausted, mirroring sql.Rows / bufio.Scanner conventions: the caller drives iteration, and ctx cancellation is checked exactly where the caller expects it.
func NewSliceStream ¶
func NewSliceStream(events ...Event) EventStream
NewSliceStream returns an EventStream that yields events in order, then io.EOF. Exported for use by provider adapters and tests that need to hand back a ready-made stream without implementing EventStream themselves.
type EventType ¶
type EventType string
EventType identifies the kind of a streamed Event.
const ( EventTextDelta EventType = "text_delta" EventThinkingDelta EventType = "thinking_delta" EventToolCallStart EventType = "tool_call_start" EventToolCallDelta EventType = "tool_call_delta" // streamed partial JSON input EventToolCallEnd EventType = "tool_call_end" EventToolResult EventType = "tool_result" // emitted after Agent executes a tool EventMessageDone EventType = "message_done" // one full assistant turn complete EventRunDone EventType = "run_done" // the whole Run/RunStream finished )
type Hooks ¶
type Hooks struct {
// BeforeGenerate runs before every provider call. Returning a non-nil
// error aborts the run without calling the provider.
BeforeGenerate func(ctx context.Context, req *Request) error
// AfterGenerate runs after every successful provider call.
AfterGenerate func(ctx context.Context, resp *Response)
// BeforeToolCall runs before a tool is invoked. Returning allow=false
// skips the tool entirely; if override is non-nil, its ToolResult is
// used as the tool's result (e.g. to synthesize a "denied by policy"
// response the model can react to). If override is nil and allow is
// false, a generic denial result is used.
BeforeToolCall func(ctx context.Context, call ToolCall) (allow bool, override *ToolResult)
// AfterToolCall runs after a tool has been invoked (whether it
// succeeded, returned a model-recoverable error, or was denied by
// BeforeToolCall).
AfterToolCall func(ctx context.Context, call ToolCall, result ToolResult)
// OnError runs whenever the run loop is about to return a fatal error
// (a provider error that exhausted retries, a tool handler's Go error,
// max iterations exceeded, etc.).
OnError func(ctx context.Context, err error)
}
Hooks are optional callbacks the Agent run loop invokes at well-defined points, covering the practical needs of a production agent: structured logging/metrics, tracing, human-in-the-loop tool approval, and error observation. Every field is optional; a nil hook is simply skipped.
type ImageBlock ¶
type ImageBlock struct {
Source ImageSource
}
ImageBlock carries an image for vision-capable models.
type ImageSource ¶
type ImageSource struct {
Kind SourceKind
MediaType string // e.g. "image/png"; ignored when Kind == SourceURL
Data string // base64 payload, or the URL, depending on Kind
}
ImageSource describes where image or document bytes come from.
type Message ¶
type Message struct {
Role Role
Content []ContentBlock
}
Message is one turn in a conversation.
func AssistantMessage ¶
func AssistantMessage(blocks ...ContentBlock) Message
AssistantMessage builds an assistant Message out of arbitrary content blocks. Mainly useful in tests and when hand-constructing few-shot examples in conversation history.
func UserMessage ¶
UserMessage builds a single-block plain-text user Message.
func UserMessageBlocks ¶
func UserMessageBlocks(blocks ...ContentBlock) Message
UserMessageBlocks builds a user Message out of arbitrary content blocks (e.g. text plus an image).
func (Message) Text ¶
Text concatenates every TextBlock in the message, in order. Convenience for the common case of reading a plain-text response.
func (Message) ToolUses ¶
func (m Message) ToolUses() []ToolUseBlock
ToolUses returns every ToolUseBlock in the message, in order.
type Option ¶
type Option func(*Agent)
Option configures an Agent. Adding a new knob to Agent never breaks an existing call site, since options are applied by name, not position.
func WithCompactor ¶ added in v0.1.1
WithCompactor enables automatic history compaction on Session.Send: when the provider implements TokenCounter and a session's estimated token count is at or above thresholdTokens, c is invoked to shrink history before the turn runs, and the compacted history is what gets persisted. Unset by default — compaction is lossy, so it's an explicit opt-in rather than an automatic behavior change. See Compactor and NewWindowCompactor.
func WithConversationStore ¶
func WithConversationStore(s ConversationStore) Option
WithConversationStore sets the backing store used by Agent.NewSession. Defaults to an in-memory store.
func WithMaxIterations ¶
WithMaxIterations bounds how many model round-trips a single Run/RunStream call may take before it gives up with ErrMaxIterations. Defaults to DefaultMaxIterations. This is the hard backstop against a runaway tool loop; combine with a context deadline for a wall-clock bound as well.
func WithMaxParallelTools ¶
WithMaxParallelTools bounds how many tool calls from a single model turn run concurrently. Zero (the default) means unbounded — every tool call in a turn runs at once, which is fine for typical single-digit tool-call counts but worth bounding when individual tools are resource-heavy (e.g. each spawns a subprocess).
func WithMaxTokens ¶
WithMaxTokens sets the maximum tokens the model may generate per turn.
func WithModel ¶
WithModel sets the model identifier passed to the provider on every request (e.g. "claude-opus-4-8", "gpt-4.1", "gemini-2.5-pro").
func WithProvider ¶
WithProvider sets the inference backend. Effectively required — an Agent built without one returns an error from every Run/RunStream call.
func WithRetryPolicy ¶
func WithRetryPolicy(rp RetryPolicy) Option
WithRetryPolicy overrides the default retry/backoff policy applied to retryable provider errors. See RetryPolicy and IsRetryable.
func WithStreamingFallback ¶
func WithStreamingFallback(mode StreamingFallbackMode) Option
WithStreamingFallback controls RunStream's behavior against a provider that does not implement StreamingProvider. Defaults to FallbackSingleShot.
func WithSystemPrompt ¶
func WithSystemPrompt(sp *SystemPrompt) Option
WithSystemPrompt sets the agent's system instructions. See SystemPrompt for composing static, cacheable, and dynamic sections.
func WithThinking ¶
func WithThinking(cfg ThinkingConfig) Option
WithThinking enables extended reasoning per cfg. Leave unset (the default) to run without extended thinking.
func WithToolChoice ¶
func WithToolChoice(tc ToolChoice) Option
WithToolChoice controls whether/how the model must use a tool. Defaults to ToolChoiceAuto.
func WithTools ¶
func WithTools(tools ...RegisteredTool) Option
WithTools registers the tools available to the model. Calling WithTools more than once appends to, rather than replaces, the existing tool set.
type Provider ¶
type Provider interface {
// Name identifies the provider, e.g. "claude", "openai", "gemini". Used
// in error messages and observability hooks.
Name() string
// Generate performs a single, non-streaming inference call.
Generate(ctx context.Context, req *Request) (*Response, error)
}
Provider is the only interface a backend must implement to be usable by Agent. Everything else in this file is additive: a minimal provider that implements just this interface already gets a working Run loop, tool execution, hooks, and retries. Streaming, capability negotiation, and token counting are unlocked by implementing the optional interfaces below as needed — see StreamingProvider, Capable, and TokenCounter.
type RegisteredTool ¶
type RegisteredTool interface {
Name() string
Description() string
Schema() *schema.Schema
Invoke(ctx context.Context, input json.RawMessage) (ToolResult, error)
}
RegisteredTool is the type-erased interface the Agent run loop and every provider adapter operate on. Tool[TIn] implements it; so can any other type — e.g. a bridge to a tool whose schema is discovered at runtime rather than known at compile time (an MCP server, a plugin registry).
type Request ¶
type Request struct {
Model string
System []SystemBlock
Messages []Message
Tools []RegisteredTool
ToolChoice ToolChoice
MaxTokens int
Thinking *ThinkingConfig
// Metadata is free-form and provider-specific; adapters may ignore keys
// they don't understand.
Metadata map[string]string
}
Request is the provider-agnostic shape of a single inference call. Every Provider.Generate/Stream implementation translates a Request into that vendor's wire format and back.
type Response ¶
type Response struct {
ID string
Model string
Message Message // Role is always RoleAssistant
StopReason StopReason
Usage Usage
// Raw is the provider-native response object (e.g. *anthropic.Message,
// *openai.ChatCompletion, *genai.GenerateContentResponse). It is an
// escape hatch for provider-specific fields not yet promoted into the
// unified model; code that reads it is coupled to that provider by
// definition, and the core Agent run loop never reads it.
Raw any
}
Response is the provider-agnostic shape of a single inference result.
type Result ¶
Result is what Run/RunMessages return: the final response plus the full transcript (including any tool round-trips) appended during this run.
type RetryPolicy ¶
type RetryPolicy struct {
MaxRetries int
BaseDelay time.Duration
MaxDelay time.Duration
Jitter bool
}
RetryPolicy controls how Agent retries a Provider.Generate/Stream call that failed with a retryable error (see IsRetryable). Errors that are not retryable (invalid request, authentication, refusal, ...) are never retried regardless of policy.
func DefaultRetryPolicy ¶
func DefaultRetryPolicy() RetryPolicy
DefaultRetryPolicy returns a conservative default: 2 retries, exponential backoff starting at 500ms, capped at 20s, with jitter.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session binds an Agent to a persistent conversation identified by id, loading and saving history via the Agent's ConversationStore around every Send/SendStream call. Session is not safe for concurrent Send calls on the same session ID — conversation history has an inherent sequential dependency; synchronize at the application layer (e.g. a per-session mutex or single-writer queue) if concurrent turns on one session are possible.
func (*Session) Send ¶
Send appends input as a user turn to the session's history, runs the agent, persists the updated history (including tool round-trips), and returns the result. If a Compactor is configured (see WithCompactor), the loaded history is compacted first whenever it crosses the configured token threshold, and the compacted (not the original) history is what gets persisted.
type SourceKind ¶
type SourceKind string
SourceKind describes how ImageBlock/DocumentBlock data is encoded.
const ( SourceBase64 SourceKind = "base64" SourceURL SourceKind = "url" )
type StopReason ¶
type StopReason string
StopReason explains why the model stopped generating.
const ( StopEndTurn StopReason = "end_turn" StopMaxTokens StopReason = "max_tokens" StopToolUse StopReason = "tool_use" StopRefusal StopReason = "refusal" StopContentFilter StopReason = "content_filter" StopUnknown StopReason = "unknown" )
type StreamingFallbackMode ¶
type StreamingFallbackMode string
StreamingFallbackMode controls Agent.RunStream's behavior against a Provider that does not implement StreamingProvider.
const ( // FallbackSingleShot (the default) performs one blocking Generate call // per turn and emits its content as a single burst of events, so // RunStream still returns a working, if non-incremental, stream rather // than an error. FallbackSingleShot StreamingFallbackMode = "single_shot" // FallbackError makes RunStream return an ErrStreamUnsupported error // immediately against a non-streaming provider, for callers that need // to know streaming is unavailable rather than silently degrading. FallbackError StreamingFallbackMode = "error" )
type StreamingProvider ¶
type StreamingProvider interface {
Provider
Stream(ctx context.Context, req *Request) (EventStream, error)
}
StreamingProvider is implemented by providers that can stream a response incrementally. If a Provider does not implement it, Agent.RunStream still works via a documented fallback (see WithStreamingFallback).
type SystemBlock ¶
type SystemBlock struct {
Text string
// Cacheable hints that a provider with prompt-caching support should
// cache this block. Providers without caching support ignore it.
Cacheable bool
}
SystemBlock is one section of a rendered system prompt. See SystemPrompt in system.go for the composable builder that produces these.
type SystemPrompt ¶
type SystemPrompt struct {
// contains filtered or unexported fields
}
SystemPrompt is a composable, ordered builder for system instructions. Each section is added independently (and can be unit-tested independently) and rendered fresh on every Agent.Run/RunStream call, so AddFunc/AddTemplate sections always reflect current state.
Ordering matters for providers with prompt caching: caching is a prefix match, so put static/cacheable sections first (via Add/AddCacheable) and per-request dynamic sections last (via AddFunc/AddTemplate) — that way a change in the dynamic tail never invalidates the cached prefix.
func NewSystemPrompt ¶
func NewSystemPrompt() *SystemPrompt
NewSystemPrompt returns an empty SystemPrompt ready for chaining.
func (*SystemPrompt) Add ¶
func (s *SystemPrompt) Add(text string) *SystemPrompt
Add appends a static, non-cacheable instruction block.
func (*SystemPrompt) AddCacheable ¶
func (s *SystemPrompt) AddCacheable(text string) *SystemPrompt
AddCacheable appends a static instruction block hinted as cacheable — use this for large, stable content (few-shot examples, a knowledge-base dump, tool-usage policy) that doesn't change between requests. Providers without prompt-caching support simply ignore the hint.
func (*SystemPrompt) AddFunc ¶
func (s *SystemPrompt) AddFunc(fn func(ctx context.Context) (string, error)) *SystemPrompt
AddFunc appends a block computed at render time — e.g. the current date, the authenticated user's name, feature flags. Evaluated fresh on every Run/RunStream call.
func (*SystemPrompt) AddTemplate ¶
func (s *SystemPrompt) AddTemplate(tmpl string, data any) *SystemPrompt
AddTemplate renders a text/template with data at render time. Convenience wrapper over AddFunc for the common "instructions with placeholders" case. The template is parsed once (at first render) and cached.
func (*SystemPrompt) Render ¶
func (s *SystemPrompt) Render(ctx context.Context) ([]SystemBlock, error)
Render evaluates every section, in order, into the final list of SystemBlock a Request carries.
type SystemUpdater ¶
type SystemUpdater interface {
// SystemUpdateMessage returns the Message to append to history in order
// to deliver note using this provider's native mechanism.
SystemUpdateMessage(note string) (Message, error)
}
SystemUpdater is implemented by providers that support injecting an operator instruction mid-conversation without rebuilding the system prompt (and, on providers with prompt caching, without invalidating the cached prefix). Agent.Note uses it when available and falls back to a synthetic reminder block otherwise, so the same call works everywhere.
type TextBlock ¶
type TextBlock struct {
Text string
}
TextBlock is plain text content, the most common block type.
type ThinkingBlock ¶
ThinkingBlock carries extended-reasoning content produced by the model. Signature is opaque and provider-specific; when a provider requires it echoed back unmodified on a later turn, the Agent run loop does so automatically and application code never needs to inspect it.
type ThinkingConfig ¶
type ThinkingConfig struct {
Mode ThinkingMode
Budget int // consulted only when Mode == ThinkingBudgeted
}
ThinkingConfig configures extended/deep reasoning for a request. Providers that don't support thinking simply ignore it; providers whose only mode is adaptive treat ThinkingBudgeted as ThinkingAdaptive.
type ThinkingMode ¶
type ThinkingMode string
ThinkingMode selects how a model should use extended reasoning.
const ( // ThinkingOff disables extended thinking (the default when Thinking is nil). ThinkingOff ThinkingMode = "off" // ThinkingAdaptive lets the provider decide when and how much to think. ThinkingAdaptive ThinkingMode = "adaptive" // ThinkingBudgeted requests a fixed thinking-token budget, for providers // that only support the legacy fixed-budget form. ThinkingBudgeted ThinkingMode = "budgeted" )
type TokenCounter ¶
TokenCounter is implemented by providers with a token-counting endpoint. Agent uses it, when present, for pre-flight context-window checks and cost-estimation helpers; it is never required.
type Tool ¶
type Tool[TIn any] struct { // contains filtered or unexported fields }
Tool[TIn] is the primary, strongly-typed way to define a tool. TIn is a plain Go struct describing the tool's input; its JSON Schema is derived once via reflection (see the schema package) from `json` and `jsonschema` struct tags and cached. The handler receives a fully-typed, already unmarshalled TIn — there is no map[string]any and no manual type assertion anywhere in a tool's implementation.
func NewTool ¶
func NewTool[TIn any](name, description string, handler func(ctx context.Context, in TIn) (ToolResult, error)) *Tool[TIn]
NewTool defines a tool named name, described by description (which the model uses to decide when to call it — be specific about *when*, not just what the tool does), backed by handler.
func (*Tool[TIn]) Description ¶
func (*Tool[TIn]) Invoke ¶
func (t *Tool[TIn]) Invoke(ctx context.Context, raw json.RawMessage) (ToolResult, error)
Invoke unmarshals raw into TIn and calls the handler. Malformed input (the model sent something that doesn't match the schema) never panics and never returns a Go error — it becomes a model-recoverable error ToolResult, since a Go error return is reserved for handler-level failures that should abort the run (see Hooks and the Agent run loop).
type ToolCall ¶
type ToolCall struct {
ID string
Name string
Input json.RawMessage
}
ToolCall describes a single tool invocation, passed to Hooks.
type ToolChoice ¶
type ToolChoice struct {
Mode ToolChoiceMode
Name string // required when Mode == ToolChoiceOne
}
ToolChoice controls tool-use behavior for a single request.
type ToolChoiceMode ¶
type ToolChoiceMode string
ToolChoiceMode controls whether/how the model must use tools.
const ( // ToolChoiceAuto lets the model decide whether to use a tool (default). ToolChoiceAuto ToolChoiceMode = "auto" // ToolChoiceAny forces the model to use some tool, any tool. ToolChoiceAny ToolChoiceMode = "any" // ToolChoiceOne forces the model to use the tool named by ToolChoice.Name. ToolChoiceOne ToolChoiceMode = "tool" // ToolChoiceNone disables tool use for this request even if tools are // attached (useful for a final "summarize" turn). ToolChoiceNone ToolChoiceMode = "none" )
type ToolResult ¶
type ToolResult struct {
Content []ContentBlock
IsError bool
}
ToolResult is what a tool handler returns. It becomes a ToolResultBlock in the next request sent to the model.
func ErrorResultf ¶
func ErrorResultf(format string, args ...any) ToolResult
ErrorResultf builds an error ToolResult with a formatted message. This is a model-recoverable error (the model sees it and can try something else, or explain the failure to the user) — distinct from a Go error returned from a tool handler, which the Agent run loop treats as fatal.
func JSONResult ¶
func JSONResult(v any) ToolResult
JSONResult marshals v and returns it as a successful ToolResult. If marshaling fails, an error ToolResult is returned instead (never a Go error — a tool that can't format its own output should be recoverable by the model, not fatal to the run).
func TextResult ¶
func TextResult(text string) ToolResult
TextResult builds a successful, plain-text ToolResult.
type ToolResultBlock ¶
type ToolResultBlock struct {
ToolUseID string
Content []ContentBlock
IsError bool
}
ToolResultBlock carries a tool's output back to the model. It is sent in a user-role Message, addressed to a specific ToolUseBlock by ID.
type ToolSet ¶
type ToolSet struct {
// contains filtered or unexported fields
}
ToolSet is a small ordered collection of RegisteredTool, primarily useful when an application assembles its tool list from more than a couple of literals (e.g. built up conditionally, or shared across multiple agents).
func NewToolSet ¶
func NewToolSet(tools ...RegisteredTool) *ToolSet
NewToolSet builds a ToolSet from the given tools, in order. Duplicate names overwrite earlier entries but keep their original position.
func (*ToolSet) Add ¶
func (ts *ToolSet) Add(t RegisteredTool) *ToolSet
Add registers t, replacing any existing tool with the same name.
func (*ToolSet) Get ¶
func (ts *ToolSet) Get(name string) (RegisteredTool, bool)
Get returns the tool registered under name, if any.
func (*ToolSet) List ¶
func (ts *ToolSet) List() []RegisteredTool
List returns every registered tool, in registration order.
type ToolUseBlock ¶
type ToolUseBlock struct {
ID string
Name string
Input []byte // raw JSON object, as sent by the model
}
ToolUseBlock is emitted by the assistant when it wants a tool invoked.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package agenttest provides a scriptable agent.Provider for testing application code built on go-agent — agent wiring, tool registration, hooks, and run-loop behavior — without any network calls or API cost.
|
Package agenttest provides a scriptable agent.Provider for testing application code built on go-agent — agent wiring, tool registration, hooks, and run-loop behavior — without any network calls or API cost. |
|
examples
|
|
|
customprovider
command
Command customprovider shows the minimum needed to bring up a new inference backend: implement agent.Provider's single required method, Generate.
|
Command customprovider shows the minimum needed to bring up a new inference backend: implement agent.Provider's single required method, Generate. |
|
multiprovider
command
Command multiprovider shows that Agent code is identical regardless of which first-class provider backs it — only construction differs.
|
Command multiprovider shows that Agent code is identical regardless of which first-class provider backs it — only construction differs. |
|
quickstart
command
Command quickstart is the smallest possible go-agent program: construct a provider, build an Agent, and run a single turn.
|
Command quickstart is the smallest possible go-agent program: construct a provider, build an Agent, and run a single turn. |
|
streaming
command
Command streaming shows Agent.RunStream: a single logical event stream for a whole run, including any tool round-trips.
|
Command streaming shows Agent.RunStream: a single logical event stream for a whole run, including any tool round-trips. |
|
tools
command
Command tools demonstrates strongly-typed tool registration: the model's tool input is unmarshalled directly into a plain Go struct — no map[string]any, no manual JSON Schema.
|
Command tools demonstrates strongly-typed tool registration: the model's tool input is unmarshalled directly into a plain Go struct — no map[string]any, no manual JSON Schema. |
|
Package filestore implements agent.ConversationStore backed by one JSON file per session on local disk.
|
Package filestore implements agent.ConversationStore backed by one JSON file per session on local disk. |
|
internal
|
|
|
providererr
Package providererr holds the HTTP-status-to-agent.ErrorCode mapping shared verbatim by every first-class provider adapter's errors.go.
|
Package providererr holds the HTTP-status-to-agent.ErrorCode mapping shared verbatim by every first-class provider adapter's errors.go. |
|
providererrtest
Package providererrtest holds assertions shared by every first-class provider's errors_test.go.
|
Package providererrtest holds assertions shared by every first-class provider's errors_test.go. |
|
skilltool
command
Command skilltool keeps the go-agent Skill in sync across the vendor directories that need a physical copy of it (Claude Code, Codex — see docs/AGENT-SKILL-PLAN.md) and checks the skill's Go code fences for drift against the real module.
|
Command skilltool keeps the go-agent Skill in sync across the vendor directories that need a physical copy of it (Claude Code, Codex — see docs/AGENT-SKILL-PLAN.md) and checks the skill's Go code fences for drift against the real module. |
|
Package otelagent provides an OpenTelemetry tracing agent.Hooks implementation.
|
Package otelagent provides an OpenTelemetry tracing agent.Hooks implementation. |
|
provider
|
|
|
claude
Package claude adapts the official Anthropic Go SDK (github.com/anthropics/anthropic-sdk-go) to the agent.Provider interface family, giving it first-class status alongside the openai and gemini adapters: Generate, Stream, CountTokens, and Capabilities are all implemented.
|
Package claude adapts the official Anthropic Go SDK (github.com/anthropics/anthropic-sdk-go) to the agent.Provider interface family, giving it first-class status alongside the openai and gemini adapters: Generate, Stream, CountTokens, and Capabilities are all implemented. |
|
gemini
Package gemini adapts the official Google GenAI Go SDK (google.golang.org/genai) to the agent.Provider interface family, giving it first-class status alongside the claude and openai adapters.
|
Package gemini adapts the official Google GenAI Go SDK (google.golang.org/genai) to the agent.Provider interface family, giving it first-class status alongside the claude and openai adapters. |
|
openai
Package openai adapts the official OpenAI Go SDK (github.com/openai/openai-go/v3) to the agent.Provider interface family, giving it first-class status alongside the claude and gemini adapters.
|
Package openai adapts the official OpenAI Go SDK (github.com/openai/openai-go/v3) to the agent.Provider interface family, giving it first-class status alongside the claude and gemini adapters. |
|
Package schema implements the small JSON Schema subset used to describe tool inputs to Claude, OpenAI, and Gemini.
|
Package schema implements the small JSON Schema subset used to describe tool inputs to Claude, OpenAI, and Gemini. |