The @ai-sdk/tui package runs a ToolLoopAgent in an interactive terminal, so you can chat with your agent locally without building a UI. The terminal interface handles prompt input, streamed responses, markdown rendering, tool cards, reasoning sections, scrolling, and tool approval prompts. It's a good fit for local development, demos, and internal tools where a terminal experience is enough. You build the agent as usual and hand it to runAgentTUI.
In this guide, you'll learn how to:
- Install
@ai-sdk/tuiand run aToolLoopAgentwithrunAgentTUI - Configure how tool calls, reasoning, and statistics are displayed
- Prompt for tool approval before the agent runs a tool
- Know when to use the terminal UI versus
agent.generate()oragent.stream()
Before you begin, make sure you have:
- The
aipackage and a provider package (e.g.,@ai-sdk/openai) - A
ToolLoopAgentto run
Install ai alongside @ai-sdk/tui and your provider package:
pnpm i ai @ai-sdk/tui @ai-sdk/openaiCreate a ToolLoopAgent and pass it to runAgentTUI. This opens an interactive session that runs until you exit.
Run the file with a TypeScript runner such as tsx agent.ts. The session stays open until you exit with Esc or Ctrl+C.
Pass display options to runAgentTUI to control how tool calls, reasoning, and response statistics appear.
| Option | Values | Default | Description |
|---|---|---|---|
tools | 'full', 'collapsed', 'auto-collapsed', 'hidden' | 'auto-collapsed' | How tool calls render. 'full' shows input and output, 'collapsed' shows only tool cards, 'auto-collapsed' keeps the latest tool expanded until another section appears, and 'hidden' omits tool calls |
reasoning | 'full', 'collapsed', 'auto-collapsed', 'hidden' | 'auto-collapsed' | How reasoning renders, with the same options as tools |
responseStatistics | 'outputTokensPerSecond', 'outputTokenCount' | 'outputTokensPerSecond' | Whether to show output token throughput or total output token count |
contextSize | number | — | When set, shows total token usage as a percentage of the model's context window |
runAgentTUI supports the ToolLoopAgent tool approval flow. When the agent requests manual approval for a tool, the terminal prompts you to approve or deny it before the agent continues.
Configure approvals with toolApproval on the agent:
When a tool needs review, press y to approve or n to deny. In this example, weather requests for San Francisco run automatically, while requests for every other location wait for your approval.
runAgentTUI is built for agents that run directly from free-form terminal input. The agent must not require per-call options and must not use structured output, because the terminal UI can't infer those values from a typed prompt. For fixed prompts, call options, structured output, custom result inspection, or custom stream processing, call agent.generate() or agent.stream() directly instead.
The terminal UI responds to these keys:
| Key | Action |
|---|---|
Enter | Submit the prompt |
y / n | Approve or deny a tool call |
Up / Down | Scroll the transcript |
PageUp / PageDown | Scroll the transcript by a full page |
Ctrl+L | Repaint the screen |
Esc / Ctrl+C | Exit |
- See Building agents for creating
ToolLoopAgentinstances. - Read Tool approvals to configure human review of tool calls.
- Check the
runAgentTUIreference for every parameter.