|
| 1 | +# LlmAgent Single-Turn Mode |
| 2 | + |
| 3 | +This guide explains the behavior of `LlmAgent` in `single_turn` mode, both when |
| 4 | +executed as a workflow node and when defined as a sub-agent in a multi-agent |
| 5 | +hierarchy. It covers default stateless execution, delegation mechanics, and how |
| 6 | +to configure history visibility. |
| 7 | + |
| 8 | +-------------------------------------------------------------------------------- |
| 9 | + |
| 10 | +## Introduction |
| 11 | + |
| 12 | +In ADK, `mode="single_turn"` is designed for isolated, stateless tasks where the |
| 13 | +agent only needs to process the immediate input without accumulating or |
| 14 | +referencing prior conversation history. |
| 15 | + |
| 16 | +Depending on how the agent is deployed—either as a step in a `Workflow` or as a |
| 17 | +`sub_agent` of another LLM agent—its behavior and interaction patterns differ. |
| 18 | + |
| 19 | +-------------------------------------------------------------------------------- |
| 20 | + |
| 21 | +## 1. Single-Turn Mode as a Workflow Node |
| 22 | + |
| 23 | +When building a `Workflow` graph, any `LlmAgent` added to the graph defaults to |
| 24 | +`mode="single_turn"` (unless explicitly configured otherwise). |
| 25 | + |
| 26 | +### Behavior |
| 27 | + |
| 28 | +- **Stateless by Default**: The node does not see previous conversation turns |
| 29 | + in the workflow session. Its history visibility (`include_contents`) |
| 30 | + automatically defaults to `'none'`. |
| 31 | +- **Isolated Execution**: Each execution of the node is independent. |
| 32 | + |
| 33 | +### Example |
| 34 | + |
| 35 | +```python |
| 36 | +from google.adk.agents import LlmAgent |
| 37 | +from google.adk.workflow import Workflow, build_node |
| 38 | + |
| 39 | +# Defaults to mode="single_turn" when run as a node |
| 40 | +writer_agent = LlmAgent( |
| 41 | + name="writer", |
| 42 | + instruction="Write a short story about the input topic." |
| 43 | +) |
| 44 | + |
| 45 | +writer_node = build_node(writer_agent) |
| 46 | + |
| 47 | +wf = Workflow( |
| 48 | + name="story_generator", |
| 49 | + edges=[ |
| 50 | + ("START", writer_node), |
| 51 | + (writer_node, "END") |
| 52 | + ] |
| 53 | +) |
| 54 | +``` |
| 55 | + |
| 56 | +-------------------------------------------------------------------------------- |
| 57 | + |
| 58 | +## 2. Single-Turn Mode as a Sub-Agent |
| 59 | + |
| 60 | +You can define hierarchical agent structures by assigning agents to the |
| 61 | +`sub_agents` list of a parent `LlmAgent`. |
| 62 | + |
| 63 | +### Behavior |
| 64 | + |
| 65 | +- **Exposed as a Tool**: A `single_turn` sub-agent is **not** a transfer |
| 66 | + target. The parent agent cannot hand over control of the conversation to it. |
| 67 | + Instead, the framework automatically exposes the sub-agent to the parent as |
| 68 | + a **Tool** (function). |
| 69 | +- **Functional Delegation**: The parent agent calls the sub-agent like a |
| 70 | + function, passing arguments. The sub-agent executes, returns its output to |
| 71 | + the parent, and the parent continues the conversation. |
| 72 | +- **Isolated Sub-Branch**: When the parent calls the sub-agent tool, the |
| 73 | + framework executes the sub-agent in an isolated sub-branch (derived from the |
| 74 | + parent's branch, e.g., `parent_branch.sub_agent@run_id`). |
| 75 | +- **Stateless by Default**: Like the workflow node, a `single_turn` sub-agent |
| 76 | + defaults to `include_contents="none"` and only sees the inputs passed to it |
| 77 | + in the tool call. |
| 78 | + |
| 79 | +### Example |
| 80 | + |
| 81 | +```python |
| 82 | +from google.adk.agents import LlmAgent |
| 83 | + |
| 84 | +# Define a specialized single-turn sub-agent |
| 85 | +translator_agent = LlmAgent( |
| 86 | + name="translator", |
| 87 | + instruction="Translate the input text to Spanish.", |
| 88 | + mode="single_turn" # Must be explicit if not auto-wrapped in workflow |
| 89 | +) |
| 90 | + |
| 91 | +# Define the parent agent and assign the sub-agent |
| 92 | +bilingual_writer = LlmAgent( |
| 93 | + name="bilingual_writer", |
| 94 | + instruction="Write a poem about the topic, then use the translator tool to translate it.", |
| 95 | + sub_agents=[translator_agent] # Exposes 'translator' as a tool to bilingual_writer |
| 96 | +) |
| 97 | +``` |
| 98 | + |
| 99 | +-------------------------------------------------------------------------------- |
| 100 | + |
| 101 | +## How Context Isolation Works |
| 102 | + |
| 103 | +ADK manages history visibility using **branches** and the `include_contents` |
| 104 | +configuration: |
| 105 | + |
| 106 | +1. **Branch Hierarchy**: When a sub-agent runs, it executes in a sub-branch |
| 107 | + (e.g., `main.translator@1`). |
| 108 | + - A sub-branch is allowed to read events from its parent branch (one-way |
| 109 | + visibility). |
| 110 | + - The parent branch cannot read events from the sub-branch (protecting the |
| 111 | + parent from sub-agent internal reasoning chatter). |
| 112 | +2. **History Filtering**: |
| 113 | + - **`include_contents="none"`** (Default): The agent bypasses history |
| 114 | + loading entirely. It only sees the immediate input (the workflow node |
| 115 | + input or the tool call arguments). |
| 116 | + - **`include_contents="default"`**: The agent loads conversation history. |
| 117 | + Because of the branch hierarchy, a sub-agent with this setting can see |
| 118 | + the parent agent's conversation history leading up to the tool call. |
| 119 | + |
| 120 | +-------------------------------------------------------------------------------- |
| 121 | + |
| 122 | +## Configuration Options |
| 123 | + |
| 124 | +Parameter | Type | Default | Description |
| 125 | +:----------------- | :--------------------------------------- | :------------------------------------ | :---------- |
| 126 | +`mode` | `Literal['single_turn', 'task', 'chat']` | `'single_turn'` (when run as node) | The execution mode. `single_turn` isolates execution; `task` supports delegation; `chat` preserves full history. |
| 127 | +`include_contents` | `Literal['default', 'none']` | `'none'` (for `single_turn` if unset) | Controls history visibility. For `single_turn` mode, it defaults to `'none'` (stateless), but can be explicitly set to `'default'` to make the agent context-aware. |
| 128 | + |
| 129 | +-------------------------------------------------------------------------------- |
| 130 | + |
| 131 | +## Advanced Applications: Context-Aware Execution |
| 132 | + |
| 133 | +If you want a single-turn agent (node or sub-agent) to have access to the |
| 134 | +conversation history, you must explicitly set `include_contents="default"`. |
| 135 | + |
| 136 | +### Context-Aware Sub-Agent Example |
| 137 | + |
| 138 | +In this setup, the `verifier` sub-agent needs to see the history of the |
| 139 | +conversation to verify the parent's draft against previous user constraints: |
| 140 | + |
| 141 | +```python |
| 142 | +verifier_agent = LlmAgent( |
| 143 | + name="verifier", |
| 144 | + instruction="Verify that the draft meets all constraints discussed in the chat.", |
| 145 | + mode="single_turn", |
| 146 | + include_contents="default" # Allows the sub-agent to see the parent's conversation history |
| 147 | +) |
| 148 | + |
| 149 | +editor_agent = LlmAgent( |
| 150 | + name="editor", |
| 151 | + instruction="Discuss the draft with the user and use verifier to check constraints.", |
| 152 | + sub_agents=[verifier_agent] |
| 153 | +) |
| 154 | +``` |
| 155 | + |
| 156 | +-------------------------------------------------------------------------------- |
| 157 | + |
| 158 | +## Limitations |
| 159 | + |
| 160 | +- **Difference from Standalone Behavior**: A standalone `LlmAgent` defaults to |
| 161 | + `include_contents="default"`. When used in a workflow or as a sub-agent, it |
| 162 | + defaults to `include_contents="none"`. |
| 163 | +- **No Direct Transfer**: You cannot use `transfer_to_agent` to target a |
| 164 | + `single_turn` agent. They must be invoked via tool calls. |
0 commit comments