Agent Invocation Context

Purpose

The Agent Invocation Context subtopic addresses the need to encapsulate all relevant contextual information and state management during an agent's invocation lifecycle within the broader AI Agent Framework. This context acts as a comprehensive environment that governs how an agent processes an invocation—from the initial user input, through multiple agent calls and steps, to the final response or transfer to another agent.

This subtopic specifically solves the challenge of managing complex, nested, and possibly concurrent agent executions by providing interfaces and implementations that unify session data, memory, artifacts, invocation metadata, and lifecycle control. It also enables consistent access to immutable and mutable state, and supports callback integration during agent execution.

By defining these context interfaces and concrete implementations, the system ensures agents operate with a consistent view of the current session, memory, and artifacts, while also enabling lifecycle controls such as early termination or branching for parallel agents. This complements other subtopics like Agent Lifecycle and Callbacks by providing the contextual backbone they rely upon.

Functionality

The core functionality revolves around the InvocationContext interface, which models the lifecycle and data scope of a single agent invocation. Key aspects include:

Code Snippet Illustration

Creating a new invocation context involves initializing it with session, memory, artifacts, and agent references:

params := InvocationContextParams{
    Artifacts: artifactService,
    Memory:    memoryService,
    Session:   sessionInstance,
    Branch:    "agent_1.agent_2",
    Agent:     currentAgent,
    UserContent: userContent,
    RunConfig: runConfig,
}
invocationCtx := context.NewInvocationContext(baseCtx, params)

Within the invocation, the context can be queried or updated as follows:

if !invocationCtx.Ended() {
    // Access session and memory
    userID := invocationCtx.Session().UserID()
    mem := invocationCtx.Memory()

    // End the invocation early if needed
    invocationCtx.EndInvocation()
}

The callback context wraps the invocation context to provide mutable state and artifact save tracking:

cbCtx := context.NewCallbackContext(invocationCtx)
cbCtx.State().Set("key", "value")
cbCtx.Artifacts().Save(ctx, "file.txt", contentPart)

Integration

The Agent Invocation Context is tightly integrated with multiple components in the agent ecosystem:

This subtopic thus forms the foundational context layer that enables consistent, stateful, and manageable agent invocations, supporting complex agent orchestration and tooling integrations.

Diagram

classDiagram
class InvocationContext {
+Agent() Agent
+Session() Session
+Memory() Memory
+Artifacts() Artifacts
+UserContent() Content
+RunConfig() RunConfig
+InvocationID() string
+Branch() string
+EndInvocation()
+Ended() bool
}
class ReadonlyContext {
+AgentName() string
+SessionID() string
+UserID() string
+AppName() string
+InvocationID() string
+Branch() string
+UserContent() Content
+ReadonlyState() ReadonlyState
}
class CallbackContext {
+State() State
+Artifacts() Artifacts
}
InvocationContext <|-- ReadonlyContext
InvocationContext <|-- CallbackContext
InvocationContext "1" --> "1" Agent
InvocationContext "1" --> "1" Session
InvocationContext "1" --> "1" Memory
InvocationContext "1" --> "1" Artifacts
InvocationContext "1" --> "1" RunConfig

This class diagram illustrates the relationship between the main context interfaces (InvocationContext, ReadonlyContext, and CallbackContext) and their key data accessors and lifecycle methods, capturing how invocation state is exposed and controlled during an agent's execution.