* Deleted redundant files. * Deleted redundant files. * Deleted redundant files. * Deleted redundant files. * Deleted redundant files. * Deleted redundant files. * Delete .gitignore Deleted redundant files. * Delete .gitignore Deleted redundant files. * Delete .gitignore * Delete .gitignore * Delete .gitignore * Revert changes to .gitignore * Revert changes to .gitignore * Revert changes to .gitignore * Revert changes to .gitignore * Revert changes to .gitignore * Revert changes to .gitignore * Add MCP Tools TS example * Removed unnecessary changes * Removed extra slashes * Added the custom agent sample. * Corrected an error in the package.json file. * Updated custom-agent.md * Made small fixes. * Add Session TS examples * Added the typescript code snippets for multi agent. * added the code sample for loop agent * Added the code sample for loop agent * Added the code sample for parallel agent * Fixed a few issues and bugs * Added the code sample for sequential agent * Renamed run back to runAsync * Updated the capital agent code sample. * Corrected the snippet * Renamed run() to runAsync() * Add Runtime TS examples * Fixed the snippets after library updates. * Fixed the sample code after library changes. * Added final touchups and fixes. * Updated the code based on recent library changes. * Updated package.json * Updated package.json * Updated package.json * Updated the code with runAsync and package.json * Updated the typescript sample. * Updated after_tool_callback.ts with runAsync * Updated after_model_callback.ts * Updated before_model_callback.ts * Updated before_tool_callback.ts * Created before_agent_callback.ts * Update before_agent_callback.ts Changed the condition * Update before_model_callback.ts Changed the condition * Update after_model_callback.ts Changed the condition * Update after_tool_callback.ts Changed the condition * Update after_agent_callback.ts Changed the condition * Update callback_basic.ts Changed the condition * Update storyflow_agent.ts Changed the condition * Update sequential_agent_code_development_agent.ts Changed the condition * Updated before_agent_callback.ts * Removed unnecessary comment. * Updated comment * Renamed run to runAsync * Added artifact code snippets. * Fixed .link-checker-config.json * Added tsconfig.json * Update .gitignore with node modules files * Added 7 typescript code snippets * Cleaned up and corrected the snippets for plugins * Update index.md Undid the changes in artifacts/index.md * Escaped properly. * Add Long Running Function Tool TS examples * Resolved code review feedback * Fixed the code sample per git comments. * Fixed the code per git comments. * Fixed the md file per git comments. * Fixed the issues per git comments. * Fixed issues per github comments. * Fixed the issues per PR comments. * Fixed issues per PR comments. * Added a code sample for HITL * Fixed the indentations in the typescript code * Used createPartFromBase64 when appropriate. * Switched to use createUserContent. * docs: Add JavaScript Quickstart, short unified format * Added a simulated user delay to the code sample. * Made some cosmetic improvements * Added the code snippet with HITL with policy * Made cosmetic modifications to multi-agents.md * Added more tutorial for the HITL with policy * Modularized the main function and added helper functions. * Resolved code review feedback * Resolved code review feedback * Resolved code review feedback * Fixed minor things in the typescript code per PR comments. * Fixed the import for Event and used createEventActions. * Simplified functionResponse code. * Corrected the Event import and used createEventActions. * Updated ADK versions in package.json. * Final review of all changes & Add Safety TS example * Make tsconfig.json files consistent * Generated the API Reference for typescript * Resolved code review feedback * feat: Add Typescript API reference to docs This commit adds the generated TypeDoc documentation for the Typescript ADK to the documentation site. It also updates the main API reference page to include a link to the new Typescript API reference. * docs: update TypeScript quickstart * docs: Add version tags for Build Agents topics - add version tags - move TS content to second position (after Python) * docs: Add MCP Toolbox for databases TS SDK Documentation * Update mcp-toolbox-for-databases.md * use typescript code block * minor changes * Update mcp-toolbox-for-databases.md * Auto generated the typescript API reference. * Added the TS code snippets for context. * Fixed TS code snippets for context. * Modified the sample based on the comment. * docs: adding TypeScript language tags (2 of 3) - Update all Run Agents section topics - change all TS tags to v0.2.0 per eng agreement * docs: Add TypeScript language support tags, Components (3 of 3) - add TS language support tags - re-order TS examples to be second - minor fixes: remove trailing spaces * Added callback samples for typescript to the markdown files. * Updated adk version in quickstart * Updated google/adk version for typescript. --------- Co-authored-by: manchan <manchan@google.com> Co-authored-by: Mandy Chan <75594070+slothwriter@users.noreply.github.com> Co-authored-by: Joe Fernandez <joefernandez@google.com> Co-authored-by: Joe Fernandez <joefernandez@users.noreply.github.com> Co-authored-by: Twisha Bansal <58483338+twishabansal@users.noreply.github.com>
4.3 KiB
Parallel agents
The ParallelAgent is a workflow agent that executes its sub-agents concurrently. This dramatically speeds up workflows where tasks can be performed independently.
Use ParallelAgent when: For scenarios prioritizing speed and involving independent, resource-intensive tasks, a ParallelAgent facilitates efficient parallel execution. When sub-agents operate without dependencies, their tasks can be performed concurrently, significantly reducing overall processing time.
As with other workflow agents, the ParallelAgent is not powered by an LLM, and is thus deterministic in how it executes. That being said, workflow agents are only concerned with their execution (i.e. executing sub-agents in parallel), and not their internal logic; the tools or sub-agents of a workflow agent may or may not utilize LLMs.
Example
This approach is particularly beneficial for operations like multi-source data retrieval or heavy computations, where parallelization yields substantial performance gains. Importantly, this strategy assumes no inherent need for shared state or direct information exchange between the concurrently executing agents.
How it works
When the ParallelAgent's run_async() method is called:
- Concurrent Execution: It initiates the
run_async()method of each sub-agent present in thesub_agentslist concurrently. This means all the agents start running at (approximately) the same time. - Independent Branches: Each sub-agent operates in its own execution branch. There is no automatic sharing of conversation history or state between these branches during execution.
- Result Collection: The
ParallelAgentmanages the parallel execution and, typically, provides a way to access the results from each sub-agent after they have completed (e.g., through a list of results or events). The order of results may not be deterministic.
Independent Execution and State Management
It's crucial to understand that sub-agents within a ParallelAgent run independently. If you need communication or data sharing between these agents, you must implement it explicitly. Possible approaches include:
- Shared
InvocationContext: You could pass a sharedInvocationContextobject to each sub-agent. This object could act as a shared data store. However, you'd need to manage concurrent access to this shared context carefully (e.g., using locks) to avoid race conditions. - External State Management: Use an external database, message queue, or other mechanism to manage shared state and facilitate communication between agents.
- Post-Processing: Collect results from each branch, and then implement logic to coordinate data afterwards.
Full Example: Parallel Web Research
Imagine researching multiple topics simultaneously:
-
Researcher Agent 1: An
LlmAgentthat researches "renewable energy sources." -
Researcher Agent 2: An
LlmAgentthat researches "electric vehicle technology." -
Researcher Agent 3: An
LlmAgentthat researches "carbon capture methods."ParallelAgent(sub_agents=[ResearcherAgent1, ResearcherAgent2, ResearcherAgent3])
These research tasks are independent. Using a ParallelAgent allows them to run concurrently, potentially reducing the total research time significantly compared to running them sequentially. The results from each agent would be collected separately after they finish.
???+ "Full Code"
=== "Python"
```py
--8<-- "examples/python/snippets/agents/workflow-agents/parallel_agent_web_research.py:init"
```
=== "Typescript"
```typescript
--8<-- "examples/typescript/snippets/agents/workflow-agents/parallel_agent_web_research.ts:init"
```
=== "Go"
```go
--8<-- "examples/go/snippets/agents/workflow-agents/parallel/main.go:init"
```
=== "Java"
```java
--8<-- "examples/java/snippets/src/main/java/agents/workflow/ParallelResearchPipeline.java:full_code"
```
