TemplatesAgent Patterns

Multi-Step Tool Pattern

Loading pattern preview

Problems solved

Technical challenges this implementation handles out of the box.
  • Coordinate multi-step tool calling inside an Experimental_Agent loop
  • Chain dependent tool results across several agent steps
  • Stream progressive tool activity to the chat UI
  • Stop runaway loops with step-count controls

Use cases

Products and workflows this pattern is designed to support.
  • Research agents that gather then synthesize
  • Ops assistants that complete multi-step checklists
  • Data enrichment chats with sequential tools
  • Teaching multi-step tool patterns with AI SDK agents

Setup

Requirements, wiring steps, and what this pattern adds to your project.

Getting started

Pick how you want to pull this pattern in. Then wire env vars and routes the same way.

  1. 1

    Download → Next.js app

    Use Download → Next.js app in the preview toolbar. You get a zip scaffold (Pro downloads rewrite preview `/view/.../api/*` paths to `/api/*` automatically).

  2. 2

    Extract the zip and install dependencies

    Unzip the archive, then run `pnpm install` (or npm / yarn) in the project folder.

  3. 3

    Fill .env.local

    The download includes .env.local placeholders when needed. Add real values for: OPENAI_API_KEY, PERPLEXITY_API_KEY.

  4. 4

    Customize the agent and tool files

    Adapt prompts, tools, and stop conditions in the generated project for your product.

  5. 5

    Run the app and open the pattern route

    Run `pnpm dev`, open the setup/checklist page if present, then navigate to the generated pattern route and verify responses.

Environment variables

Capabilities

AI SDK APIs

AgenttoolstepCountIsgenerateObjectExperimental_AgentgatewayhasToolCall

Providers

OpenAIPerplexity

External services

Hacker News

Files added

18 files
  • app/page.tsx
  • app/layout.tsx
  • app/api/multi-step-tool-agent/route.ts
  • lib/multi-step-agent.ts
  • lib/multi-step-tool-types.ts
  • lib/ai-agent-multi-step-tools/web-search.ts
  • lib/ai-agent-multi-step-tools/news-search.ts
  • lib/ai-agent-multi-step-tools/analyze.ts
  • lib/ai-agent-multi-step-tools/decide.ts
  • lib/ai-agent-multi-step-tools/provide-answer.ts
  • lib/ai-agent-multi-step-tools/schema.ts
  • components/multi-step-tool-chat-demo.tsx
  • components/tool-views/web-search-view.tsx
  • components/tool-views/news-search-view.tsx
  • components/tool-views/analyze-view.tsx
  • components/tool-views/decide-view.tsx
  • components/tool-views/provide-answer-view.tsx
  • README.md

Dependencies

7 total

npm packages

6
AISDKlucide-reactNext.jsReactsonnerZod

Shadcn IconRegistry components

1
empty

Critical files

  • multi-step-tool-agent/route.ts

    API route wiring the agent to HTTP: uses createAgentUIStreamResponse and validateUIMessages to connect the multi-step agent to a streaming endpoint.

  • lib/multi-step-agent.ts

    Core agent definition: creates an Experimental_Agent with comprehensive system instructions, 5 registered tools, and stop conditions (hasToolCall or stepCountIs(8)).

  • ai-agent-multi-step-tools/web-search.ts

    Web search tool using Perplexity Sonar: uses generateObject with perplexity/sonar model. Implements generator-based execute with loading/ready states.

  • ai-agent-multi-step-tools/news-search.ts

    News search tool using HN Algolia API. Generator-based tool with loading/ready states and graceful error fallback.

  • ai-agent-multi-step-tools/analyze.ts

    Analysis tool: breaks down complex problems into components using systematic/creative/technical approaches. Generator-based with Zod schema validation.

  • ai-agent-multi-step-tools/decide.ts

    Decision tool with scoring logic: evaluates options against criteria using keyword matching and heuristic scoring. Shows a tool with non-trivial local computation.

  • ai-agent-multi-step-tools/provide-answer.ts

    Terminal answer tool that synthesizes all previous tool results into a final structured answer with inline citations and confidence score. Triggers agent termination via stopWhen condition.

  • ai-agent-multi-step-tools/schema.ts

    Shared Zod schemas for web search tools: WebSearchItemSchema and WebSearchSchema for Perplexity Sonar responses.

Requirements

Complexity
Intermediate
Setup time
~10 minutes
Node
24+
Memory
1GB