This article is based on the README and package.json of laravel/baton. It reflects
@laravel/baton v0.1.1 (released December 1, 2026). The package is at 0.x, so its API may change.What Baton is
Baton is a TypeScript package for building command-line apps that work well whether a person, CI, or an AI agent runs them. From one command definition, you get different behavior depending on who is running it.@laravel/cloud-cli, is also built with Baton (Laravel Cloud CLI overview).
The problem it addresses
A CLI has three kinds of users.- People: want clear prompts, tidy output, and suggestions when they mistype a command
- CI: wants predictable exit codes and output it can parse
- AI agents: want machine-friendly output and never want to stop at a question nobody can answer
Installation
Baton requires Node.js 22 or later. To start a new CLI, use thecreate-baton scaffolder.
AGENTS.md (working rules for AI agents), and a CLAUDE.md for Claude Code.
To add Baton to an existing oclif CLI, install the package and point the oclif settings in package.json at your command map, help class, and typo handler.
Writing a command
Every Baton command extendsBaseCommand. Here is the hello command from the README.
open: draws the title and loadsuionly when someone is watching. On a JSON run,uiis a silent stand-in, so you never need to check whether anyone is watching.form: uses the--nameflag if it was passed, asks a person if one is at the terminal, and otherwise uses thefallback.respond: writes the answer as JSON on a JSON run and ends the run. It has no effect on the output for people.
src/index.ts.
Output and prompts
All output is drawn on a single timeline with a gray rail down the left edge. The main building blocks are:- Notes: one-line messages with
ui.info,ui.success,ui.warning, andui.error - Callouts: boxed messages with headings, steps, key-value pairs, and links, using
ui.callout - Details and tables:
ui.detailsfor one item andui.tablefor several - Code:
ui.codeBlockfor code or command output, andui.copyablefor text people copy and paste - Work in progress:
ui.spinfor a spinner, andui.taskfor work that logs progress as it runs
Forms and validation
When an API rejects an input, the form asks again for only the rejected fields, with the rejected value filled in. You signal a rejection withCliError.
errors object of a 422 response. The CLI then asks again only for the rejected name.
On a headless run, nobody can answer again, so the form writes a JSON error to stderr that includes the values it sent, and exits with code 5. Keys that name secrets, such as password and token, are masked as ***** by default.
Machine output
JSON and agent detection
A command answers in JSON when--json is passed, or when nobody can answer a question: in CI, with stdin or stdout piped, with -n or --no-interaction, or when an AI agent runs it. Baton recognizes 16 agents, including Claude Code, Cursor, Codex, Gemini, and Copilot. For others, set the AI_AGENT variable.
Because a person’s shell may also set these variables, you can treat a person as interactive with a variable named after your CLI’s bin, such as MY_CLI_INTERACTIVE=1. A piped stdin or stdout, --json, and -n still take priority over it.
JSON is written to stdout as a single compact line. Use --fields to select only the values you need.
Masking
By default, any value whose key names a secret (password, token, credential, private key, API key, authorization, cookie, session id, DSN, and so on) is replaced with*****. Pass --show-sensitive to reveal it. Keys such as db_password and apiKey are masked, while bypass_cache is not.
Errors and exit codes
Throw aCliError for anything a person should read. It is drawn on the timeline for people and written as JSON to stderr for machines.
code, not on message.
Testing
Tests can check both what a script receives and what a person sees, without a terminal.runCommand: runs the command as a script or agent would, and returns the exit code, JSON, and errorrenderCommand: runs the command as a person would, answers each prompt by its label, and returns the drawn timeline as text
Write output through
ui and respond, not console.log. Output that bypasses Baton’s rendering cannot be checked by these tests.Observations
- Baton is a Node.js library, not part of Laravel. For interactive prompts in Artisan commands, use Laravel Prompts.
- It is used by
@laravel/cloud-cli, and it is designed so that AI agents can run the same CLI. - It is at 0.x, so the API may change. If you adopt it, consider pinning the version.
- It is licensed under MIT.
Related pages
Laravel Cloud CLI
An overview of
@laravel/cloud-cli, which uses Baton.Laravel Prompts
The interactive UI for PHP Artisan commands.
Primary sources
laravel/baton(default branchmain)- README
package.json(@laravel/batonv0.1.1, MIT)docs/AGENTS.md