Skip to main content
This tutorial walks through the chat-ui-example — a Next.js app that lets users chat with a Browser Use agent in real time. We will focus on the SDK integration, not the UI components. The app has two pages:
  1. Home — the user types a task, the app creates a session and sends the task.
  2. Session — the app polls for messages and lets the user send follow-ups.
All SDK calls live in a single file: src/lib/api.ts.

Setup: SDK Clients

The app uses both SDK versions — v3 for the agent session API and v2 for profiles (which are not on v3 yet).
api.ts
NEXT_PUBLIC_ exposes the key to the browser. In production, move SDK calls to server actions or API routes.

Section 1: Home Page — Creating a Session

API layer

Two functions handle session creation:
api.ts
Key details:
  • keepAlive: true keeps the session open after the task completes so the user can send follow-ups.
  • createSession creates the browser without a task — the session starts idle.
  • sendTask calls sessions.create() again with the existing sessionId and a task to start the agent.
The settings dropdowns are populated by two list calls:
api.ts

Page flow

The home page calls these functions in sequence — create the session, navigate immediately, then fire-and-forget the task:
page.tsx
This pattern gives instant navigation — the user sees the session page (with the live browser view) while the task is still being dispatched.

Section 2: Messages Interface — Polling & Follow-ups

API layer

The session page needs three more SDK calls:
api.ts
  • getSession returns the session status (created, idle, running, stopped, timed_out, error) and the liveUrl.
  • getMessages returns the conversation history — user messages, agent thoughts, and tool calls.
  • stopTask stops only the current task (not the session) using strategy: "task", so the user can send another task.

Polling with React Query

The session context sets up two parallel polls using TanStack Query:
session-context.tsx
Both polls run at 1-second intervals and automatically stop when the session reaches a terminal status.

Sending follow-ups

Follow-up messages reuse the same sendTask function from the home page. The context adds optimistic updates so the user’s message appears instantly:
session-context.tsx
Optimistic messages are automatically filtered out once the server returns the real message with matching content.

Stopping a task

session-context.tsx
The session page wires stopTask to a stop button that appears while the agent is running. Since we used strategy: "task", the session stays alive for follow-ups.

Session page

The session page consumes everything through the context provider:
session/[id]/page.tsx

Summary

The full SDK surface used by this app: The complete source is at github.com/browser-use/chat-ui-example.