# Class InvocationContext

java.lang.Object
com.google.adk.agents.InvocationContext

public class InvocationContext extends Object
The context for an agent invocation.

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static class`
  `InvocationContext.Builder`
  Builder for `InvocationContext`.

- ## Constructor Summary

  Constructors
  Modifier
  Constructor
  Description
  `protected`
  `InvocationContext(InvocationContext.Builder builder)`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `Map<String, ActiveStreamingTool>`
  `activeStreamingTools()`
  Returns a map of tool call IDs to active streaming tools for the current invocation.
  `BaseAgent`
  `agent()`
  Returns the agent being invoked.
  `String`
  `appName()`
  Returns the application name associated with the session.
  `BaseArtifactService`
  `artifactService()`
  Returns the artifact service for persisting artifacts.
  `Optional<String>`
  `branch()`
  Returns the branch ID for the current invocation, if one is set.
  `void`
  `branch(@Nullable String branch)`
  Sets the [branch] ID for the current invocation.
  `static InvocationContext.Builder`
  `builder()`
  Returns a new `InvocationContext.Builder` for creating `InvocationContext` instances.
  `Map<String,Object>`
  `callbackContextData()`
  Returns a map for storing temporary context data that can be shared between different parts of the invocation (e.g., before/on/after model callbacks).
  `Optional<ContextCacheConfig>`
  `contextCacheConfig()`
  Returns the context cache configuration for the current agent run.
  `boolean`
  `endInvocation()`
  Returns whether this invocation should be ended, e.g., due to reaching a terminal state or error.
  `boolean`
  `equals(Object o)`
  `Optional<EventsCompactionConfig>`
  `eventsCompactionConfig()`
  Returns the events compaction configuration for the current agent run.
  `int`
  `hashCode()`
  `void`
  `incrementLlmCallsCount()`
  Increments the count of LLM calls made during this invocation and throws an exception if the limit defined in `RunConfig` is exceeded.
  `String`
  `invocationId()`
  Returns the unique ID for this invocation.
  `Optional<LiveRequestQueue>`
  `liveRequestQueue()`
  Returns the queue for managing live requests, if available for this invocation.
  `BaseMemoryService`
  `memoryService()`
  Returns the memory service for accessing agent memory.
  `static String`
  `newInvocationContextId()`
  Generates a new unique ID for an invocation context.
  `Plugin`
  `pluginManager()`
  Returns the plugin manager for accessing tools and plugins.
  `RunConfig`
  `runConfig()`
  Returns the configuration for the current agent run.
  `Session`
  `session()`
  Returns the session associated with this invocation.
  `BaseSessionService`
  `sessionService()`
  Returns the session service for managing session state.
  `void`
  `setEndInvocation(boolean endInvocation)`
  Sets whether this invocation should be ended.
  `InvocationContext.Builder`
  `toBuilder()`
  Returns a `InvocationContext.Builder` initialized with the values of this instance.
  `Optional<com.google.genai.types.Content>`
  `userContent()`
  Returns the user content that triggered this invocation, if any.
  `String`
  `userId()`
  Returns the user ID associated with the session.

  ### Methods inherited from class Object

  `clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### InvocationContext

    protected InvocationContext(InvocationContext.Builder builder)

- ## Method Details

  - ### builder

    public static InvocationContext.Builder builder()
    Returns a new `InvocationContext.Builder` for creating `InvocationContext` instances.

  - ### toBuilder

    public InvocationContext.Builder toBuilder()
    Returns a `InvocationContext.Builder` initialized with the values of this instance.

  - ### sessionService

    public BaseSessionService sessionService()
    Returns the session service for managing session state.

  - ### artifactService

    public BaseArtifactService artifactService()
    Returns the artifact service for persisting artifacts.

  - ### memoryService

    public BaseMemoryService memoryService()
    Returns the memory service for accessing agent memory.

  - ### pluginManager

    public Plugin pluginManager()
    Returns the plugin manager for accessing tools and plugins.

  - ### activeStreamingTools

    public Map<String, ActiveStreamingTool> activeStreamingTools()
    Returns a map of tool call IDs to active streaming tools for the current invocation.

  - ### liveRequestQueue

    public Optional<LiveRequestQueue> liveRequestQueue()
    Returns the queue for managing live requests, if available for this invocation.

  - ### invocationId

    public String invocationId()
    Returns the unique ID for this invocation.

  - ### branch

    public void branch(@Nullable String branch)
    Sets the [branch] ID for the current invocation. A branch represents a fork in the conversation history.

  - ### branch

    public Optional<String> branch()
    Returns the branch ID for the current invocation, if one is set. A branch represents a fork in the conversation history.

  - ### agent

    public BaseAgent agent()
    Returns the agent being invoked.

  - ### session

    public Session session()
    Returns the session associated with this invocation.

  - ### userContent

    public Optional<com.google.genai.types.Content> userContent()
    Returns the user content that triggered this invocation, if any.

  - ### runConfig

    public RunConfig runConfig()
    Returns the configuration for the current agent run.

  - ### callbackContextData

    public Map<String,Object> callbackContextData()
    Returns a map for storing temporary context data that can be shared between different parts of the invocation (e.g., before/on/after model callbacks).

  - ### endInvocation

    public boolean endInvocation()
    Returns whether this invocation should be ended, e.g., due to reaching a terminal state or error.

  - ### setEndInvocation

    public void setEndInvocation(boolean endInvocation)
    Sets whether this invocation should be ended.

  - ### appName

    public String appName()
    Returns the application name associated with the session.

  - ### userId

    public String userId()
    Returns the user ID associated with the session.

  - ### newInvocationContextId

    public static String newInvocationContextId()
    Generates a new unique ID for an invocation context.

  - ### incrementLlmCallsCount

    public void incrementLlmCallsCount() throws LlmCallsLimitExceededException
    Increments the count of LLM calls made during this invocation and throws an exception if the limit defined in `RunConfig` is exceeded.
    Throws:
      `LlmCallsLimitExceededException` - if the call limit is exceeded

  - ### eventsCompactionConfig

    public Optional<EventsCompactionConfig> eventsCompactionConfig()
    Returns the events compaction configuration for the current agent run.

  - ### contextCacheConfig

    public Optional<ContextCacheConfig> contextCacheConfig()
    Returns the context cache configuration for the current agent run.

  - ### equals

    public boolean equals(Object o)
    Overrides:
      `equals` in class `Object`

  - ### hashCode

    public int hashCode()
    Overrides:
      `hashCode` in class `Object`