Context

open class Context(val invocationContext: InvocationContext, actions: EventActions? = null, val functionCallId: String? = null, val toolConfirmation: ToolConfirmation? = null, val eventId: String? = null) : ReadonlyContext, ReadonlyToolContext

Execution context passed to agent callbacks, model callbacks, tools, and workflow nodes during an invocation.

Provides read access to invocation metadata and session state, along with methods to record state deltas, artifacts, and control-flow signals. Tool-specific operations such as requestConfirmation require a functionCallId, which is only populated during a tool call. The node-only members (such as output and routes) throw IllegalStateException unless this context was created for a workflow node; a callback, model-callback, or tool context is not a node activation and so has none of them.

A context is created in one of two ways: the public constructor is used for callbacks and tools, while the internal constructor is used for workflow node activations. Only the internal constructor populates node execution state (such as node and parent); contexts created with the public constructor leave that state empty, causing node-only members to throw.

CallbackContext and com.google.adk.kt.tools.ToolContext are thin subclasses kept for backward compatibility. Prefer Context in new code.

This class is open only so those two subclasses can exist as distinct JVM types that Java callers can reference. Every member is final so subclasses only forward constructor arguments without changing behavior.

Inheritors

Constructors

Link copied to clipboard
constructor(invocationContext: InvocationContext, actions: EventActions? = null, functionCallId: String? = null, toolConfirmation: ToolConfirmation? = null, eventId: String? = null)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

The event actions for the current context, holding state deltas and control-flow signals to be attached to emitted events.

Link copied to clipboard
Link copied to clipboard
override val agentName: String

The name of the agent that is being invoked.

Link copied to clipboard

The ArtifactService instance.

Link copied to clipboard

1-based attempt number, which a retry increments. 1 off-graph.

Link copied to clipboard
override val branch: String?

The branch of the invocation context.

Link copied to clipboard
Link copied to clipboard
override val context: ReadonlyContext

The readonly invocation context.

Link copied to clipboard

The same EventActions instance as actions, under the property name used by callbacks.

Link copied to clipboard

The author stamped on events this node emits.

Link copied to clipboard
override val eventId: String?

The unique ID of the event that triggered this tool call.

Link copied to clipboard
override val functionCallId: String?

The unique ID of the function call.

Link copied to clipboard

Whether an output has been set, which distinguishes "no output" from "the output was null".

Link copied to clipboard

The ids of the input requests this activation raised and is now waiting on, which the graph pauses on until an answer arrives keyed by that id.

Link copied to clipboard
Link copied to clipboard
override val invocationId: String

The unique ID of this invocation.

Link copied to clipboard

The MemoryService instance.

Link copied to clipboard

The node this context is an activation of.

Link copied to clipboard

This activation's path: name@runId segments joined by /, rooted at the outermost node.

Link copied to clipboard

The node's result. Settable once per activation, whether by emitting it or by assigning it.

Link copied to clipboard

The context of the node that scheduled this one, or null at the root or off-graph.

Link copied to clipboard

Answers to this node's interrupts, keyed by interrupt id. Empty off-graph.

Link copied to clipboard

The routes this node selected, read by the scheduler to pick the outgoing edges.

Link copied to clipboard
override val runConfig: RunConfig?

The run configuration for this invocation.

Link copied to clipboard

This activation's id within its node, counting from "1". It is a string because it forms the name@runId segment of a node path. "1" off-graph.

Link copied to clipboard
override val session: Session

The session that this invocation is a part of.

Link copied to clipboard
override val state: Map<String, Any>

The delta-aware state of the current session: committed session state merged with pending actions stateDelta writes and this activation's transient writes, with removed keys filtered out.

Link copied to clipboard

The tool confirmation of the current tool call.

Link copied to clipboard
override val userContent: Content?

The user content that this invocation is processing.

Link copied to clipboard
override val userId: String

The user ID of the user that initiated the session.

Functions

Link copied to clipboard
suspend fun addEventsToMemory(events: List<Event>, customMetadata: Map<String, Any?>? = null)

Adds an explicit list of events to the memory service, scoped to the current session's app/user/session ids.

Link copied to clipboard
suspend fun addMemory(memories: List<MemoryEntry>, customMetadata: Map<String, Any?>? = null)

Adds explicit memories directly to the memory service, scoped to the current session's app/user ids.

Link copied to clipboard
suspend fun addSessionToMemory()

Triggers memory generation for the current session.

Link copied to clipboard

Requests the current LLM agent to stop after the current step completes.

Link copied to clipboard
suspend override fun getEvents(currentInvocation: Boolean = false, currentBranch: Boolean = false): List<Event>

Returns the events from the current session.

Link copied to clipboard
suspend override fun listArtifacts(): List<String>

Lists the filenames of the artifacts attached to the current session. Returns an empty list if no artifact service is configured.

Link copied to clipboard
suspend override fun loadArtifact(name: String, version: Int? = null): Part?

Loads an artifact attached to the current session by name. Returns null if no artifact service is configured or the artifact is not found.

Link copied to clipboard

Merges the given event actions into the current event actions, replacing actions with the merged result, after checking their state delta against the state schema in force here. Any reference to actions taken before the merge will not receive subsequent writes.

Link copied to clipboard
fun requestConfirmation(hint: String? = null, payload: Any? = null)

Requests confirmation for the current tool call. Only a tool call has a functionCallId, so this can only be called in a tool context.

Link copied to clipboard
suspend fun saveArtifact(name: String, artifact: Part): Int

Saves artifact under name in the current session, records the new version in actions' artifactDelta, and returns the saved version number.

Link copied to clipboard
fun updateState(key: String, value: Any)

Records a state change so it shows up in state and, on a callback or tool context, flushes on the next event as a delta.