---
title: What is an agent in weft?
description: A weft.Agent is an immutable value — a model, instructions, tools and policy — that weft.New builds once and every run reuses, concurrently.
url: https://weftgo.dev/docs/concepts/agents/
type: concept
sources:
  - weft/AGENTS.md (block 2, lines 62–90)
  - weft/core/agent.go:31-55 (Option, Options)
  - weft/core/agent.go:71-76 (Instructions)
  - weft/core/agent.go:416-427 (Name)
  - weft/core/agent.go:602-761 (Agent, New, defaults at 21-28)
  - weft/core/agent.go:902-993 (Agent.Tools, Name, Model, Params, Thinking, ToolChoice)
  - weft/core/tool.go:481-497 (a *ToolDef is an Option; deep copy; duplicate and nil panics)
  - weft/core/message.go:15-20 (no system role)
  - weft/README.md:238-262 (Composing agents)
  - weft/CHANGELOG.md:5357 (Agent.Model), 6270 (weft.Options)
  - weft/example_test.go ExampleOptions, ExampleAgent_Model
  - weft/core/example_test.go ExampleAgent_Params, ExampleAgent_Thinking, ExampleAgent_ToolChoice
  - docs-site/examples/concepts/agents.go, concepts_test.go TestAgentValueConcurrent, TestPluginComposes
---

# What is an agent in weft?

An agent in weft is a [`weft.Agent`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#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`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#New) and run it as many times as you need, from as many goroutines as you need; the [runs](https://weftgo.dev/docs/reference/glossary/#run) share no state.

The support agent in `concepts/agents.go` is a name, an instruction and a plugin that brings its tools:

```go title="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](https://github.com/weftgo/weftgo.dev/blob/main/docs-site/examples/concepts/agents.go)

Every argument after the model is a [`weft.Option`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#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

`weft.New` applies its options in order to a fresh `Agent`, then freezes it: the tool set is deep-copied, the [`WrapModel`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#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?](https://weftgo.dev/docs/concepts/runs/).

## An agent is a value

`weft.New(m, opts...)` takes any [`weft.Model`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Model): a provider adapter (`weft/openai`, `weft/anthropic`, `weft/google`) or a test double such as [`wefttest.Script`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/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`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#MaxSteps) |
| Tool calls executing at once | 4 | [`weft.Parallelism`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Parallelism) |
| `RETRY` results per tool since its last success | 3 | [`weft.MaxModelRetries`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#MaxModelRetries) |
| One tool result's size | 64 KiB | [`weft.MaxResultBytes`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#MaxResultBytes) |

`Option` is a sealed interface: you get options from weft's constructors, never by implementing one. Tools are options too. [`weft.Tool`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#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`:

```go title="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](https://github.com/weftgo/weftgo.dev/blob/main/docs-site/examples/concepts/agents.go)

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](https://weftgo.dev/docs/guides/conversations/).

## Name and instructions

[`weft.Name`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Name) names the agent. The name appears on every run's `RunStart.Agent` event and in the manifest; [`weft.Manifest`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#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`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#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](https://weftgo.dev/docs/guides/run-options/) covers every option that works at both levels.

## Compose options

[`weft.Options`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#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`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#ToolOptions) is the per-tool counterpart: a named bundle of `Timeout`, `MaxResultBytes` and similar options for one tool. [Set per-tool policy](https://weftgo.dev/docs/guides/tool-policy/) shows it. The package example [`ExampleOptions`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#example-Options) is the same plugin shape.

## 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:

```go title="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](https://github.com/weftgo/weftgo.dev/blob/main/docs-site/examples/concepts/agents.go)

| Accessor | Returns |
|---|---|
| [`agt.Name()`](https://pkg.go.dev/github.com/weftgo/weft/core@v0.20.2#Agent.Name) | The `Name` option's value; empty when unnamed. |
| [`agt.Model()`](https://pkg.go.dev/github.com/weftgo/weft/core@v0.20.2#Agent.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()`](https://pkg.go.dev/github.com/weftgo/weft/core@v0.20.2#Agent.Tools) | Copies of the registered tools in registration order. Changing a copy does not reach the agent. |
| [`agt.Params()`](https://pkg.go.dev/github.com/weftgo/weft/core@v0.20.2#Agent.Params) | The default sampling knobs, as a copy with fresh pointers; the zero value leaves every knob to the adapter. |
| [`agt.Thinking()`](https://pkg.go.dev/github.com/weftgo/weft/core@v0.20.2#Agent.Thinking) | The default reasoning level; the zero value, `ThinkUnset`, keeps the provider default. |
| [`agt.ToolChoice()`](https://pkg.go.dev/github.com/weftgo/weft/core@v0.20.2#Agent.ToolChoice) | The default tool choice; the zero value is `ToolChoiceAuto`. |

The core package's examples [`ExampleAgent_Model`](https://pkg.go.dev/github.com/weftgo/weft/core@v0.20.2#example-Agent.Model), [`ExampleAgent_Params`](https://pkg.go.dev/github.com/weftgo/weft/core@v0.20.2#example-Agent.Params), [`ExampleAgent_Thinking`](https://pkg.go.dev/github.com/weftgo/weft/core@v0.20.2#example-Agent.Thinking) and [`ExampleAgent_ToolChoice`](https://pkg.go.dev/github.com/weftgo/weft/core@v0.20.2#example-Agent.ToolChoice) pin each accessor. Walking `agt.Model()` through its middleware with `weft.Unwrap` is shown on [What is a Model?](https://weftgo.dev/docs/concepts/models/#unwrap).

## 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`](https://weftgo.dev/docs/sessions/overview/).
- **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`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#ToolSource) supplies at run time are not in it, nor in the manifest: both describe the code, not a live registry.

## Complete example

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

```go title="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
}
```

[concepts/agents.go on GitHub](https://github.com/weftgo/weftgo.dev/blob/main/docs-site/examples/concepts/agents.go)

Run it from `docs-site/examples`:

```sh
go run ./concepts agents
go test ./concepts
```

```text
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
```

## Best practices & gotchas

- **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`)

## See also

- [Change an agent's configuration for one run](https://weftgo.dev/docs/guides/run-options/)
- [What does a run return?](https://weftgo.dev/docs/concepts/runs/)
- [How do tools work?](https://weftgo.dev/docs/concepts/tools/)
- [Describe your agents in weft.json (the manifest)](https://weftgo.dev/docs/guides/manifest/)
- [Glossary: run](https://weftgo.dev/docs/reference/glossary/#run)

Symbols used: [`weft.Agent`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Agent) · [`weft.New`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#New) · [`weft.Option`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Option) · [`weft.Options`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Options) · [`weft.ToolOptions`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#ToolOptions) · [`weft.Name`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Name) · [`weft.Instructions`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Instructions) · [`weft.Tool`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Tool) · [`weft.MaxResultBytes`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#MaxResultBytes) · [`weft.Thinking`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Thinking) · [`weft.Params`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Params) · [`weft.InfoOf`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#InfoOf) · [`weft.Manifest`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#Manifest) · [`weft.ToolSource`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2#ToolSource) · [`wefttest.Script`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/wefttest#Script)
