Skip to content

What is an agent in weft?

View as Markdown

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:

concepts/agents.go
// 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),
)
}

concepts/agents.go on GitHub

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.

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?.

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:

SettingDefaultOption
Steps per run10weft.MaxSteps
Tool calls executing at once4weft.Parallelism
RETRY results per tool since its last success3weft.MaxModelRetries
One tool result’s size64 KiBweft.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:

concepts/agents.go
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()

concepts/agents.go on GitHub

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.

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.

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 Options value may hold other Options values; 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.

The agent reports its configuration through accessors, so code that receives an *Agent can inspect it without the options it was built from:

concepts/agents.go
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)

concepts/agents.go on GitHub

AccessorReturns
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?.

  • 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 a weft.ToolSource supplies at run time are not in it, nor in the manifest: both describe the code, not a live registry.

concepts/agents.go builds the support agent once, runs it for three questions at once, and reads its configuration back:

concepts/agents.go
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
}

View concepts/agents.go on GitHub

Run it from docs-site/examples:

Terminal window
go run ./concepts agents
go test ./concepts
answer: Your order has shipped.
answer: Your order has shipped.
answer: Your order has shipped.
name: support-bot
model: {wefttest script}
tool: lookup_order
thinking low: true
tool choice auto: true
temperature: 0.2
  • Build the agent once, at startup. New computes 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 Params override from agt.Params(). A run-level weft.Params replaces the agent’s struct whole, so a run that sets only Temperature loses the agent’s MaxTokens. (Source: weft/core/agent.go:384-399, ExampleAgent_Params)

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