Observability
agent.Sink and agent.SinkFunc receive every execution lifecycle event for a run without altering control flow. Unlike agent.Policy, sinks are strictly observational; a panicking sink is recovered by the runtime and cannot disrupt execution or block other sinks.
Attach a Sink to a run
Pass agent.WithSink (or set RunOptions.Sink) on Generate or Stream to observe text deltas, tool invocations, step boundaries, and terminal errors:
package main
import (
"context"
"fmt"
"log"
"github.com/ijaidev/gojent/core/agent"
"github.com/ijaidev/gojent/providers/mockllm"
)
func main() {
assistant, err := agent.New(agent.Config{
Name: "assistant",
Model: mockllm.New("Repository summary ready."),
})
if err != nil {
log.Fatal(err)
}
sink := agent.SinkFunc(func(event agent.Event) {
switch event := event.(type) {
case agent.TextDelta:
fmt.Print(event.Text)
case agent.ToolStart:
log.Printf("tool %s: %s", event.Call.Name, event.Call.Arguments)
case agent.ToolEnd:
log.Printf("tool %s finished with error: %v", event.Call.Name, event.Err)
}
})
response, err := assistant.Generate(context.Background(), "Summarize this repository.",
agent.WithSink(sink),
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("\nTotal tokens: %d\n", response.Usage.TotalTokens)
}
Runtime event lifecycle
Every run emits typed values implementing the sealed agent.Event interface:
| Event | When emitted |
|---|---|
RunStart |
Once at the start of a run (RunID, Agent, Session, At). |
StepStart |
Before each model call (Step). |
MessageStart |
Before streamed assistant output for a step. |
TextDelta |
Text fragment (Step, Index, Text, Replace). |
ThinkingDelta |
Reasoning fragment (Step, Index, Text, Replace). |
ToolCallDelta |
Streamed tool-call fragment (ID, Name, Arguments). |
MessageEnd |
Completed assistant *llm.Message for a step. |
ToolStart |
Before a tool call is evaluated (Call, Spec). |
ToolUpdate |
Reserved for tools/schedulers reporting progress (not emitted by default runner). |
ToolEnd |
Paired once for every ToolStart, including denied, failed, or policy-aborted calls. |
StepEnd |
After a step’s model call and tool executions (Usage, Err). |
RunEnd |
Always the final event (Response, Err); Response may be non-nil on budget limits. |
Sinks must not block indefinitely in Emit; forward to a buffered channel or asynchronous queue when doing slow I/O. Because the runner isolates panics per sink, a panicking user sink never stops the internal Run.Events() stream from receiving RunEnd.
Wrapping seams for metrics
Policies decide and sinks observe. For latency histograms, token accounting, OpenTelemetry spans, or persistence error counters, wrap llm.Model, tool.Tool, or session.Store directly—see Extending Gojent for a concrete wrapper pattern and pkg.go.dev for all event structs.