2026-06-28 23:03:57 +00:00
|
|
|
# pkg/agent
|
|
|
|
|
|
2026-06-30 20:40:37 +00:00
|
|
|
> The main loop that runs an LLM agent with guardrails.
|
2026-06-28 23:03:57 +00:00
|
|
|
|
2026-06-30 20:40:37 +00:00
|
|
|
## Responsibility
|
2026-06-28 23:03:57 +00:00
|
|
|
|
2026-06-30 20:40:37 +00:00
|
|
|
Coordinate the iterative cycle between the LLM and tool execution:
|
2026-06-28 23:03:57 +00:00
|
|
|
|
|
|
|
|
```
|
|
|
|
|
while iteration < MaxIterations:
|
|
|
|
|
response = llm.Generate(messages, tools)
|
|
|
|
|
if no tool calls: return response
|
|
|
|
|
for tool_call in response.ToolCalls:
|
|
|
|
|
if needs_approval: ask_user()
|
|
|
|
|
result = execute(tool_call)
|
|
|
|
|
append tool result to messages
|
|
|
|
|
```
|
|
|
|
|
|
2026-06-30 20:40:37 +00:00
|
|
|
## Public API
|
2026-06-28 23:03:57 +00:00
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
type Loop interface {
|
|
|
|
|
Run(ctx context.Context, input string) (Response, error)
|
|
|
|
|
RunStream(ctx context.Context, input string) iter.Seq2[Chunk, error]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
|
LLM llm.LLMClient
|
|
|
|
|
Persona persona.Persona
|
|
|
|
|
Tools tools.Registry
|
|
|
|
|
Sandbox Sandbox
|
|
|
|
|
MaxIters int
|
|
|
|
|
Approver Approver // nil = auto-approve all
|
|
|
|
|
OnIteration func(Iteration) // observability hook
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Response struct {
|
|
|
|
|
Content string
|
|
|
|
|
ToolCalls []tools.Call
|
|
|
|
|
Iterations int
|
|
|
|
|
Duration time.Duration
|
|
|
|
|
TokenUsage llm.TokenUsage
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2026-06-30 20:40:37 +00:00
|
|
|
## Guarantees
|
2026-06-28 23:03:57 +00:00
|
|
|
|
2026-06-30 20:40:37 +00:00
|
|
|
- **Termination**: Always terminates (max iterations, error, or final response)
|
|
|
|
|
- **Idempotency**: Re-running with the same input produces the same output (given the same LLM)
|
|
|
|
|
- **Observability**: Each iteration emits an OpenTelemetry span
|
|
|
|
|
- **Approval**: Destructive tools (`Ask` permission) require confirmation
|
2026-06-28 23:03:57 +00:00
|
|
|
|
2026-06-30 20:40:37 +00:00
|
|
|
## See also
|
2026-06-28 23:03:57 +00:00
|
|
|
|
|
|
|
|
- [pkg/tools](../tools/README.md) — Tool execution
|
|
|
|
|
- [pkg/llm](../llm/README.md) — LLMClient interface
|
|
|
|
|
- [pkg/persona](../persona/README.md) — Persona assembly
|