Frontend Tools

Let your agent interact with and update your application's UI.

Not available for Langchain DeepAgents yet

The code on this page comes from the showcase's frontend-tools demo, and Langchain DeepAgents doesn't have it yet. See this page for CopilotKit's Built-in Agent, LangGraph (Python), LangGraph (TypeScript).
See this in Inspector

Open Inspector on localhost. Go to Inspect, then Frontend Tools. Your tool and its schema are listed.

More detail: Inspector.

What is this?

A frontend tool is a function that you register in the browser. Your agent can call it, and the function runs in the user's browser. Because the handler runs in the browser, it has direct access to component state, browser APIs, and any third-party UI library the page already uses. That's how an agent can "reach into" the app: update React state, trigger animations, read localStorage, pop a toast, or steer the user's view.

This page covers the "agent drives the UI" shape of frontend tools. To render a component for a tool call, see Generative UI. To ask the user before a tool runs, see Human-in-the-loop.

When should I use this?

Use frontend tools when your agent needs to:

  • Read or modify React component state
  • Access browser APIs like localStorage, sessionStorage, or cookies
  • Trigger UI updates, animations, or transitions
  • Show alerts, toasts, or notifications
  • Interact with third-party frontend libraries

How it works in code

Wire CopilotKit middleware into your agent

Deep Agents compile to LangGraph graphs, and CopilotKitMiddleware is the bridge. It reads the frontend tools from the request. It adds them to the tool list that the model sees. Without the middleware, a tool that you register with useFrontendTool or useComponent never reaches the model.

agent.py
from deepagents import create_deep_agent
from copilotkit import CopilotKitMiddleware

agent = create_deep_agent(
    model="openai:gpt-6-luna",
    tools=[],  # Backend tools go here
    middleware=[CopilotKitMiddleware()],
    system_prompt=SYSTEM_PROMPT,
)

Without the middleware the run still completes, and the tool is never called, because the model was never told the tool exists. You do not write code to call a frontend tool: the middleware adds it to every model call, and the agent calls it like any other tool.

Tell the model when to call it

The forwarded tool arrives on every run, but a model with no instruction about it will answer in prose and never call it. Name the tool in system_prompt and say what it is for.

agent.py
SYSTEM_PROMPT = """
You are a data visualization assistant.

When the user asks for a chart, call `render_bar_chart` with a concise
title and a `data` array of `{label, value}` items.

Keep chat responses brief and let the chart do the talking.
"""

Register a tool in the browser

Deep Agents has no live demo on these pages yet, so the demo code below does not show for it. Here is a minimal frontend tool to start from. It says hello to the user:

page.tsx
import { z } from "zod";
import { useFrontendTool } from "@copilotkit/react-core/v2";

export function Page() {
  // ...

  useFrontendTool({
    name: "sayHello",
    description: "Say hello to the user",
    parameters: z.object({
      name: z.string().describe("The name of the user to say hello to"),
    }),
    handler: async ({ name }) => {
      alert(`Hello, ${name}!`);
      return `Said hello to ${name}!`;
    },
  });

  // ...
}

Ask the agent: Say hello to Ada. The browser shows an alert with Hello, Ada!, and the agent replies after it gets the result.

Register a frontend tool with useFrontendTool. Give it a name, a Zod schema for parameters, and a handler. The agent can then call it like any other tool and your frontend runs it in the browser.

The handler receives the parsed, type-safe parameters and can do anything the browser can: update state, call an API, touch the DOM. Its return value is sent back to the agent as the tool result so the model can reason about what happened.

Ask the agent to change the background to a blue gradient. The page background changes, and the agent gets { status: "success" } as the tool result.

Registering a list of tools

useFrontendTool registers one tool per call, so it cannot be called in a loop over a list whose length changes between renders. When the set of tools comes from state, from props, or from a backend response, use useFrontendTools instead. It takes an array and runs a single effect over it, so the array can be empty on one render and hold twenty entries on the next.

useFrontendTools(
  reports.map((report) => ({
    name: `open_${report.id}`,
    description: `Open the ${report.title} report`,
    handler: async () => navigate(`/reports/${report.id}`),
  })),
  [navigate],
);

On each render:

  • A tool that leaves the array is unregistered.
  • A tool that joins the array is registered.
  • A re-render that produces an equal list does not re-register anything.

A description built from your data stays current on its own. The second argument is for values a handler closes over, such as navigate above.

How is this guide?