Skip to Content
WorkflowsQuery Plans

Query Plans

Compose AI agent orchestration plans with reusable templates and branch-based workflows.

What are Query Plans?

Query Plans are specialized workflows designed for AI agent orchestration. Unlike general DAG workflows, query plans focus on:

  • Agent coordination - Orchestrate multiple AI agents with different roles
  • Reusable templates - Save and share common patterns as prefabs
  • Branch-based execution - Track execution branches and merge points
  • Canvas composition - Visual editor optimized for agent flows

Query plans are built on the same DAG execution engine as workflows but provide higher-level abstractions for multi-agent scenarios.

Query Plan builder showing plan configuration with node arrangement

Key Concepts

Agents vs Nodes

In query plans, nodes represent AI agents rather than low-level operations:

  • Planner Agent - Breaks down complex queries into subtasks
  • Executor Agent - Performs specific operations (search, extract, transform)
  • Reviewer Agent - Validates outputs and provides feedback
  • Coordinator Agent - Merges results from parallel agents

Each agent node encapsulates prompts, tools, and validation logic.

Prefabs

Prefabs are reusable query plan templates stored in the prefab library. Common prefabs include:

PrefabPurposePattern
Multi-Step RAGRetrieval with reranking and synthesisRetrieve → Rerank → Generate → Validate
Parallel AnalysisRun multiple analyzers concurrentlySplit → [Analyzer1, Analyzer2, …] → Merge
Approval LoopHuman-in-the-loop validationProcess → Review → Approve/Retry
Iterative RefinementImprove outputs through feedbackGenerate → Evaluate → Refine → Check

Branch Tracking

Query plans track execution branches for:

  • Parallel agent execution - Multiple agents working on subtasks
  • Conditional routing - Different paths based on intermediate results
  • Merge points - Combining outputs from parallel branches
  • Branch metadata - Timestamps, costs, token usage per branch

Creating Query Plans

  1. Click BUILD in the sidebar
  2. Select Query Plans from the menu
  3. Click New Query Plan or browse existing plans

Choose a Starting Point

Option 1: Start from Scratch

  • Blank canvas with no nodes
  • Build custom agent flow from first principles

Option 2: Use a Prefab

  • Click Prefabs in the toolbar
  • Browse the prefab library
  • Select a template matching your use case
  • Customize agents and connections

Add Agent Nodes

Click nodes from the palette:

  • Planner - Decomposes queries into steps
  • Executor - Runs specific operations
  • Reviewer - Validates and scores outputs
  • Merger - Combines results from multiple agents

Configure each agent’s:

  • Role - Agent identity and responsibilities
  • Prompt - System and user prompts
  • Tools - Functions the agent can call
  • Model - LLM backend (GPT-4, Claude, etc.)

Connect Agents

Drag from output ports to input ports to define:

  • Sequential dependencies - Agent B runs after Agent A completes
  • Parallel execution - Multiple agents run concurrently
  • Conditional routing - Route based on intermediate results

Configure Merge Logic

When multiple branches converge, configure merge behavior:

  • Concatenation - Combine all outputs as array
  • Voting - Select most common result
  • Weighted average - Score-based combination
  • Custom function - Python code for complex merging

Test with Queries

Create test queries to validate your plan:

  1. Click Test in the toolbar
  2. Enter a sample user query
  3. Click Run to execute the plan
  4. Inspect agent outputs and branch timeline
  5. Review total cost and token usage

Save as Prefab (Optional)

If your query plan is reusable:

  1. Click Save as Prefab
  2. Provide name and description
  3. Specify parameterizable fields (model, thresholds, etc.)
  4. Publish to prefab library

Canvas Features

Branch Visualization

The query plan canvas highlights execution branches with color coding:

  • Active branch - Currently executing agents (green)
  • Completed branch - Finished execution (blue)
  • Failed branch - Encountered error (red)
  • Pending branch - Waiting for dependencies (gray)

Agent Timeline

View chronological execution in the timeline panel:

  • Start/end timestamps per agent
  • Token usage and API cost per agent
  • Output preview for each completed agent
  • Error details for failed agents

Branch Metadata

Inspect metadata for each branch:

{ "branch_id": "branch_1", "parent_branch_id": null, "agents": ["planner_1", "executor_1", "executor_2"], "status": "completed", "total_tokens": 2456, "total_cost_usd": 0.047, "duration_ms": 3421, "merge_point": "merger_1" }

Common Patterns

Multi-Step RAG

Agent Roles:

  • Retriever - Semantic search over knowledge base
  • Reranker - Score and filter retrieved docs
  • Generator - Synthesize response using top docs
  • Validator - Check faithfulness and relevance

Parallel Analysis

Agent Roles:

  • Classifier - Categorize document type
  • NER Agent - Extract named entities
  • Sentiment Agent - Analyze tone and sentiment
  • Merger - Combine all analyses into unified output

Iterative Refinement

Agent Roles:

  • Generator - Create initial response
  • Reviewer - Evaluate quality with rubric
  • Refiner - Incorporate feedback and regenerate

Prefab Management

Creating Prefabs

  1. Build and test a query plan
  2. Click Save as Prefab
  3. Define parameters:
    • Name - Descriptive identifier
    • Category - RAG, Analysis, Processing, etc.
    • Description - Use case and expected inputs
    • Parameters - Configurable fields (model, threshold, etc.)
  4. Click Publish

Using Prefabs

  1. Navigate to Query PlansPrefabs
  2. Browse by category or search
  3. Click a prefab to preview
  4. Click Use Template to instantiate
  5. Customize parameters for your use case

Sharing Prefabs

Prefabs are stored in the M3 Forge database and available to all workspace users. To share across workspaces:

  1. Export prefab as JSON
  2. Send to other workspace admin
  3. Import via PrefabsImport

Prefabs are versioned. Updates create new versions while preserving existing instantiations.

Execution and Monitoring

Running Query Plans

Query plans can be executed:

  • Manually - Click Run in the editor
  • Via API - tRPC endpoint queryPlans.execute
  • Via triggers - Schedule or event-based (see Triggers)

Monitoring Runs

Navigate to OPERATERuns and filter by type: query_plan:

  • Status - Pending, Running, Completed, Failed
  • Duration - Total execution time
  • Cost - Total API cost across all agents
  • Branch count - Number of execution branches

Click a run to view detailed timeline and agent outputs.

Debugging

Use the timeline view to identify issues:

  • Slow agents - High duration agents causing bottlenecks
  • Failed branches - Agents with errors or timeouts
  • Cost spikes - Agents consuming excessive tokens
  • Validation failures - Agents producing low-quality outputs

Best Practices

  1. Design for reusability - Create prefabs for common patterns
  2. Limit parallel branches - Too many parallel agents increase cost and complexity
  3. Add validation gates - Use reviewer agents to catch quality issues early
  4. Monitor costs - Set token budgets and timeouts for expensive agents
  5. Test with edge cases - Validate behavior on unusual or malformed queries
  6. Document agent roles - Add descriptions explaining each agent’s responsibility

Query plans can spawn many parallel LLM calls. Always set token limits and timeouts to prevent runaway costs.

Example: Customer Support Query Plan

A complete query plan for handling customer support inquiries:

Classify Intent

  • Agent: Intent Classifier
  • Model: GPT-3.5
  • Output: intent (billing, technical, feedback)

Route to Specialist

  • Agent: Router
  • Logic: Route based on intent classification
  • Branches: billing_agent, technical_agent, feedback_agent

Process Query

  • Agents: Domain-specific specialists (3 parallel)
  • Tools: Knowledge base search, ticket lookup, user history
  • Output: Proposed resolution

Review Response

  • Agent: Quality Reviewer
  • Metrics: Completeness, empathy, accuracy
  • Pass threshold: 0.8

Human Approval (if needed)

  • Trigger: Review score < 0.8
  • Node: HITL Approval
  • Action: Send to support manager

Send Response

  • Agent: Response Formatter
  • Output: Final customer-facing message
Last updated on