What is an agent in weft?
An agent in weft is a weft.Agent: an immutable value that holds a model, a system instruction, a tool set and an execution policy. You build it once with weft.New and run it as many times as you need, from as many goroutines as you need; the runs share no state.
The support agent in concepts/agents.go is a name, an instruction and a plugin that brings its tools:
// Orders is a plugin: the order tools and their policy, closed over the// store they need, returned as one weft.Option.func Orders(store *OrderStore) weft.Option { return weft.Options( weft.Tool("lookup_order", "Look up an order by ID.", func(ctx context.Context, in OrderLookup) (string, error) { return "order " + in.OrderID + ": " + store.Status[in.OrderID], nil }), weft.MaxResultBytes(4096), )}
func newSupportAgent(model weft.Model, store *OrderStore) *weft.Agent { return weft.New(model, weft.Name("support-bot"), weft.Instructions("You are a support agent."), Orders(store), )}Every argument after the model is a weft.Option. Orders(store) is one option that carries a tool and a policy. The weft.MaxResultBytes(4096) inside it is an agent option, so it caps every tool on the agent, not only lookup_order; passed to weft.Tool instead, it would cap that tool alone. The next sections explain each part.
How it works
Section titled “How it works”weft.New applies its options in order to a fresh Agent, then freezes it: the tool set is deep-copied, the WrapModel chain is built, and nothing about the value changes again. Running it is a separate call, agt.Generate or agt.Stream, covered in What does a run return?.
An agent is a value
Section titled “An agent is a value”weft.New(m, opts...) takes any weft.Model: a provider adapter (weft/openai, weft/anthropic, weft/google) or a test double such as wefttest.Script. A nil model panics, including a typed nil such as var m *myModel. Every other setting has a working default:
| Setting | Default | Option |
|---|---|---|
| Steps per run | 10 | weft.MaxSteps |
| Tool calls executing at once | 4 | weft.Parallelism |
RETRY results per tool since its last success | 3 | weft.MaxModelRetries |
| One tool result’s size | 64 KiB | weft.MaxResultBytes |
Option is a sealed interface: you get options from weft’s constructors, never by implementing one. Tools are options too. weft.Tool returns a *weft.ToolDef, and passing it to New registers a deep copy of it. A second tool with the same name, or a nil *ToolDef, panics at New.
Because the value never changes, one agent serves concurrent runs. This loop runs three questions at once on the same agt:
answers := make([]string, len(questions))errs := make([]error, len(questions))var wg sync.WaitGroupfor i, q := range questions { wg.Go(func() { // one agent value, many runs at once: runs share no state res, err := agt.Generate(ctx, weft.Prompt(q)) if err != nil { errs[i] = err return } answers[i] = res.Text() })}wg.Wait()Each run gets its own transcript, step count and usage. A conversation’s history lives in the messages you pass to the next run, never in the agent: see Continue a conversation.
Name and instructions
Section titled “Name and instructions”weft.Name names the agent. The name appears on every run’s RunStart.Agent event and in the manifest; weft.Manifest returns an error for an unnamed agent. An empty name is ignored. A named agent’s runs also carry a hash of its manifest, so a recorded run says which version of the agent ran.
weft.Instructions sets the system prompt. There is no system role in a weft transcript: the instruction is agent configuration and reaches the model on each request’s ModelRequest.System, followed by the PromptSnippet text of the tools that step advertises. Passed to Generate instead of New, weft.Instructions replaces the prompt for that one run; the manifest keeps reporting the agent’s own. Change an agent’s configuration for one run covers every option that works at both levels.
Compose options
Section titled “Compose options”weft.Options composes several options into one, applied in order. That is how you package a family of tools with its policy: a plugin is a function func(deps) weft.Option, like Orders(store) above. Its dependencies are parameters, never globals.
Nothing registers itself, so there is no registry, no scope and no deduplication:
- Nil entries are ignored.
- A tool name that appears twice still panics at
New, wherever the two tools came from. - An
Optionsvalue may hold otherOptionsvalues; nesting composes.
weft.ToolOptions is the per-tool counterpart: a named bundle of Timeout, MaxResultBytes and similar options for one tool. Set per-tool policy shows it. The package example ExampleOptions is the same plugin shape.
Read an agent’s configuration
Section titled “Read an agent’s configuration”The agent reports its configuration through accessors, so code that receives an *Agent can inspect it without the options it was built from:
temp := 0.2agt := weft.New(model, weft.Name("support-bot"), weft.Thinking(weft.ThinkingConfig{Level: weft.ThinkLow}), weft.Params(weft.RequestParams{Temperature: &temp}), Orders(store),)fmt.Fprintln(w, "name:", agt.Name())fmt.Fprintln(w, "model:", weft.InfoOf(agt.Model()))for _, t := range agt.Tools() { fmt.Fprintln(w, "tool:", t.Name)}fmt.Fprintln(w, "thinking low:", agt.Thinking().Level == weft.ThinkLow)fmt.Fprintln(w, "tool choice auto:", agt.ToolChoice().Mode == weft.ToolChoiceAuto)
p := agt.Params() // a copy: changing it does not reach the agent*p.Temperature = 0.9fmt.Fprintln(w, "temperature:", *agt.Params().Temperature)| Accessor | Returns |
|---|---|
agt.Name() | The Name option’s value; empty when unnamed. |
agt.Model() | The model as the loop calls it: the model New was given, wrapped by the WrapModel middleware, outermost first. weft/thread summarizes with it. |
agt.Tools() | Copies of the registered tools in registration order. Changing a copy does not reach the agent. |
agt.Params() | The default sampling knobs, as a copy with fresh pointers; the zero value leaves every knob to the adapter. |
agt.Thinking() | The default reasoning level; the zero value, ThinkUnset, keeps the provider default. |
agt.ToolChoice() | The default tool choice; the zero value is ToolChoiceAuto. |
The core package’s examples ExampleAgent_Model, ExampleAgent_Params, ExampleAgent_Thinking and ExampleAgent_ToolChoice pin each accessor. Walking agt.Model() through its middleware with weft.Unwrap is shown on What is a Model?.
What it does not do
Section titled “What it does not do”- An agent holds no conversation. Every run starts from the messages you give it; the agent remembers nothing between runs. Durable multi-turn state is
weft/thread. - An agent cannot be reconfigured after
New. There is no setter, and middleware cannot be added to the model chain later. Per-run changes are run options, which leave the agent untouched. agt.Tools()reports the static tool set. Tools aweft.ToolSourcesupplies at run time are not in it, nor in the manifest: both describe the code, not a live registry.
Complete example
Section titled “Complete example”concepts/agents.go builds the support agent once, runs it for three questions at once, and reads its configuration back:
package main
import ( "context" "fmt" "io" "sync"
"github.com/weftgo/weft" "github.com/weftgo/weft/wefttest")
// OrderStore is the dependency the Orders plugin closes over.type OrderStore struct { Status map[string]string}
// OrderLookup is the lookup_order tool's input.type OrderLookup struct { OrderID string `json:"order_id" jsonschema:"the order to look up"`}
// Orders is a plugin: the order tools and their policy, closed over the// store they need, returned as one weft.Option.func Orders(store *OrderStore) weft.Option { return weft.Options( weft.Tool("lookup_order", "Look up an order by ID.", func(ctx context.Context, in OrderLookup) (string, error) { return "order " + in.OrderID + ": " + store.Status[in.OrderID], nil }), weft.MaxResultBytes(4096), )}
func newSupportAgent(model weft.Model, store *OrderStore) *weft.Agent { return weft.New(model, weft.Name("support-bot"), weft.Instructions("You are a support agent."), Orders(store), )}
// askConcurrently runs one agent value for several questions at once and// returns the answers in question order.func askConcurrently(ctx context.Context, agt *weft.Agent, questions []string) ([]string, error) { answers := make([]string, len(questions)) errs := make([]error, len(questions)) var wg sync.WaitGroup for i, q := range questions { wg.Go(func() { // one agent value, many runs at once: runs share no state res, err := agt.Generate(ctx, weft.Prompt(q)) if err != nil { errs[i] = err return } answers[i] = res.Text() }) } wg.Wait() for _, err := range errs { if err != nil { return nil, err } } return answers, nil}
// describeAgent prints what an agent value reports about its own// configuration.func describeAgent(w io.Writer, model weft.Model, store *OrderStore) { temp := 0.2 agt := weft.New(model, weft.Name("support-bot"), weft.Thinking(weft.ThinkingConfig{Level: weft.ThinkLow}), weft.Params(weft.RequestParams{Temperature: &temp}), Orders(store), ) fmt.Fprintln(w, "name:", agt.Name()) fmt.Fprintln(w, "model:", weft.InfoOf(agt.Model())) for _, t := range agt.Tools() { fmt.Fprintln(w, "tool:", t.Name) } fmt.Fprintln(w, "thinking low:", agt.Thinking().Level == weft.ThinkLow) fmt.Fprintln(w, "tool choice auto:", agt.ToolChoice().Mode == weft.ToolChoiceAuto)
p := agt.Params() // a copy: changing it does not reach the agent *p.Temperature = 0.9 fmt.Fprintln(w, "temperature:", *agt.Params().Temperature)}
// demoAgents builds the support agent once, runs it for three questions at// once, and reads its configuration back.func demoAgents(ctx context.Context, w io.Writer) error { store := &OrderStore{Status: map[string]string{"1234": "shipped"}} model := wefttest.Script( wefttest.Say("Your order has shipped."), wefttest.Say("Your order has shipped."), wefttest.Say("Your order has shipped."), ) agt := newSupportAgent(model, store) answers, err := askConcurrently(ctx, agt, []string{ "Where is order 1234?", "Has order 1234 left?", "Is order 1234 on its way?", }) if err != nil { return err } for _, a := range answers { fmt.Fprintln(w, "answer:", a) } describeAgent(w, wefttest.Script(), store) return nil}Run it from docs-site/examples:
go run ./concepts agentsgo test ./conceptsanswer: Your order has shipped.answer: Your order has shipped.answer: Your order has shipped.name: support-botmodel: {wefttest script}tool: lookup_orderthinking low: truetool choice auto: truetemperature: 0.2Best practices & gotchas
Section titled “Best practices & gotchas”- Build the agent once, at startup.
Newcomputes the manifest hash and builds the model chain once, and every run reuses them; building an agent per request repeats that work. (Source:weft/core/agent.go:747-760) - Pass dependencies into plugins as parameters. A plugin that reads a global cannot be tested with a second store, and nothing in weft registers plugins for you. (Source:
weft/core/agent.go:49-55,ExampleOptions) - Expect a panic, not an error, for a construction mistake. A nil model, a nil tool and a duplicate tool name all panic at
New, so a wrong agent never starts. (Source:weft/core/agent.go:678-684,weft/core/tool.go:481-497) - Start a per-run
Paramsoverride fromagt.Params(). A run-levelweft.Paramsreplaces the agent’s struct whole, so a run that sets onlyTemperatureloses the agent’sMaxTokens. (Source:weft/core/agent.go:384-399,ExampleAgent_Params)
See also
Section titled “See also”- Change an agent’s configuration for one run
- What does a run return?
- How do tools work?
- Describe your agents in weft.json (the manifest)
- Glossary: run
Symbols used: weft.Agent · weft.New · weft.Option · weft.Options · weft.ToolOptions · weft.Name · weft.Instructions · weft.Tool · weft.MaxResultBytes · weft.Thinking · weft.Params · weft.InfoOf · weft.Manifest · weft.ToolSource · wefttest.Script