PatternsSDK API

Tool Approval

Loading pattern preview

Loading preview…

Problems solved

Technical challenges this implementation handles out of the box.
  • Require user confirmation before a needsApproval tool runs
  • Demonstrate the AI SDK built-in approval mechanism for delete_file
  • Keep pending approval state visible in the chat UI

Use cases

Products and workflows this pattern is designed to support.
  • File-deletion confirmations in assistants
  • Destructive admin actions gated by the user
  • Teaching needsApproval tool flows
  • Safe tool execution demos

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

    Install from the preview toolbar

    Shadcn Icon

    Copy the install command above and run it in your project — Pro patterns include a short-lived token.

  2. 2

    Add environment variables to .env.local

    OPENAI_API_KEY

  3. 3

    Point client fetches at your API routes

    Replace preview `/view/.../api/...` paths with `/api/...` in the installed files.

  4. 4

    Customize the agent and tool files

    Adapt prompts, tools, and stop conditions for your product — Copy for AI in the toolbar helps seed that work.

  5. 5

    Run your dev server and open the pattern route

    Install dependencies if needed, then start the app and verify responses.

Environment variables

Capabilities

AI SDK APIs

streamTextconvertToModelMessagesuseChataddToolApprovalResponse

Providers

OpenAI

Files added

4 files
  • app/page.tsx
  • app/layout.tsx
  • components/chat-form.tsx
  • app/api/ai-elements-confirmation/route.ts

Dependencies

7 total

npm packages

5
AISDKlucide-reactNext.jsReactZod

Shadcn IconRegistry components

2
buttonconfirmation

Critical files

  • ai-elements-confirmation/route.ts

    API route demonstrating the needsApproval tool pattern. Defines a delete_file tool with needsApproval: true that requires user confirmation before execution. Shows the built-in AI SDK approval mechanism.

Requirements

Complexity
Beginner
Setup time
~10 minutes
Node
24+
Memory
512MB