Documentation
¶
Overview ¶
Package agentkit runs LLM agents on top of llmkit: typed tools generated from Go structs, a budgeted tool-calling loop with streaming events and hooks, conversation memory, and multi-agent composition.
weather := agentkit.Func("weather", "Current weather for a city",
func(ctx context.Context, in struct {
City string `json:"city" jsonschema:"city name"`
}) (string, error) {
return lookup(in.City), nil
})
agent, err := agentkit.New("claude-sonnet-4-5",
agentkit.WithInstructions("You are a concise assistant."),
agentkit.WithTools(weather))
res, err := agent.Run(ctx, "What's the weather in Cape Town?")
fmt.Println(res.Output)
Index ¶
- Constants
- Variables
- func Map[In, Out any](ctx context.Context, parallel int, items []In, ...) ([]Out, error)
- func SchemaFor[T any](opts ...FuncOption) (json.RawMessage, error)
- func WithCall(ctx context.Context, c Call) context.Context
- type Agent
- func (a *Agent) Name() string
- func (a *Agent) Run(ctx context.Context, input string, opts ...RunOption) (*Result, error)
- func (a *Agent) RunMessages(ctx context.Context, msgs []core.Message, opts ...RunOption) (*Result, error)
- func (a *Agent) Stream(ctx context.Context, input string, opts ...RunOption) iter.Seq2[Event, error]
- func (a *Agent) StreamMessages(ctx context.Context, msgs []core.Message, opts ...RunOption) iter.Seq2[Event, error]
- func (a *Agent) Tools() Toolset
- type AsToolOption
- type Budget
- type BudgetError
- type Call
- type CharEstimator
- type CompactInfo
- type Compactor
- type CompactorFunc
- type Deleter
- type Estimator
- type Event
- type EventKind
- type FileStore
- type FuncOption
- type HandoffInfo
- type Hooks
- type MemoryStore
- type Middleware
- type ModelCallInfo
- type ModelResponseInfo
- type Option
- func WithBudget(b Budget) Option
- func WithClientOptions(opts ...core.Option) Option
- func WithCompactor(c Compactor) Option
- func WithContextWindow(tokens int) Option
- func WithEstimator(e Estimator) Option
- func WithHooks(h Hooks) Option
- func WithInstructions(s string) Option
- func WithMaxTokens(n int) Option
- func WithMiddleware(mws ...Middleware) Option
- func WithName(name string) Option
- func WithParallel(n int) Option
- func WithProviderOptions(po map[string]map[string]any) Option
- func WithReasoning(r core.ReasoningConfig) Option
- func WithRegistry(r *llmkit.Registry) Option
- func WithRequestExtra(extra map[string]any) Option
- func WithRetriever(r Retriever, k int) Option
- func WithRetry(p RetryPolicy) Option
- func WithStore(s Store) Option
- func WithTemperature(t float64) Option
- func WithToolChoice(tc core.ToolChoice) Option
- func WithTools(tools ...Tool) Option
- type Output
- type OutputMode
- type Result
- type Retriever
- type RetryInfo
- type RetryPolicy
- type RunInfo
- type RunOption
- type Sequential
- type StepInfo
- type StopReason
- type Store
- type SummarizeOption
- type Tool
- func AsTool(a *Agent, name, description string, opts ...AsToolOption) Tool
- func Func[In, Out any](name, description string, fn func(context.Context, In) (Out, error), ...) Tool
- func Handoff(targets ...*Agent) Tool
- func NewFunc[In, Out any](name, description string, fn func(context.Context, In) (Out, error), ...) (Tool, error)
- func Raw(name, description string, params json.RawMessage, ...) Tool
- func Recall(r Retriever, k int) Tool
- func Rename(t Tool, name string) Tool
- func Wrap(t Tool, mws ...Middleware) Tool
- type ToolCallInfo
- type ToolResultInfo
- type Toolset
- type VectorMemory
- type VectorOption
Constants ¶
const FinalAnswerTool = "final_answer"
FinalAnswerTool is the name of the tool Run[T] injects in OutputTool mode.
const HandoffToolName = "handoff"
HandoffToolName is the name of the tool Handoff builds.
const RecallToolName = "recall"
RecallToolName is the name of the tool Recall builds.
Variables ¶
var ( ErrBudgetExceeded = errors.New("agentkit: budget exceeded") ErrNoFinalAnswer = errors.New("agentkit: model finished without final answer") ErrToolNotFound = errors.New("agentkit: tool not found") ErrApprovalDenied = errors.New("agentkit: tool call not approved") ErrHandoffLoop = errors.New("agentkit: handoff limit reached") ErrDuplicateTool = errors.New("agentkit: duplicate tool name") ErrInvalidArgs = errors.New("agentkit: tool arguments failed schema validation") )
Sentinel errors. Match them with errors.Is; a partial *Result accompanies every run error.
var DefaultRetry = RetryPolicy{MaxAttempts: 3, BaseDelay: 500 * time.Millisecond, MaxDelay: 30 * time.Second, Jitter: 0.2}
DefaultRetry is the policy agents use unless WithRetry overrides it.
Functions ¶
func Map ¶
func Map[In, Out any](ctx context.Context, parallel int, items []In, fn func(context.Context, In) (Out, error)) ([]Out, error)
Map runs fn over items with bounded concurrency and returns results in input order. Errors are joined; on cancellation unrun items stay zero and the joined error includes ctx.Err().
func SchemaFor ¶
func SchemaFor[T any](opts ...FuncOption) (json.RawMessage, error)
SchemaFor derives the JSON Schema Func would use for T.
Types ¶
type Agent ¶
type Agent struct {
// contains filtered or unexported fields
}
Agent is an immutable configuration: a model, instructions, tools and limits. One Agent serves any number of concurrent runs.
func NewFromClient ¶
NewFromClient builds an Agent around an existing client (or a fake).
func (*Agent) RunMessages ¶
func (a *Agent) RunMessages(ctx context.Context, msgs []core.Message, opts ...RunOption) (*Result, error)
RunMessages is Run with caller-built messages appended to the session.
func (*Agent) Stream ¶
func (a *Agent) Stream(ctx context.Context, input string, opts ...RunOption) iter.Seq2[Event, error]
Stream is Run yielding events as they happen. The final event is EventFinish.
type AsToolOption ¶
type AsToolOption func(*subAgentTool)
AsToolOption configures AsTool.
func WithForwardEvents ¶
func WithForwardEvents() AsToolOption
WithForwardEvents is reserved for surfacing child events in the parent stream; child runs are currently reported through Hooks only.
type Budget ¶
type Budget struct {
MaxSteps int // model calls; default 10
MaxTokens int // cumulative Usage.TotalTokens; 0 = unlimited
MaxToolCalls int // 0 = unlimited
MaxHandoffs int // default 5
Timeout time.Duration // 0 = none
}
Budget bounds a run. Zero values take the documented defaults.
type BudgetError ¶
BudgetError reports which Budget limit stopped a run.
func (*BudgetError) Unwrap ¶
func (e *BudgetError) Unwrap() error
Unwrap lets errors.Is match ErrBudgetExceeded.
type CharEstimator ¶
CharEstimator divides text length by CharsPerToken and charges flat costs per message and per binary part. Zero fields take the defaults 4, 4, 800.
type CompactInfo ¶
CompactInfo describes a history compaction; Reason is "proactive" or "context_length".
type Compactor ¶
type Compactor interface {
Compact(ctx context.Context, msgs []core.Message, budgetTokens int) ([]core.Message, error)
}
Compactor shrinks the history sent to the model to roughly budgetTokens. Implementations must keep tool calls with their results.
func Summarize ¶
func Summarize(c core.Chatter, keepTurns int, opts ...SummarizeOption) Compactor
Summarize replaces turns older than keepTurns with one model-written summary, inserted as a user message after the system prompt. Summaries are memoised per dropped prefix so repeated runs in one process do not pay twice.
type CompactorFunc ¶
type CompactorFunc func(ctx context.Context, msgs []core.Message, budgetTokens int) ([]core.Message, error)
CompactorFunc adapts a function to Compactor.
type Event ¶
type Event struct {
Kind EventKind
RunID string
Agent string
Step int
Text string
ToolCall *core.ToolCall
ToolResult *core.ToolResult
Duration time.Duration
Attempt int
Delay time.Duration
Err error
Before int
After int
Handoff *HandoffInfo
Result *Result
}
Event is one item of Agent.Stream. Exactly one EventFinish ends a stream and carries the Result; on failure it is paired with the error.
type EventKind ¶
type EventKind uint8
EventKind discriminates streamed run events.
type FileStore ¶
type FileStore struct {
// contains filtered or unexported fields
}
FileStore keeps one JSON file per session under a directory, written atomically. The directory is created on first Append.
func NewFileStore ¶
NewFileStore returns a FileStore rooted at dir; no I/O happens here.
type FuncOption ¶
type FuncOption func(*funcConfig)
FuncOption tunes schema generation for Func and NewFunc.
func WithEnum ¶
func WithEnum[T ~string](values ...T) FuncOption
WithEnum constrains every field of type T to the given values.
func WithForOptions ¶
func WithForOptions(o *jsonschema.ForOptions) FuncOption
WithForOptions passes options through to jsonschema.For.
func WithSchema ¶
func WithSchema(params json.RawMessage) FuncOption
WithSchema supplies the full parameter schema instead of deriving it.
func WithStrict ¶
func WithStrict() FuncOption
WithStrict marks the tool for OpenAI strict mode. Every property must then be required, so avoid omitempty fields.
func WithoutValidation ¶
func WithoutValidation() FuncOption
WithoutValidation skips schema validation of incoming arguments.
type HandoffInfo ¶
HandoffInfo describes control passing to another agent.
type Hooks ¶
type Hooks struct {
OnRunStart func(RunInfo)
OnStep func(StepInfo)
OnModelCall func(ModelCallInfo)
OnModelResponse func(ModelResponseInfo)
OnToolCall func(ToolCallInfo)
OnToolResult func(ToolResultInfo)
OnRetry func(RetryInfo)
OnCompact func(CompactInfo)
OnHandoff func(HandoffInfo)
OnRunEnd func(*Result, error)
}
Hooks receive callbacks during a run. Every field is optional. Tool hooks may fire from worker goroutines when WithParallel is above one.
type MemoryStore ¶
type MemoryStore struct {
// contains filtered or unexported fields
}
MemoryStore keeps sessions in process memory.
func NewMemoryStore ¶
func NewMemoryStore() *MemoryStore
NewMemoryStore returns an empty MemoryStore.
type Middleware ¶
Middleware wraps a Tool. Wrap applies the first middleware outermost.
func Approve ¶
func Approve(fn func(ctx context.Context, c Call) error) Middleware
Approve gates each call; a non-nil error from fn is fed back to the model as an error result wrapping ErrApprovalDenied. Cancel ctx inside fn to abort the run instead.
func Recover ¶
func Recover() Middleware
Recover turns a panic inside the tool into an error result.
func Serial ¶
func Serial() Middleware
Serial marks the tool as never running concurrently with other tool calls.
type ModelCallInfo ¶
ModelCallInfo describes a request about to be sent; treat Request as read-only.
type ModelResponseInfo ¶
type ModelResponseInfo struct {
RunID string
Agent string
Step int
Response *core.Response
Duration time.Duration
Err error
}
ModelResponseInfo describes a completed model call.
type Option ¶
Option configures an Agent.
func WithClientOptions ¶
WithClientOptions passes options to the llmkit client (New only).
func WithCompactor ¶
WithCompactor shapes the history sent to the model when it grows too large.
func WithContextWindow ¶
WithContextWindow enables proactive compaction when the estimated prompt exceeds 90% of tokens.
func WithEstimator ¶
WithEstimator replaces the token estimator used for proactive compaction.
func WithInstructions ¶
WithInstructions sets the system prompt.
func WithMaxTokens ¶
WithMaxTokens caps output tokens per model call.
func WithMiddleware ¶
func WithMiddleware(mws ...Middleware) Option
WithMiddleware wraps every tool of the agent, including Recall and sub-agents.
func WithParallel ¶
WithParallel allows up to n tool calls of one step to run concurrently.
func WithProviderOptions ¶
WithProviderOptions merges provider-keyed raw fields into every request.
func WithReasoning ¶
func WithReasoning(r core.ReasoningConfig) Option
WithReasoning enables extended thinking where supported.
func WithRegistry ¶
WithRegistry resolves the model against a specific llmkit registry (New only).
func WithRequestExtra ¶
WithRequestExtra merges raw fields into every request body.
func WithRetriever ¶
WithRetriever adds a "recall" tool returning the k best matches.
func WithTemperature ¶
WithTemperature sets the sampling temperature.
func WithToolChoice ¶
func WithToolChoice(tc core.ToolChoice) Option
WithToolChoice sets the request tool choice.
type Output ¶
Output is what a tool hands back to the model. IsError marks a tool-level failure the model should see; a Go error from Call means the same and is also reported to Hooks.
type OutputMode ¶
type OutputMode uint8
OutputMode selects how Run[T] obtains structured output.
const ( OutputAuto OutputMode = iota // schema for tool-less agents, tool otherwise OutputSchema // provider json_schema response format OutputTool // synthetic final_answer tool )
Output modes.
type Result ¶
type Result struct {
RunID string
Agent string
Output string
Message core.Message
Messages []core.Message
New []core.Message
Usage core.Usage
Steps int
ToolCalls int
Handoffs int
FinishReason core.FinishReason
StopReason StopReason
Duration time.Duration
}
Result is the outcome of a run. On error it is partial but always present.
type RetryInfo ¶
type RetryInfo struct {
RunID string
Agent string
Step int
Attempt int
Delay time.Duration
Err error
}
RetryInfo describes a model-call retry.
type RetryPolicy ¶
type RetryPolicy struct {
MaxAttempts int // total attempts; default 3, 1 disables retries
BaseDelay time.Duration // default 500ms
MaxDelay time.Duration // default 30s
Jitter float64 // fraction of the delay, default 0.2
}
RetryPolicy governs retries of model calls. Tools are never retried.
func (RetryPolicy) Delay ¶
func (p RetryPolicy) Delay(attempt int, err error) time.Duration
Delay returns how long to wait before the given 1-based retry attempt, honoring a Retry-After hint when the error carries one.
func (RetryPolicy) Retryable ¶
func (p RetryPolicy) Retryable(err error) bool
Retryable reports whether err is transient: rate limits, 5xx and transport failures. Context errors, other 4xx and ErrContextLength are not.
type RunOption ¶
type RunOption func(*runConfig)
RunOption configures one run.
func WithOutputMode ¶
func WithOutputMode(m OutputMode) RunOption
WithOutputMode overrides how Run[T] obtains structured output.
func WithRunHooks ¶
WithRunHooks adds observers for this run only.
func WithSession ¶
WithSession loads history from the agent's Store before the run and appends the new messages afterwards.
type Sequential ¶
type Sequential interface {
Sequential() bool
}
Sequential is implemented by tools that must not run concurrently with other tool calls from the same step. Serial adds it; MCP tools may declare it.
type StopReason ¶
type StopReason string
StopReason says why a run ended.
const ( StopCompleted StopReason = "completed" StopFinalAnswer StopReason = "final_answer" StopMaxSteps StopReason = "max_steps" StopMaxTokens StopReason = "max_tokens" StopMaxToolCalls StopReason = "max_tool_calls" StopDeadline StopReason = "deadline" StopCancelled StopReason = "canceled" StopError StopReason = "error" )
Stop reasons.
type Store ¶
type Store interface {
Load(ctx context.Context, sessionID string) ([]core.Message, error)
Append(ctx context.Context, sessionID string, msgs ...core.Message) error
}
Store persists conversations by session ID. Load returns nil for an unknown session. Stores hold the lossless transcript; compaction never touches them.
type SummarizeOption ¶
type SummarizeOption func(*summarizer)
SummarizeOption configures Summarize.
func WithSummaryEstimator ¶
func WithSummaryEstimator(e Estimator) SummarizeOption
WithSummaryEstimator replaces the estimator used to decide how much to drop.
func WithSummaryPrompt ¶
func WithSummaryPrompt(prompt string) SummarizeOption
WithSummaryPrompt replaces the instruction sent to the summarizing model.
type Tool ¶
type Tool interface {
Definition() core.Tool
Call(ctx context.Context, args json.RawMessage) (Output, error)
}
Tool is anything the model may call.
func AsTool ¶
func AsTool(a *Agent, name, description string, opts ...AsToolOption) Tool
AsTool exposes an agent as a tool taking {"input": string}. The child runs with its own budget, one level deeper, and only its final text comes back; its token usage is added to the parent's.
func Func ¶
func Func[In, Out any](name, description string, fn func(context.Context, In) (Out, error), opts ...FuncOption) Tool
Func builds a Tool whose arguments are decoded into In and whose result is Out. The schema is derived from In; arguments are validated against it before decoding. Func panics when In cannot be represented; use NewFunc for a recoverable error.
func Handoff ¶
Handoff builds a tool that transfers the conversation to one of the target agents: the system prompt, tools and model switch while the transcript is carried over. Give targets their own Handoff tool to allow handing back.
func NewFunc ¶
func NewFunc[In, Out any](name, description string, fn func(context.Context, In) (Out, error), opts ...FuncOption) (Tool, error)
NewFunc is Func returning an error instead of panicking.
func Raw ¶
func Raw(name, description string, params json.RawMessage, fn func(context.Context, json.RawMessage) (Output, error)) Tool
Raw builds a Tool from a hand-written JSON Schema.
func Wrap ¶
func Wrap(t Tool, mws ...Middleware) Tool
Wrap applies mws to t; nil middleware is skipped.
type ToolCallInfo ¶
type ToolCallInfo struct {
Call Call
}
ToolCallInfo describes a tool about to run.
type ToolResultInfo ¶
ToolResultInfo describes a finished tool call.
type Toolset ¶
type Toolset []Tool
Toolset is an ordered list of tools.
func (Toolset) Definitions ¶
Definitions returns the core.Tool definitions in order.
type VectorMemory ¶
type VectorMemory struct {
// contains filtered or unexported fields
}
VectorMemory is an in-memory store of texts searched by cosine similarity.
func NewVectorMemory ¶
func NewVectorMemory(e core.Embedder, opts ...VectorOption) *VectorMemory
NewVectorMemory builds an empty memory over the embedder.
type VectorOption ¶
type VectorOption func(*VectorMemory)
VectorOption configures VectorMemory.
func WithDimensions ¶
func WithDimensions(n int) VectorOption
WithDimensions requests a fixed embedding size from the embedder.
func WithReranker ¶
func WithReranker(r core.Reranker) VectorOption
WithReranker reorders the top candidates with a cross-encoder.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
internal/fake
Package fake registers an in-process OpenAI-compatible provider so the examples run offline.
|
Package fake registers an in-process OpenAI-compatible provider so the examples run offline. |
|
multiagent
command
Command multiagent shows a sub-agent exposed as a tool and a parallel Map.
|
Command multiagent shows a sub-agent exposed as a tool and a parallel Map. |
|
tools
command
Command tools shows a typed tool driven by the agent loop.
|
Command tools shows a typed tool driven by the agent loop. |
|
typed
command
Command typed shows Run[T]: the agent must return a value matching a Go struct.
|
Command typed shows Run[T]: the agent must return a value matching a Go struct. |
|
internal
|
|
|
atomicfile
Package atomicfile writes files via a temporary sibling and rename.
|
Package atomicfile writes files via a temporary sibling and rename. |
|
pool
Package pool runs functions over a slice with bounded concurrency, preserving input order in the results.
|
Package pool runs functions over a slice with bounded concurrency, preserving input order in the results. |
|
Package slogx adapts agentkit.Hooks to log/slog.
|
Package slogx adapts agentkit.Hooks to log/slog. |