Documentation
¶
Overview ¶
Package agent provides the Agent abstraction for Go Micro.
An Agent is a service with an LLM inside it. It registers a Chat RPC endpoint, discovers its assigned services' tools, and orchestrates them intelligently.
agent := micro.NewAgent("task-mgr",
micro.AgentServices("task"),
micro.AgentPrompt("You manage tasks."),
micro.AgentProvider("anthropic"),
)
agent.Run()
Index ¶
- Constants
- func Builtins(opts ...Option) (tools []ai.Tool, ...)
- func Pending(ctx context.Context, ag Agent) ([]flow.Run, error)
- func ResumePending(ctx context.Context, ag Agent) (string, error)
- func Summary(m Memory) string
- type Agent
- type AgentStream
- type ApproveFunc
- type Memory
- func NewCompactingMemory(s store.Store, key string, maxMessages, keepRecent int) Memory
- func NewCompactingMemoryWithOptions(s store.Store, key string, compaction MemoryCompaction) Memory
- func NewInMemory(limit int) Memory
- func NewMemory(s store.Store, key string, limit int) Memory
- func NewRetrievalMemory(s store.Store, key string, activeLimit int) Memory
- type MemoryCompaction
- type MemoryRecall
- type MemorySummary
- type MemorySummaryFunc
- type Option
- func APIKey(k string) Option
- func Address(addr string) Option
- func ApproveTool(fn ApproveFunc) Option
- func BaseURL(url string) Option
- func Budget(amount int64) Option
- func CompactMemory(maxMessages, keepRecent int) Option
- func HistoryLimit(n int) Option
- func LoopLimit(n int) Option
- func MaxSpend(amount int64) Option
- func MaxSteps(n int) Option
- func MemoryRecallLimit(n int) Option
- func MemorySummarizer(fn MemorySummaryFunc) Option
- func Model(m string) Option
- func ModelCallTimeout(d time.Duration) Option
- func ModelRetry(maxAttempts int, backoff time.Duration) Option
- func ModelRetryJitter(d time.Duration) Option
- func Name(n string) Option
- func Payer(p x402.Payer) Option
- func Prompt(p string) Option
- func Provider(p string) Option
- func RetrievalMemory(activeLimit int) Option
- func Services(names ...string) Option
- func ToolCallTimeout(d time.Duration) Option
- func ToolRetry(maxAttempts int, backoff time.Duration) Option
- func ToolSpend(tool string, amount int64) Option
- func TraceInputs(enabled bool) Option
- func TraceProvider(tp trace.TracerProvider) Option
- func WithA2A(addr string) Option
- func WithBroker(b broker.Broker) Option
- func WithCheckpoint(c flow.Checkpoint) Option
- func WithClient(c client.Client) Option
- func WithMemory(m Memory) Option
- func WithRegistry(r registry.Registry) Option
- func WithStore(s store.Store) Option
- func WithTool(name, description string, properties map[string]any, handler ToolFunc) Option
- func WrapTool(w ...ai.ToolWrapper) Option
- type Options
- type Response
- type RunEvent
- type RunListOptions
- type RunSummary
- type StreamEvent
- type StreamEventType
- type ToolFunc
- type Usage
Constants ¶
const ( AttrRunID = "agent.run.id" AttrParentRunID = "agent.run.parent_id" AttrAgentName = "agent.name" AttrProvider = "agent.model.provider" AttrModel = "agent.model.name" AttrLatencyMS = "agent.latency_ms" AttrInputTokens = "agent.tokens.input" AttrOutputTokens = "agent.tokens.output" AttrTotalTokens = "agent.tokens.total" AttrAttempt = "agent.model.attempt" AttrMaxAttempts = "agent.model.max_attempts" AttrToolAttempt = "agent.tool.attempt" AttrToolMaxAttempts = "agent.tool.max_attempts" AttrToolName = "agent.tool.name" AttrDelegate = "agent.delegate" AttrGuardrailBlock = "agent.guardrail.block" AttrRefusal = "agent.refusal" AttrInputChars = "agent.input.chars" AttrErrorKind = "agent.error.kind" AttrCheckpointStatus = "agent.checkpoint.status" AttrCheckpointStage = "agent.checkpoint.stage" AttrFlowName = "agent.flow.name" AttrFlowStep = "agent.flow.step" AttrDispatch = "agent.dispatch" AttrTrigger = "agent.trigger" AttrRunEventKind = "agent.event.kind" AttrSpend = "agent.spend" AttrToolSpend = "agent.tool.spend" )
Variables ¶
This section is empty.
Functions ¶
func Builtins ¶
func Builtins(opts ...Option) (tools []ai.Tool, handle func(name string, input map[string]any) (result any, content string, ok bool))
Builtins returns the built-in agent tools (plan, delegate) together with a handler for them, so the same capabilities can be wired into a tool loop that isn't a running Agent — for example the `micro chat` fallback. The handler's third return value is false when the name is not a built-in, so callers can fall through to their own tools.
Configure it with the same options as an Agent (Name, Provider, WithStore, WithRegistry, WithClient, ...); these back plan's memory and delegate's RPC/sub-agent behavior.
func Pending ¶ added in v6.3.1
Pending returns checkpointed agent runs that have not completed. It mirrors flow.Pending for startup recovery loops that drain durable agent work.
func ResumePending ¶ added in v6.3.18
ResumePending resumes every checkpointed agent run that has not completed yet, in the same oldest-first order returned by Pending.
It is a convenience for service startup and recovery loops: after recreating an agent with the same checkpoint store, call ResumePending to drain the durable backlog without listing and resuming each run manually. If any run fails again, ResumePending stops and returns that run id with the error so callers can log, alert, or retry later without hiding the failing run.
Types ¶
type Agent ¶
type Agent interface {
Name() string
Init(...Option)
Options() Options
Ask(ctx context.Context, message string) (*Response, error)
Stream(ctx context.Context, message string) (ai.Stream, error)
Run() error
Stop() error
String() string
}
Agent is the interface for an AI agent that manages services.
type AgentStream ¶ added in v6.3.9
type AgentStream interface {
Recv() (*StreamEvent, error)
Close() error
}
AgentStream is a stream of tool execution events followed by final-answer chunks.
func ResumeStreamAsk ¶ added in v6.3.9
ResumeStreamAsk resumes a checkpointed agent run and emits the same event shape as StreamAsk. Completed runs are streamed from the persisted response; unfinished runs continue from their checkpoint and emit tool events for any work that still needs to run. Tool calls already recorded as done in the checkpoint are reused by the agent checkpoint wrapper and are not re-executed.
type ApproveFunc ¶
ApproveFunc decides whether an agent may execute a tool call before it runs. Returning false blocks the call; the reason is shown to the model so it can adapt. Use it for human-in-the-loop approval or policy checks. It is called for actions (service tools and delegate), not for the internal plan tool.
type Memory ¶
type Memory interface {
// Add appends a message to the conversation.
Add(role, content string)
// Messages returns the retained conversation, oldest first.
Messages() []ai.Message
// Clear resets the conversation.
Clear()
}
Memory is an agent's conversation memory. Like the rest of the framework it is pluggable: the default is store-backed and durable across restarts, but any implementation can be supplied with WithMemory — in-process, a database, or a semantic/vector store.
func NewCompactingMemory ¶ added in v6.3.1
NewCompactingMemory returns store-backed memory with explicit compaction and retrieval controls. It keeps all messages in the backing store, compacts older turns into a deterministic summary when the conversation exceeds maxMessages, and lets callers recall relevant prior turns with Recall.
func NewCompactingMemoryWithOptions ¶ added in v6.3.11
func NewCompactingMemoryWithOptions(s store.Store, key string, compaction MemoryCompaction) Memory
NewCompactingMemoryWithOptions returns store-backed memory configured with explicit compaction options, including an optional summarization hook.
func NewInMemory ¶
NewInMemory returns conversation memory that is not persisted.
func NewMemory ¶
NewMemory returns the default store-backed memory: an in-process conversation buffer (truncated to limit) that persists to the store under key, so an agent picks up where it left off after a restart. A nil store or empty key yields non-persistent memory.
func NewRetrievalMemory ¶ added in v6.3.11
NewRetrievalMemory returns store-backed memory that keeps a bounded active conversation and archives every turn for retrieval. It is useful when callers want relevant durable recall without summary compaction in the active context. A nil store or empty key keeps only the active in-process buffer.
type MemoryCompaction ¶ added in v6.3.1
type MemoryCompaction struct {
MaxMessages int
KeepRecent int
Summarize MemorySummaryFunc
}
MemoryCompaction configures deterministic, store-backed context compaction for the default memory implementation. When the retained conversation grows past MaxMessages, older turns are collapsed into a summary message while the newest KeepRecent turns stay verbatim for provider-neutral continuity.
type MemoryRecall ¶ added in v6.3.1
MemoryRecall is implemented by memory backends that can retrieve durable prior context relevant to a new turn without replaying every stored message.
type MemorySummary ¶ added in v6.7.1
type MemorySummary interface {
Summary() string
}
MemorySummary is implemented by memory backends that expose their current compacted summary for inspection. It lets long-running agents make memory compaction observable without coupling callers to a concrete store.
type MemorySummaryFunc ¶ added in v6.3.11
MemorySummaryFunc turns older conversation messages into a compact replacement message for active context. It is called while the default memory is locked, so implementations should be deterministic and avoid calling back into the same memory instance.
type Option ¶
type Option func(*Options)
Option configures an Agent.
func Address ¶ added in v6.3.1
Address sets the network address for the agent's service endpoint. Use "127.0.0.1:0" in local harnesses/tests to bind an ephemeral loopback port and avoid advertising the default service address.
func ApproveTool ¶
func ApproveTool(fn ApproveFunc) Option
ApproveTool sets a human-in-the-loop / policy hook called before each action (service tools and delegate). Returning false blocks the call.
func BaseURL ¶ added in v6.3.12
BaseURL sets the base URL for the LLM provider. Use this to point the provider at a non-default endpoint (e.g., local Ollama, a proxy).
func Budget ¶ added in v6.7.1
Budget bounds autonomous x402 payments per Ask, in the asset's smallest unit (0 = unlimited). The budget is enforced by wrapper/x402.Client.
func CompactMemory ¶ added in v6.3.1
CompactMemory enables deterministic, store-backed memory compaction for the default agent memory. Older turns are summarized once active context exceeds maxMessages, keepRecent newest turns remain verbatim, and recalled archived turns are injected into matching future asks.
func HistoryLimit ¶
HistoryLimit sets the max conversation messages to retain.
func LoopLimit ¶
LoopLimit sets how many times the agent may repeat the same tool call (same name and arguments) in one Ask before it is refused as a no-progress loop. 0 disables loop detection.
func MaxSpend ¶ added in v6.7.0
MaxSpend bounds paid x402 tool spend per Ask, in the asset's smallest unit (0 = disabled). A paid tool that would exceed the cap is refused before the tool handler runs or any payment can be made.
func MaxSteps ¶
MaxSteps bounds tool executions per Ask (0 = unbounded). A stopping condition: beyond the limit, tool calls are refused and the model is told to stop and summarize.
func MemoryRecallLimit ¶ added in v6.3.1
MemoryRecallLimit sets how many archived turns a memory backend may inject into a model request for the current Ask. Use 0 to disable retrieval.
func MemorySummarizer ¶ added in v6.3.11
func MemorySummarizer(fn MemorySummaryFunc) Option
MemorySummarizer sets the deterministic summarization hook used by the default compacting memory. It is optional; without it, compacted memory uses a provider-neutral text summary. The hook receives the older messages being removed from active context and returns the replacement summary message.
func ModelCallTimeout ¶ added in v6.3.0
ModelCallTimeout sets the timeout for each provider Generate call.
func ModelRetry ¶ added in v6.3.0
ModelRetry sets the provider retry budget and backoff for transient failures.
func ModelRetryJitter ¶ added in v6.7.1
ModelRetryJitter adds bounded random jitter to provider retry backoff. Set 0 to disable.
func Payer ¶ added in v6.7.1
Payer configures the wallet/signing hook used to settle x402-paid tools. Without a payer, payment-required tool results are returned as clear errors.
func RetrievalMemory ¶ added in v6.3.11
RetrievalMemory enables deterministic, store-backed retrieval memory for the default agent memory without compaction. Active context is capped at activeLimit messages while every turn is archived in the store for Recall.
func ToolCallTimeout ¶ added in v6.3.11
ToolCallTimeout sets the timeout for each tool execution. It bounds custom tools, built-in delegate calls, and service RPC tools with the same context deadline so mid-run cancellation and slow tools produce safe error results instead of unbounded agent runs. Set 0 to disable.
func ToolRetry ¶ added in v6.3.11
ToolRetry sets the tool retry budget and backoff for transient failures. Attempts include the first call. Retries are opt-in because tools may have side effects; keep handlers idempotent before enabling this.
func ToolSpend ¶ added in v6.7.0
ToolSpend records the x402 price for a tool, in the asset's smallest unit, so MaxSpend can reserve budget before execution. Non-positive amounts are treated as free.
func TraceInputs ¶ added in v6.3.11
TraceInputs opts in to recording raw user messages on agent run events. By default inputs are redacted from OpenTelemetry spans and persisted run timelines; use this only when the observability backend is approved to store prompt content.
func TraceProvider ¶ added in v6.3.0
func TraceProvider(tp trace.TracerProvider) Option
TraceProvider enables OpenTelemetry tracing for agent runs. The persisted run timeline is recorded even when TraceProvider is nil; trace/span IDs are added only when a provider is configured.
func WithA2A ¶
WithA2A makes Run serve the agent over the A2A protocol on addr (e.g. ":4000"), so other agents can reach it directly by URL without a separate gateway. The agent stays a normal go-micro service as well; this adds a second, A2A-native HTTP endpoint that calls it in-process.
func WithBroker ¶ added in v6.3.15
WithBroker sets the broker used by the agent service endpoint. Use an in-memory broker in local harnesses/tests to avoid sharing the package-wide default broker listener across concurrently running examples.
func WithCheckpoint ¶ added in v6.3.1
func WithCheckpoint(c flow.Checkpoint) Option
WithCheckpoint sets the durability backend for agent Ask runs. The Checkpoint interface is shared with flow so services, agents, and workflows can use one execution history backend. When set, each Ask is saved as a single-step run keyed by run id; Resume returns a completed run's persisted response instead of calling the model again.
func WithMemory ¶
WithMemory sets the agent's conversation memory. The default is store-backed memory keyed by agent name; supply your own to use an in-process, database, or semantic store.
func WithRegistry ¶
WithRegistry sets the service registry.
func WithTool ¶
WithTool registers a custom tool the agent can call, beyond the services it discovers — a local function, an external API, anything. properties is the JSON-schema map for the tool's parameters.
func WrapTool ¶
func WrapTool(w ...ai.ToolWrapper) Option
WrapTool registers a tool-execution wrapper, the tool-side analog of a client/server middleware wrapper. Each wrapper takes the next handler and returns a new one; code before the next(...) call runs before the tool executes, code after runs after. Use it for logging, metrics, retries, or custom policy. Wrappers run outside the built-in guardrails (MaxSteps, LoopLimit, ApproveTool), so they observe every call and its result, including refusals. Multiple wrappers compose outermost-first.
micro.NewAgent("worker", micro.AgentWrapTool(
func(next ai.ToolHandler) ai.ToolHandler {
return func(ctx context.Context, call ai.ToolCall) ai.ToolResult {
res := next(ctx, call)
log.Printf("id=%s tool=%s", call.ID, call.Name)
return res
}
}))
type Options ¶
type Options struct {
Name string
Services []string
Prompt string
Provider string
Model string
APIKey string
BaseURL string
Address string
Registry registry.Registry
Client client.Client
Broker broker.Broker
Store store.Store
HistoryLimit int
// ModelTimeout bounds each provider Generate call (0 disables).
ModelTimeout time.Duration
// ModelMaxAttempts bounds provider Generate attempts including the first
// call. Default 1 — retries are opt-in (enable with ModelRetry). A Generate
// runs the whole tool-execution turn, so auto-retrying it would re-run
// already-executed, possibly side-effecting tool calls; keep it explicit.
ModelMaxAttempts int
// ModelRetryBackoff is the base delay between transient provider failures
// (grows exponentially per attempt when retries are enabled).
ModelRetryBackoff time.Duration
// ModelRetryJitter adds up to this random delay to each provider retry
// backoff. Default 0 preserves deterministic timing unless explicitly set.
ModelRetryJitter time.Duration
// ToolTimeout bounds each tool execution (0 disables). The timeout is
// applied before custom tools, delegate, and service RPC calls so context
// deadlines propagate consistently through the agent loop.
ToolTimeout time.Duration
// ToolMaxAttempts bounds tool execution attempts including the first call.
// Default 1; retries are opt-in because tools can have side effects.
ToolMaxAttempts int
// ToolRetryBackoff is the base delay between transient tool failures.
ToolRetryBackoff time.Duration
// Memory is the agent's conversation memory. Nil = the default
// store-backed memory (durable across restarts).
Memory Memory
// MemoryRetrievalLimit enables retrieval-backed default memory without
// compaction. The active conversation stays bounded to this many messages
// while every turn is archived for deterministic recall.
MemoryRetrievalLimit int
// MemoryCompaction enables deterministic compaction/retrieval on the
// default store-backed memory. Custom Memory implementations can expose
// retrieval by implementing MemoryRecall.
MemoryCompaction MemoryCompaction
// MemoryRecallLimit bounds recalled archived turns injected into a model
// request (0 disables recall injection).
MemoryRecallLimit int
// Checkpoint persists agent Ask runs so callers can resume by run id
// after a restart without replaying a run that already completed.
Checkpoint flow.Checkpoint
// MaxSteps bounds the number of tool executions per Ask (0 =
// unbounded). Once exceeded, further tool calls are refused and the
// model is told to stop and summarize. A stopping condition.
MaxSteps int
// LoopLimit bounds how many times the agent may call the same tool
// with the same arguments in one Ask before the call is refused as a
// no-progress loop (0 = disabled). Catches the agent repeating an
// identical action — which MaxSteps only bounds by total count.
LoopLimit int
// Approve gates each action before it runs. Nil = allow all.
Approve ApproveFunc
// MaxSpend bounds paid x402 tool spend per Ask in the asset's smallest
// unit (0 = disabled). ToolSpend lists known paid tools and their prices.
MaxSpend int64
ToolSpend map[string]int64
// Payer lets the agent settle x402 Payment Required challenges from tools.
// Budget bounds autonomous x402 payments per Ask (0 = unlimited).
Payer x402.Payer
Budget int64
// A2AAddress, if set, makes Run serve this agent over the A2A protocol
// on that address directly (no separate gateway), e.g. ":4000".
A2AAddress string
// TraceProvider enables OpenTelemetry spans for agent runs, model calls,
// and tool calls. Nil disables instrumentation.
TraceProvider trace.TracerProvider
// TraceInputs controls whether agent observability records include raw
// user messages. It is false by default so spans and persisted run
// timelines carry correlation and shape without leaking prompts.
TraceInputs bool
// contains filtered or unexported fields
}
Options holds agent configuration.
type Response ¶
type Response struct {
Reply string
ToolCalls []ai.ToolCall
Agent string
// RunID correlates this Ask with tool calls, trace spans, and the
// persisted run timeline. ParentID is set when this response belongs
// to a delegated sub-agent run.
RunID string
ParentID string
}
Response is what an agent returns from Chat.
func Resume ¶ added in v6.3.1
Resume returns the response for a checkpointed agent run. Completed runs are returned from the checkpoint without calling the model or replaying tool calls; failed or in-progress runs continue from the saved input message.
func ResumeInput ¶ added in v6.3.1
ResumeInput resumes a checkpointed agent run that paused via the built-in request_input tool. The supplied input is appended to the original request so the same run can continue with durable checkpoint and completed tool history.
type RunEvent ¶ added in v6.3.0
type RunEvent struct {
Time time.Time `json:"time"`
RunID string `json:"run_id"`
ParentID string `json:"parent_id,omitempty"`
TraceID string `json:"trace_id,omitempty"`
SpanID string `json:"span_id,omitempty"`
Agent string `json:"agent"`
Kind string `json:"kind"`
Name string `json:"name,omitempty"`
Provider string `json:"provider,omitempty"`
Model string `json:"model,omitempty"`
Attempt int `json:"attempt,omitempty"`
MaxAttempts int `json:"max_attempts,omitempty"`
LatencyMS int64 `json:"latency_ms,omitempty"`
Tokens Usage `json:"tokens,omitempty"`
Refused string `json:"refused,omitempty"`
Status string `json:"status,omitempty"`
Error string `json:"error,omitempty"`
ErrorKind string `json:"error_kind,omitempty"`
InputChars int `json:"input_chars,omitempty"`
Spent int64 `json:"spent,omitempty"`
ToolSpend int64 `json:"tool_spend,omitempty"`
}
type RunListOptions ¶ added in v6.3.1
type RunListOptions struct {
// Status, when set, keeps only runs with the matching status
// (for example "running", "done", "canceled", "timeout",
// "rate_limited", "auth", "configuration", "unavailable",
// "provider_error", "error", or "refused").
Status string
// TraceID, when set, keeps only runs correlated with this trace id.
// A prefix is accepted so operators can paste the shortened trace id
// printed by `micro runs`.
TraceID string
// Limit, when positive, returns the most recently updated runs up to
// the limit. Limited results are ordered newest first.
Limit int
}
RunListOptions controls how recorded agent run summaries are returned. Zero values preserve the full deterministic run list.
type RunSummary ¶ added in v6.3.1
type RunSummary struct {
RunID string `json:"run_id"`
Agent string `json:"agent"`
ParentID string `json:"parent_id,omitempty"`
TraceID string `json:"trace_id,omitempty"`
SpanID string `json:"span_id,omitempty"`
StartedAt time.Time `json:"started_at"`
UpdatedAt time.Time `json:"updated_at"`
DurationMS int64 `json:"duration_ms,omitempty"`
Events int `json:"events"`
Status string `json:"status,omitempty"`
Checkpoint string `json:"checkpoint,omitempty"`
Stage string `json:"stage,omitempty"`
LastKind string `json:"last_kind,omitempty"`
LastError string `json:"last_error,omitempty"`
LastErrorKind string `json:"last_error_kind,omitempty"`
Spent int64 `json:"spent,omitempty"`
}
RunSummary is a compact index entry for a recorded agent run.
func ListRunSummaries ¶ added in v6.3.1
func ListRunSummaries(s store.Store, agentName string) ([]RunSummary, error)
ListRunSummaries returns a deterministic summary of recorded runs for agentName.
func ListRunSummariesWithOptions ¶ added in v6.3.1
func ListRunSummariesWithOptions(s store.Store, agentName string, opts RunListOptions) ([]RunSummary, error)
ListRunSummariesWithOptions returns summaries of recorded runs for agentName, optionally filtered by status and limited to the most recently updated runs.
type StreamEvent ¶ added in v6.3.9
type StreamEvent struct {
Type StreamEventType
Token string
ToolCall ai.ToolCall
Result ai.ToolResult
Response *Response
}
StreamEvent is one event from StreamAsk.
type StreamEventType ¶ added in v6.3.9
type StreamEventType string
StreamEventType identifies an event emitted by a tool-aware agent stream.
const ( // StreamEventToolStart is emitted immediately before a tool call runs. StreamEventToolStart StreamEventType = "tool_start" // StreamEventToolEnd is emitted after a tool call returns or is refused. StreamEventToolEnd StreamEventType = "tool_end" // StreamEventToken carries a chunk of the final answer. StreamEventToken StreamEventType = "token" // StreamEventDone carries the completed agent response. StreamEventDone StreamEventType = "done" )