Skip to main content
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.
It is a library for CLIs that run on Node.js, not a feature of Laravel itself. The Laravel Cloud CLI, @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
Baton serves all three from the same code path. You write each command once, and its behavior adapts to whoever is running it.

Installation

Baton requires Node.js 22 or later. To start a new CLI, use the create-baton scaffolder.
The scaffolder generates an example command, a test, shell completions, an 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 extends BaseCommand. Here is the hello command from the README.
  • open: draws the title and loads ui only when someone is watching. On a JSON run, ui is a silent stand-in, so you never need to check whether anyone is watching.
  • form: uses the --name flag if it was passed, asks a person if one is at the terminal, and otherwise uses the fallback.
  • respond: writes the answer as JSON on a JSON run and ends the run. It has no effect on the output for people.
respond ends the run by throwing a CommandExit. If you wrap it in a try, rethrow the CommandExit, or the command will keep running past its answer.
Register commands in 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, and ui.error
  • Callouts: boxed messages with headings, steps, key-value pairs, and links, using ui.callout
  • Details and tables: ui.details for one item and ui.table for several
  • Code: ui.codeBlock for code or command output, and ui.copyable for text people copy and paste
  • Work in progress: ui.spin for a spinner, and ui.task for work that logs progress as it runs
Prompts exist for each kind of answer.
Once answered, each prompt collapses into a small box showing its label and answer, so the timeline reads as a record of every decision.

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 with CliError.
For a Laravel application, you pass the field errors from the 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.
Everything inside submit runs again from the top after a rejection. Put the request that may be rejected last in the body, and make sure anything before it is safe to repeat.

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 a CliError for anything a person should read. It is drawn on the timeline for people and written as JSON to stderr for machines.
Scripts should branch on 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 error
  • renderCommand: 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.

Laravel Cloud CLI

An overview of @laravel/cloud-cli, which uses Baton.

Laravel Prompts

The interactive UI for PHP Artisan commands.

Primary sources

Last modified on December 3, 2026