Skip to content

feat: parallel tool execution in agentic loop #45

Closed

Description

@pavelanni

Summary

Execute independent tool calls concurrently instead of sequentially in the agentic loop.

Motivation

DocsClaw's RunToolLoop() in pkg/tools/loop.go processes tool calls one at a time in a for loop. When the LLM requests multiple independent tools (e.g., fetch a document + check permissions + get user profile), sequential execution adds unnecessary latency.

Go's goroutines make this a natural fit. Pi-mono defaults to parallel execution with a per-tool sequential override for tools that must run exclusively.

Proposed design

Tool execution mode

type ExecutionMode string

const (
    ExecutionModeParallel   ExecutionMode = "parallel"
    ExecutionModeSequential ExecutionMode = "sequential"
)

type Tool interface {
    Name() string
    Description() string
    Parameters() map[string]any
    Execute(ctx context.Context, args map[string]any) *ToolResult
    ExecutionMode() ExecutionMode // default: parallel
}

Loop changes

// In RunToolLoop, replace sequential execution with:
// 1. Validate all tool calls (find tool, check hook) — sequential
// 2. Execute allowed tools — parallel via errgroup, respecting per-tool mode
// 3. Collect results in original order — for deterministic message assembly

Per-tool override

Most tools (webfetch, readfile, fetchdoc) are safe to run in parallel. Some (exec, writefile) may need sequential mode to avoid race conditions on shared state.

Additional improvement: AfterToolCall hook

Add AfterToolCall to the Hook interface alongside the existing BeforeToolCall. This enables post-execution logging, result transformation, and audit — useful for business compliance scenarios.

type Hook interface {
    BeforeToolCall(ctx context.Context, name string, args map[string]any) (allow bool, reason string)
    AfterToolCall(ctx context.Context, name string, args map[string]any, result *ToolResult)
}

Related

Identified from analysis of pi-mono's executeToolCallsParallel in packages/agent/src/agent-loop.ts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions