> For the complete documentation index, see [llms.txt](/llms.txt).
> A full single-fetch corpus is available at [llms-full.txt](/llms-full.txt).
---
last_verified: 2026-10-09
title: Context API
description: The *agnt5.Context every Go handler receives — run details, state, sleep, asking people, logging, events, and model calls
category: Go SDK
hideRightSidebar: true
---

Every function and workflow handler receives a `*agnt5.Context`. It also implements `context.Context`, so you can pass it to any API that takes one.

```go
func audit(ctx *agnt5.Context, in AuditInput) (map[string]string, error) {
	ctx.Logger().Info("Auditing order", "order_id", in.OrderID, "attempt", ctx.Attempt())
	return map[string]string{"run_id": ctx.RunID()}, nil
}
```

## Run details

| Method | Returns |
|---|---|
| `ctx.RunID()` | The ID of the current run. |
| `ctx.InvocationID()` | The ID of this invocation. |
| `ctx.Attempt()` | Which try this is, starting at `0`. |
| `ctx.ComponentName()` | The registered name of the running component. |
| `ctx.ComponentType()` | `function`, `workflow`, `tool`, or `agent`. |
| `ctx.Metadata(key)`, `ctx.MetadataMap()` | Platform metadata for the run. |

## State and sleep

| Method | Description |
|---|---|
| `ctx.State()` | Run-scoped state: `Set`, `Get`, `GetString`, `List`, `Delete`. See [Workflows](/sdk/go/workflows.md#workflow-state). |
| `ctx.Sleep(duration, agnt5.WithSleepKey(key))` | Pause the workflow durably. |

## Ask a person

`ctx.AskUser(request)` pauses the workflow until someone answers, and returns the answer. On the first call it returns a `*WaitingForUserInputError`: return that error from your handler so the run can pause. When the person answers, the workflow replays and the same call returns the answer.

```go
answer, err := ctx.AskUser(agnt5.UserInputRequest{
	ID:     "approve_refund",
	Prompt: "Approve this refund?",
	Type:   agnt5.HITLApproval,
	Options: []agnt5.HITLOption{
		{Value: "approve", Label: "Approve"},
		{Value: "reject", Label: "Reject"},
	},
})
if err != nil {
	return nil, err // pauses the run on the first call
}
```

`ctx.RequestApproval(prompt, metadata)` is a shortcut that returns a `bool`. People answer in Studio or with the [resume API](/docs/api-reference/runs-and-traces/resume-a-workflow.md). See [Human-in-the-loop](/docs/build/human-in-the-loop.md).

## Logging and events

| Method | Description |
|---|---|
| `ctx.Logger().Info(message, keyvals...)` | Structured log line with key-value pairs; also `Debug`, `Warn`, and `Error`. Logs appear in `agnt5 inspect logs`. |
| `ctx.Emit(agnt5.Event{Type, Data})` | Record a custom event in the run's journal, for example `agnt5.Event{Type: "order.flagged", Data: map[string]any{"reason": reason}}`. |

## Model calls

`ctx.Generate(model, request)` calls a language model and records the call in the run's trace. See [LM](/sdk/go/lm.md).

## Next steps

* [Workflows](/sdk/go/workflows.md): steps, tasks, and state.
* [LM](/sdk/go/lm.md): model clients and requests.
* [Human-in-the-loop](/docs/build/human-in-the-loop.md): every way to pause for a person.
