
The agent loop in Go
The whole agent loop in about 30 lines of Go, read stage by stage.
On the agent compass The whole loop, all four stagesAn AI agent lets a model choose the next tool to run, turn after turn, using whatever the earlier tools reported, and it keeps going until the work is finished or a limit cuts it off. The model takes part in that only by talking. It sits behind an API, takes text in, and gives text back. Keeping the conversation, executing the tools it requests, and recognizing the end of the run are all jobs for code on your side. That code is the harness, and the loop at the middle of it fits on one screen.
Below is that loop, taken from the companion repository. No model is needed to run it. That is deliberate: the loop is ordinary Go, so you can write a test for each of its rules long before you pay for an API call.
Four stages, every turn
A turn is a single exchange. The harness sends the conversation so far, gets one reply, and does what the reply calls for. Every turn passes through the same four stages:
The agent compass
- Build the request
Decide what the model sees: instructions, the conversation so far, tool results, and the tools it may call.
- Call the model
Send the request and read the reply, with a deadline and a count of what it cost.
- Run the tools
Carry out the calls the model asked for, and add each result to the conversation.
- Decide
Stop with an answer, stop at a limit, or go around again.
- What each call sends
- Call 1 sent the tool list and 1 message: 583 tokens.
- Call 2 sent the tool list and 4 messages, 3 of them new: 834 tokens.
- Call 3 sent the tool list and 7 messages, 3 of them new: 1,846 tokens.
- Call 4 sent the tool list and 10 messages, 3 of them new: 2,164 tokens.
- In all, the four calls sent 5,427 tokens.
The stages double as a debugging map. If a run never ends, look at Decide. If the agent acts as though it never saw a file it read long ago, look at Build the request. Knowing which stage misbehaved narrows the search to a few lines.
The model is one method
From the loop’s side, a model is just a thing that returns the next reply:
// Model is anything that can produce the next reply in a conversation:
// the scripted fake in this chapter, a real model from chapter 2.
type Model interface {
Generate(ctx context.Context, req Request) (Reply, error)
}
A Request carries the messages and the definitions of the tools on offer.
What comes back is one assistant message, and it may contain text, tool calls, or a mix.
With only one method to satisfy, a canned stand-in and a real HTTP client look identical to the loop.
The companion code first plugs in Fake, which hands back replies from a prepared list.
It later plugs in a client that talks to a real model, and the loop itself is untouched.
The loop resends the whole conversation each turn
Here is Run:
// Run runs the agent loop: it sends the conversation to the model, runs
// any tools the model asks for, and repeats until the model answers
// without calling a tool, the turn limit is reached, or ctx is done. It
// returns the conversation so far even when it returns an error.
func Run(ctx context.Context, m Model, tools []Tool,
msgs []Message, lim Limits) (Result, error) {
defs, byName := index(tools)
msgs = slices.Clone(msgs)
for range lim.MaxTurns {
if err := ctx.Err(); err != nil {
return Result{Messages: msgs}, fmt.Errorf("run: %w", err)
}
req := Request{Messages: msgs, Tools: defs}
reply, err := m.Generate(ctx, req)
if err != nil {
err = fmt.Errorf("generate: %w", err)
return Result{Messages: msgs}, err
}
msgs = append(msgs, reply.Message)
if len(reply.Message.ToolCalls) == 0 {
return Result{Messages: msgs}, nil
}
for _, call := range reply.Message.ToolCalls {
msgs = append(msgs, runTool(ctx, byName, call))
}
}
return Result{Messages: msgs}, ErrMaxTurns
}
Read it stage by stage.
Build the request. Before the loop starts, index(tools) splits the tools in two.
defs holds each tool’s definition (its name, description, and parameter schema), which is all the model ever sees of a tool, and goes into every request.
byName maps each name to the Tool itself, so when a reply asks for run_tests, the loop can find the Go function to call.
index panics if two tools share a name, since the model could never say which one it meant; that is a mistake in the program, caught before the first call.
Every call sends msgs in full.
A model carries no state from one call to the next, so the only thing it knows on a given call is what that request contains.
That means the requests keep getting bigger as the run goes on; tool calling on the wire shows how many tokens the first request already takes before the task is read.
Why Run clones the slice
Run calls slices.Clone before it appends anything, so the caller’s slice and its backing array stay as they were.
A Go slice is a small window onto an array that lives elsewhere (the backing array), and append may write into that array in place.
Without the copy, the loop’s appends could overwrite elements the caller still holds in its own slice.
Call the model. m.Generate(ctx, req) receives the run’s ctx, which lets a real client drop the HTTP request if the run is cancelled.
Run the tools. The assistant’s reply goes into msgs first, and only then do its tools run.
That way every tool result refers to a call already present in the conversation, and each call gets one tool message, in the order the model listed them.
This order is required, since the APIs refuse a request where some tool call lacks its result.
Decide. A reply without tool calls is the answer, and the loop returns.
If it has tool calls, the loop takes another turn, and when for range lim.MaxTurns is exhausted, Run returns ErrMaxTurns.
Two details are easy to skim past.
Run hands back a Result with the messages even on failure, because the turns leading up to a failure are the evidence for why it happened.
And each turn begins by checking ctx.Err(), so a deadline or Ctrl-C ends the run without paying for one more model call.
Calls run in order
In the bug fix this example replays, one reply asks for two things: change duration.go, then run the tests.
If both ran at once, the test run might see the file before the change and report a failure that the edit had already fixed.
So this loop runs the calls in a reply sequentially, in the model’s order, which can never cause that problem.
Parallel tool calls are faster, but they force the harness to work out which calls are safe to overlap, and that is a problem of its own.
Every problem becomes a message
Nothing in Run ends the run because a tool went wrong.
runTool converts every kind of failure into a tool message for the model to read.
In plain terms, it looks up the tool by name, runs it, and wraps whatever comes back, success or failure, in a message flagged IsError when it went wrong.
In the unknown-tool branch, %q prints a string in double quotes, and slices.Sorted(maps.Keys(byName)) collects the tool names into an alphabetical list.
// runTool runs one tool call and returns the tool message that answers
// it. Every problem becomes an error message the model can read.
func runTool(ctx context.Context, byName map[string]Tool,
call ToolCall) Message {
msg := Message{Role: "tool", ToolCallID: call.ID}
t, ok := byName[call.Name]
if !ok {
names := slices.Sorted(maps.Keys(byName))
msg.IsError = true
msg.Content = fmt.Sprintf("no tool named %q; the tools are %s",
call.Name, strings.Join(names, ", "))
return msg
}
out, err := t.Run(ctx, call.Arguments)
if err != nil {
msg.IsError = true
msg.Content = err.Error()
return msg
}
msg.Content = out
return msg
}
If the model names a tool that doesn’t exist, the reply lists the tools that do.
If a tool returns an error, the error text becomes the result, with IsError set.
On the next turn the model sees what went wrong and can send a corrected call.
Consider the other options. Ending the run discards all the work done before the mistake. Picking the nearest tool name and running it executes a call the model never made. Reporting the error costs a single turn. A run written by hand in the API’s format, standing in for a model’s replies, shows the model making three mistakes in a row: it calls a tool that isn’t defined, passes a path as an array, and asks to replace text the file doesn’t contain. Every error message explains the problem, and every following call is correct. The ch04 example replays that run.
How quickly the model recovers depends on how the error is worded.
A message like old text appears 0 times in duration.go, want exactly once tells it what to change, while a bare “please try again” tells it nothing.
The turn limit comes first
Without a turn limit, nothing guarantees the loop ends, and a real model bills you for every turn it takes.
So MaxTurns is the one setting every caller must choose, and leaving it at zero permits no turns at all, so every caller has to set it.
There’s no correct number you can know ahead of time. In recorded runs, all 40 bug-fix runs (ten on each of four models, October 2026) finished in four to six turns, so the program sets the limit to ten. Agent frameworks ship defaults that range from 10 turns to thousands of steps to no limit at all. A default can’t know what your task needs, so choose the limit yourself and report hitting it to the caller as a failure.
A real model fits behind the same interface
This same Run works against a real model unchanged.
On the way in, the program wraps the client in another Model that totals the cost of the run.
In plain terms, meter forwards every Generate call to the real model, then adds the reply’s tokens and cost to running totals.
This code comes from the ch04 example, which runs this same loop against a real model:
// meter is a Model that passes each call on to another Model and adds
// up the calls, tokens, and cost.
type meter struct {
agent.Model
calls int
usage agent.Usage
}
func (m *meter) Generate(ctx context.Context,
req agent.Request) (agent.Reply, error) {
reply, err := m.Model.Generate(ctx, req)
if err != nil {
return reply, err //nolint:wrapcheck // passed through unchanged
}
m.calls++
m.usage.PromptTokens += reply.Usage.PromptTokens
m.usage.CompletionTokens += reply.Usage.CompletionTokens
m.usage.Cost += reply.Usage.Cost
return reply, nil
}
How meter becomes a Model
meter embeds a Model: the line agent.Model with no field name pulls that interface’s methods into meter.
It then defines its own Generate, which replaces the embedded one and calls it as m.Model.Generate.
Go interfaces are satisfied implicitly, so *meter (the pointer receiver lets it update calls and usage) is a Model itself, and the loop has no way to know there is a client inside.
The //nolint:wrapcheck comment tells the linter that returning the inner error unchanged is intended.
The same trick wraps the model to add retries, timeouts, and tracing while the loop stays as it is.
The New to Go? box has short notes on interfaces and receivers.
Beyond this short loop come the cases it leaves out: a provider that rejects the request, a reply the loop can’t use (a tool-call ID that appears twice, an answer truncated by the token limit), a run that overspends, and Ctrl-C arriving while a reply’s tools are running.
Each one ends the run with a named stop reason that callers test with errors.Is, as they already can for ErrMaxTurns; ErrBudgetExceeded is one of them.
Checking for a stop reason
Each stop reason is an error value exported by the package, and errors.Is(err, agent.ErrMaxTurns) reports whether err is that value, even if other code has wrapped it with fmt.Errorf and %w on the way up.
A caller can then react differently to a run that hit its limit than to one that failed.
Run it
From the code directory of a clone of the repository:
$ go run ./ch01
$ go test ./ch01/...
The first command plays the bug fix through the loop and prints every turn. Here are the first two:
task: The tests in this project fail. Find out why and fix it.
turn 1
model asks: run_tests {}
model asks: list_files {}
tool result: --- FAIL: TestParse (0.00s) (5 lines)
tool result: duration.go, duration_test.go
turn 2
model asks: read_file {"path": "duration.go"}
model asks: read_file {"path": "duration_test.go"}
tool result: // Package duration parses durations suc... (44 lines)
tool result: package duration (36 lines)
Each model asks line is one tool call in the model’s reply, and each tool result line is a tool message the loop appended, one per call and in the same order.
In turn 1 the model runs the tests and lists the files; in turn 2 it reads both files, so its third reply can name the exact line to change.
Nothing in Run knows it is fixing a test: it builds the request, calls the model, runs what was asked, and goes around again.
The ch01 example shows all four turns, including the edit and the final answer.
The tests cover each rule separately: an answer ends the run, the turn limit ends the run, each tool call receives its own tool message, failing tools come back flagged as errors, and a made-up tool name is answered with the real list.
None of them uses the network or an API key.
Where this goes next
- Tool calling in Go, on the wire: what the request and reply look like as JSON once a real model is attached.
- A real agent that fixes a failing test: the same loop with a real model’s recorded replies.
- Glossary: agent, harness, turn, tool call, and the rest of the vocabulary.
type Model interface {
Generate(ctx context.Context, req Request) (Reply, error)
}
Model is anything that can produce the next reply in a conversation: the scripted fake in this chapter, a real model from chapter 2.
Model is anything that can produce the next reply in a conversation: the scripted fake in this chapter, a real model from chapter 2. type Model interface { Generate(ctx context.Context, req Request) (Reply, error) }. In ch01/agent/types.go, line 13.
func (Model) Generate(ctx context.Context, req Request) (Reply, error)
func (Model) Generate(ctx context.Context, req Request) (Reply, error). In ch01/agent/types.go, line 14.
var ctx context.Context
var ctx context.Context. In ch01/agent/types.go, line 14.
package context ("context")
Package context defines the Context type, which carries deadlines, cancellation signals, and other request-scoped values across API boundaries and between processes.
Package context defines the Context type, which carries deadlines, cancellation signals, and other request-scoped values across API boundaries and between processes. package context ("context"). Documented at pkg.go.dev, context.
type Context interface {
// Deadline returns the time when work done on behalf of this context
// should be canceled. Deadline returns ok==false when no deadline is
// set. Successive calls to Deadline return the same results.
Deadline() (deadline time.Time, ok bool)
// Done returns a channel that's closed when work done on behalf of this
// context should be canceled. Done may return nil if this context can
// never be canceled. Successive calls to Done return the same value.
// The close of the Done channel may happen asynchronously,
// ...
}
A Context carries a deadline, a cancellation signal, and other values across API boundaries.
A Context carries a deadline, a cancellation signal, and other values across API boundaries. type Context interface { // Deadline returns the time when work done on behalf of this context // should be canceled. Deadline returns ok==false when no deadline is // set. Successive calls to Deadline return the same results. Deadline() (deadline time.Time, ok bool) // Done returns a channel that's closed when work done on behalf of this // context should be canceled. Done may return nil if this context can // never be canceled. Successive calls to Done return the same value. // The close of the Done channel may happen asynchronously, // ... }. Documented at pkg.go.dev, context.Context.
type Request struct {
Messages []Message
Tools []ToolDef
}
Request is what the harness sends the model on each turn.
Request is what the harness sends the model on each turn. type Request struct { Messages []Message Tools []ToolDef }. In ch01/agent/types.go, line 22.
type Reply struct {
Message Message // the assistant's message: text, tool calls, or both
}
Reply is the model's answer to one Request.
Reply is the model's answer to one Request. type Reply struct { Message Message // the assistant's message: text, tool calls, or both }. In ch01/agent/types.go, line 57.
type error interface {
Error() string
}
The error built-in interface type is the conventional interface for representing an error condition, with the nil value representing no error.
The error built-in interface type is the conventional interface for representing an error condition, with the nil value representing no error. type error interface { Error() string }. Documented at pkg.go.dev, builtin.error.
func Run(ctx context.Context, m Model, tools []Tool,
msgs []Message, lim Limits) (Result, error)
Run runs the agent loop: it sends the conversation to the model, runs any tools the model asks for, and repeats until the model answers without calling a tool, the turn limit is reached, or ctx is done.
Run runs the agent loop: it sends the conversation to the model, runs any tools the model asks for, and repeats until the model answers without calling a tool, the turn limit is reached, or ctx is done. func Run(ctx context.Context, m Model, tools []Tool, msgs []Message, lim Limits) (Result, error). In ch01/agent/loop.go, line 22.
var ctx context.Context
var ctx context.Context. In ch01/agent/loop.go, line 22.
package context ("context")
Package context defines the Context type, which carries deadlines, cancellation signals, and other request-scoped values across API boundaries and between processes.
Package context defines the Context type, which carries deadlines, cancellation signals, and other request-scoped values across API boundaries and between processes. package context ("context"). Documented at pkg.go.dev, context.
type Tool struct {
Def ToolDef
Run func(ctx context.Context, args jsontext.Value) (string, error)
}
Tool is the harness's side of a tool: the definition the model sees, and the Go function that runs it.
Tool is the harness's side of a tool: the definition the model sees, and the Go function that runs it. type Tool struct { Def ToolDef Run func(ctx context.Context, args jsontext.Value) (string, error) }. In ch01/agent/types.go, line 65.
type Message struct {
Role string // "system", "user", "assistant", or "tool"
Content string // empty when the message has tool calls
ToolCalls []ToolCall // set on assistant messages that call tools
ToolCallID string // on tool messages: the call this answers
IsError bool // set on tool messages that report a failure
}
Message is one entry in the conversation.
Message is one entry in the conversation. type Message struct { Role string // "system", "user", "assistant", or "tool" Content string // empty when the message has tool calls ToolCalls []ToolCall // set on assistant messages that call tools ToolCallID string // on tool messages: the call this answers IsError bool // set on tool messages that report a failure }. In ch01/agent/types.go, line 28.
type Limits struct {
MaxTurns int
}
Limits holds the settings that bound a run.
Limits holds the settings that bound a run. type Limits struct { MaxTurns int }. In ch01/agent/types.go, line 75.
type Result struct {
Messages []Message
}
Result is what a run produced, whether or not it ended in an error.
Result is what a run produced, whether or not it ended in an error. type Result struct { Messages []Message }. In ch01/agent/types.go, line 80.
var byName map[string]Tool
var byName map[string]Tool. In ch01/agent/loop.go, line 24.
func index(tools []Tool) ([]ToolDef, map[string]Tool)
index splits tools into the definitions the model sees and the tools the harness looks up by name.
index splits tools into the definitions the model sees and the tools the harness looks up by name. func index(tools []Tool) ([]ToolDef, map[string]Tool). In ch01/agent/loop.go, line 52.
package slices ("slices")
Package slices defines various functions useful with slices of any type.
Package slices defines various functions useful with slices of any type. package slices ("slices"). Documented at pkg.go.dev, slices.
func Clone[S ~[]E, E any](s S) S
Clone returns a copy of the slice.
Clone returns a copy of the slice. func Clone[S ~[]E, E any](s S) S. Documented at pkg.go.dev, slices.Clone.
func (Context) Err() error
If Done is not yet closed, Err returns nil.
If Done is not yet closed, Err returns nil. func (Context) Err() error. Documented at pkg.go.dev, context.Context.Err.
var nil Type
nil is a predeclared identifier representing the zero value for a pointer, channel, func, interface, map, or slice type.
nil is a predeclared identifier representing the zero value for a pointer, channel, func, interface, map, or slice type. var nil Type. Documented at pkg.go.dev, builtin.nil.
field Messages []Message
field Messages []Message. In ch01/agent/types.go, line 81.
package fmt ("fmt")
Package fmt implements formatted I/O with functions analogous to C's printf and scanf.
Package fmt implements formatted I/O with functions analogous to C's printf and scanf. package fmt ("fmt"). Documented at pkg.go.dev, fmt.
func Errorf(format string, a ...any) (err error)
Errorf formats according to a format specifier and returns the string as a value that satisfies error.
Errorf formats according to a format specifier and returns the string as a value that satisfies error. func Errorf(format string, a ...any) (err error). Documented at pkg.go.dev, fmt.Errorf.
field Messages []Message
field Messages []Message. In ch01/agent/types.go, line 23.
func append(slice []Type, elems ...Type) []Type
The append built-in function appends elements to the end of a slice.
The append built-in function appends elements to the end of a slice. func append(slice []Type, elems ...Type) []Type. Documented at pkg.go.dev, builtin.append.
field Message Message
the assistant's message: text, tool calls, or both
the assistant's message: text, tool calls, or both field Message Message. In ch01/agent/types.go, line 58.
func len(v Type) int
The len built-in function returns the length of v, according to its type:
The len built-in function returns the length of v, according to its type: func len(v Type) int. Documented at pkg.go.dev, builtin.len.
field ToolCalls []ToolCall
set on assistant messages that call tools
set on assistant messages that call tools field ToolCalls []ToolCall. In ch01/agent/types.go, line 31.
func runTool(ctx context.Context, byName map[string]Tool,
call ToolCall) Message
runTool runs one tool call and returns the tool message that answers it.
runTool runs one tool call and returns the tool message that answers it. func runTool(ctx context.Context, byName map[string]Tool, call ToolCall) Message. In ch01/agent/loop.go, line 69.
var ErrMaxTurns = errors.New("agent: max turns reached")
ErrMaxTurns means the run stopped because it reached Limits.MaxTurns without an answer.
ErrMaxTurns means the run stopped because it reached Limits.MaxTurns without an answer. var ErrMaxTurns = errors.New("agent: max turns reached"). In ch01/agent/loop.go, line 14.
var ctx context.Context
var ctx context.Context. In ch01/agent/loop.go, line 69.
var byName map[string]Tool
var byName map[string]Tool. In ch01/agent/loop.go, line 69.
type string string
string is the set of all strings of 8-bit bytes, conventionally but not necessarily representing UTF-8-encoded text.
string is the set of all strings of 8-bit bytes, conventionally but not necessarily representing UTF-8-encoded text. type string string. Documented at pkg.go.dev, builtin.string.
type ToolCall struct {
ID string
Name string
Arguments jsontext.Value // JSON the model wrote; may be invalid
}
ToolCall is the model asking the harness to run a tool.
ToolCall is the model asking the harness to run a tool. type ToolCall struct { ID string Name string Arguments jsontext.Value // JSON the model wrote; may be invalid }. In ch01/agent/types.go, line 48.
field Role string
"system", "user", "assistant", or "tool"
"system", "user", "assistant", or "tool" field Role string. In ch01/agent/types.go, line 29.
field ToolCallID string
on tool messages: the call this answers
on tool messages: the call this answers field ToolCallID string. In ch01/agent/types.go, line 32.
func Sorted[E cmp.Ordered](seq iter.Seq[E]) []E
Sorted collects values from seq into a new slice, sorts the slice, and returns it.
Sorted collects values from seq into a new slice, sorts the slice, and returns it. func Sorted[E cmp.Ordered](seq iter.Seq[E]) []E. Documented at pkg.go.dev, slices.Sorted.
package maps ("maps")
Package maps defines various functions useful with maps of any type.
Package maps defines various functions useful with maps of any type. package maps ("maps"). Documented at pkg.go.dev, maps.
func Keys[Map ~map[K]V, K comparable, V any](m Map) iter.Seq[K]
Keys returns an iterator over keys in m.
Keys returns an iterator over keys in m. func Keys[Map ~map[K]V, K comparable, V any](m Map) iter.Seq[K]. Documented at pkg.go.dev, maps.Keys.
field IsError bool
set on tool messages that report a failure
set on tool messages that report a failure field IsError bool. In ch01/agent/types.go, line 33.
const true = 0 == 0
Untyped bool.
Untyped bool. const true = 0 == 0. Documented at pkg.go.dev, builtin.true.
field Content string
empty when the message has tool calls
empty when the message has tool calls field Content string. In ch01/agent/types.go, line 30.
func Sprintf(format string, a ...any) string
Sprintf formats according to a format specifier and returns the resulting string.
Sprintf formats according to a format specifier and returns the resulting string. func Sprintf(format string, a ...any) string. Documented at pkg.go.dev, fmt.Sprintf.
package strings ("strings")
Package strings implements simple functions to manipulate UTF-8 encoded strings.
Package strings implements simple functions to manipulate UTF-8 encoded strings. package strings ("strings"). Documented at pkg.go.dev, strings.
func Join(elems []string, sep string) string
Join concatenates the elements of its first argument to create a single string.
Join concatenates the elements of its first argument to create a single string. func Join(elems []string, sep string) string. Documented at pkg.go.dev, strings.Join.
field Run func(ctx context.Context, args jsontext.Value) (string, error)
field Run func(ctx context.Context, args jsontext.Value) (string, error). In ch01/agent/types.go, line 67.
field Arguments jsontext.Value
JSON the model wrote; may be invalid
JSON the model wrote; may be invalid field Arguments jsontext.Value. In ch01/agent/types.go, line 51.
func (error).Error() string
func (error).Error() string.
type meter struct {
agent.Model
calls int
usage agent.Usage
}
meter is a Model that passes each call on to another Model and adds up the calls, tokens, and cost.
meter is a Model that passes each call on to another Model and adds up the calls, tokens, and cost. type meter struct { agent.Model calls int usage agent.Usage }. In ch04/main.go, line 195.
package agent ("github.com/blackwell-systems/go-agents-book/code/ch04/agent")
Package agent is the agent loop and the types it works with.
Package agent is the agent loop and the types it works with. package agent ("github.com/blackwell-systems/go-agents-book/code/ch04/agent"). In ch04/agent.
type Model interface {
Generate(ctx context.Context, req Request) (Reply, error)
}
Model is anything that can produce the next reply in a conversation: a scripted fake, or a real model such as the OpenRouter client.
Model is anything that can produce the next reply in a conversation: a scripted fake, or a real model such as the OpenRouter client. type Model interface { Generate(ctx context.Context, req Request) (Reply, error) }. In ch04/agent/types.go, line 13.
type int int
int is a signed integer type that is at least 32 bits in size.
int is a signed integer type that is at least 32 bits in size. type int int. Documented at pkg.go.dev, builtin.int.
type Usage struct {
PromptTokens int // everything sent, counted in tokens
CompletionTokens int // everything the model wrote
CachedTokens int // prompt tokens served from the cache
ReasoningTokens int // completion tokens spent on reasoning
Cost float64 // in dollars, as the provider reported it
}
Usage is what one model call consumed.
Usage is what one model call consumed. type Usage struct { PromptTokens int // everything sent, counted in tokens CompletionTokens int // everything the model wrote CachedTokens int // prompt tokens served from the cache ReasoningTokens int // completion tokens spent on reasoning Cost float64 // in dollars, as the provider reported it }. In ch04/agent/types.go, line 75.
func (m *meter) Generate(ctx context.Context,
req agent.Request) (agent.Reply, error)
func (m *meter) Generate(ctx context.Context, req agent.Request) (agent.Reply, error). In ch04/main.go, line 201.
package context ("context")
Package context defines the Context type, which carries deadlines, cancellation signals, and other request-scoped values across API boundaries and between processes.
Package context defines the Context type, which carries deadlines, cancellation signals, and other request-scoped values across API boundaries and between processes. package context ("context"). Documented at pkg.go.dev, context.
type Request struct {
Messages []Message
Tools []ToolDef
MaxTokens int // longest reply; 0 means the provider's default
}
Request is what the harness sends the model on each turn.
Request is what the harness sends the model on each turn. type Request struct { Messages []Message Tools []ToolDef MaxTokens int // longest reply; 0 means the provider's default }. In ch04/agent/types.go, line 24.
type Reply struct {
Message Message // the assistant's text, tool calls, or both
FinishReason string // why the model stopped: "stop", "length", ...
Usage Usage // tokens counted for this call, and its cost
}
Reply is the model's answer to one Request.
Reply is the model's answer to one Request. type Reply struct { Message Message // the assistant's text, tool calls, or both FinishReason string // why the model stopped: "stop", "length", ... Usage Usage // tokens counted for this call, and its cost }. In ch04/agent/types.go, line 68.
func (Model) Generate(ctx context.Context, req Request) (Reply, error)
func (Model) Generate(ctx context.Context, req Request) (Reply, error). In ch04/agent/types.go, line 14.
field PromptTokens int
everything sent, counted in tokens
everything sent, counted in tokens field PromptTokens int. In ch04/agent/types.go, line 76.
field Usage Usage
tokens counted for this call, and its cost
tokens counted for this call, and its cost field Usage Usage. In ch04/agent/types.go, line 71.
field CompletionTokens int
everything the model wrote
everything the model wrote field CompletionTokens int. In ch04/agent/types.go, line 77.
field Cost float64
in dollars, as the provider reported it
in dollars, as the provider reported it field Cost float64. In ch04/agent/types.go, line 80.

