返回 Skill 列表
extension
分类: 开发与工程无需 API Key

parallelization

一种并发模式,其中多个代理任务同时执行以加快处理速度或收集不同的观点。当用户要求“并行运行代理”、“并行化任务”、“并发执行”,或提到并行处理、扇出或批量执行时使用。

person作者: jakexiaohubgithub

Parallelization

Parallelization allows an agentic system to perform multiple independent operations simultaneously. This is commonly used in distinct flavors: "Sectioning" (breaking a large task into independent chunks to process in parallel) and "Voting" (running the same task multiple times to get diverse outputs for consensus or increasing quality).

When to Use

  • Speed: When tasks are independent and can be run concurrently to reduce total latency (e.g., verifying 5 different facts).
  • Diversity: When you want multiple creative options (e.g., generate 5 different headlines).
  • Reliability: When used in a "majority vote" pattern to reduce hallucinations (Self-Consistency).
  • Aggregating Information: Researching a topic from multiple sources simultaneously.

Use Cases

  • Batch Processing: Grading 100 student essays concurrently.
  • Multi-Perspective Analysis: Asking a "Skeptic Agent", an "Optimist Agent", and a "Realist Agent" to review a plan simultaneously.
  • Map-Reduce: Identifying key themes in 50 documents by summarizing them all in parallel (Map) and then synthesizing the summaries (Reduce).

Implementation Pattern

import asyncio

async def parallel_workflow(topic):
    # Define independent tasks
    tasks = [
        research_agent.run(f"Research history of {topic}"),
        research_agent.run(f"Research economic impact of {topic}"),
        research_agent.run(f"Research cultural significance of {topic}")
    ]
    
    # Execute all concurrently
    # This takes as long as the slowest single task, not the sum of all tasks.
    results = await asyncio.gather(*tasks)
    
    # Synthesize results
    final_report = synthesize_agent.run(
        prompt="Combine these research findings into a report...",
        input=results
    )
    
    return final_report

Troubleshooting

| Problem | Cause | Fix | |---|---|---| | Race conditions | Shared mutable state | Use immutable message passing; collect results only in the aggregator | | One slow task blocks completion | No partial results | Set a per-task timeout; return partial results after timeout | | Rate limits hit with parallelization | Too many concurrent API calls | Add a semaphore (e.g., asyncio.Semaphore(10)) to cap concurrency | | Results aggregated in wrong order | Non-deterministic completion order | Tag each result with its task ID; sort before aggregating |