agents-go

Context management

“Context” means two distinct things, and the Go SDK keeps them separate by design:

  1. Local context your code needs — dependencies, the current user, loggers… This never reaches the LLM.
  2. Conversation context the LLM sees — the input item history. This is what Sessions, tool results and instructions shape.

Local context: RunContext

Where Python parameterizes everything on a generic RunContextWrapper[T], Go splits the two concerns idiomatically:

type AppContext struct {
	UserID string
	DB     *sql.DB
}

res, err := agents.Run(ctx, agent, input, agents.RunOptions{
	ModelProvider: provider,
	Context:       &AppContext{UserID: "u_123", DB: db},
})

Every tool, guardrail, hook and dynamic-instructions function receives the same *agents.RunContext; type-assert your value back:

tool := agents.NewFunctionTool("whoami", "Return the current user.",
	func(ctx context.Context, tc *agents.ToolContext, _ struct{}) (string, error) {
		app := tc.RunContext.Context.(*AppContext)
		return app.UserID, nil
	})

RunContext also carries run-scoped state the SDK maintains for you:

Field Meaning
Context Your value, verbatim
Usage Token usage accumulated so far (Usage)
Approvals Recorded human-in-the-loop decisions (Human-in-the-loop)

ToolContext embeds *RunContext and adds the call’s ToolName, ToolCallID and raw ToolArguments.

Tools run concurrently within a turn. If tools mutate your context value, make it goroutine-safe.

Agent/LLM context

The LLM only sees the conversation history. To expose data to it you have the same options as Python:

  1. Instructions — static or computed from the run context (dynamic instructions). Good for always-relevant facts (user name, current date).
  2. Input — append data to the input items when calling Run.
  3. Tools — let the model fetch data on demand via function tools.
  4. Retrieval / MCP — ground answers in external documents or services (MCP).