adk-python icon indicating copy to clipboard operation
adk-python copied to clipboard

Let Tool Agents Automatically Access Shared State

Open mohankumar-mg opened this issue 1 year ago • 6 comments

Is your feature request related to a problem? Please describe. I'm building a multi-agent environment using Google ADK where a root agent sequentially invokes specialized sub-agents (e.g., a math-solving agent and a haiku-generating agent). These sub-agents are defined as tools within the root agent. While the math agent writes its output (e.g., solution) to the global state, the haiku agent cannot read this shared state automatically unless I explicitly pass the value through input parameters. This leads to redundant boilerplate logic and breaks the modular abstraction where agents should be able to operate autonomously based on global state keys.

Describe the solution you'd like I'd like Google ADK agents—even when invoked as tools inside another agent—to be able to automatically read values from the shared ToolContext.state, without requiring manual propagation of state via the input_dict or template logic. This would allow more elegant and maintainable code where agents can assume responsibility for their required state keys without tight coupling to the orchestration logic.

Describe alternatives you've considered

  1. Using a SequentialAgent to pass values via {} string templating — but this becomes cumbersome in complex flows or deeply nested agent compositions.

  2. Manually passing the required state values from the orchestrator or root agent — which breaks encapsulation and increases boilerplate.

  3. Accessing ToolContext.state directly within each agent’s code — which works, but still requires custom logic and reduces composability.

Additional context The current behavior is inconsistent: agents can write to the global state when used as tools, but cannot read from it automatically. Aligning both read and write access would improve clarity and consistency. Use case example:

python

math_agent writes to state

state["math_solution"] = "x = 4"

haiku_agent expects math_solution from state, but fails unless passed manually

This feature would empower developers to build more loosely coupled, declarative, and robust multi-agent systems with better developer ergonomics.

mohankumar-mg avatar Jul 28 '25 10:07 mohankumar-mg

@mokhankumar-mg, I don't fully understand the issue, and I'm not a maintainer, but you can access the state in the system prompt by using {state_variable_name}. This allows sub-agents to access the session's state.

Alternatively, you can use tools with tool context to access the state.

I hope this answers your question. If not, could you please explain the issue in more detail?

ac-machache avatar Jul 28 '25 12:07 ac-machache

Thank you for the reply!

Let me clarify my request:

I'm aware that we can use {state_variable} in system prompts using templating (like in SequentialAgent), and that we can manually access context.state["key"] inside a tool. However, my feature request is about improving developer ergonomics when using agents as tools.

Currently, sub-agents used as tools cannot automatically read from the global session state unless:

  1. I explicitly pass each required input using {} templating, or

  2. I write imperative code inside the agent to pull from ToolContext.state.

    This leads to tight coupling and boilerplate code. I'm requesting a declarative mechanism where:

If a sub-agent needs input like math_solution, and that value already exists in the session state, it should automatically be passed in during agent invocation (without extra templating logic or manual input dict construction).

For example:

math_agent sets state["math_solution"]

haiku_agent (used as a tool) should receive this as input automatically if it has a prompt like:

system_prompt="Write a haiku for the solution: {math_solution}"

Right now, this doesn’t work unless I manually pass "math_solution" as part of the tool input.

I believe enabling this kind of automatic state injection for agents-used-as-tools would greatly improve composability and reduce boilerplate.

Please let me know if I should provide a minimal reproducible example. I'd be happy to.

mohankumar-mg avatar Jul 29 '25 08:07 mohankumar-mg

This should be a good add. It's also in our backlog.

Jacksunwei avatar Aug 06 '25 18:08 Jacksunwei

Thanks. Waiting for the update👍

mohankumar-mg avatar Sep 27 '25 14:09 mohankumar-mg

@mohankumar-mg Thanks for reporting. We are currently reviewing your feature request,Its under review Thanks for understanding.

klateefa avatar Nov 21 '25 05:11 klateefa

Thanks for the feature request!

While ADK does propagate the session state to sub-agents (both inline and nested ones run via AgentTool inherit and sync back state deltas), ADK intentionally does not automatically inject the entire state dictionary into the LLM prompts of sub-agents. Doing so would lead to prompt bloat and unnecessary token costs.

To make a sub-agent aware of shared state, you should explicitly declare the dependency:

  1. Using Template Logic (Declarative): You can reference state keys directly in the sub-agent's instruction string using brackets, e.g., instruction=""Write a haiku about {math_solution}"". ADK will automatically resolve this from the shared state at runtime.
  2. Using Input Parameters (Modular): Declare inputs on the sub-agent's input_schema and let the parent orchestrator (or parent LLM) map and pass the values when calling the tool.
  3. Using Custom Code: If you are implementing a custom agent, you can read from the shared state directly using ctx.state[""key""].

Since the state is already shared and propagation is possible via these explicit mechanisms, we are closing this issue to maintain the design principle of controlled context propagation.

DeanChensj avatar Jul 20 '26 22:07 DeanChensj