Docs/Reference/CLI Reference

>_ DOCS / REFERENCE

CLI
REFERENCE.

The p402 CLI gives you direct access to routing, sessions, API keys, and the model catalog from your terminal.

Installation

bash
# npm
npm install -g @p402/cli

# Or use without installing (always latest version):
npx @p402/cli chat "What is the x402 protocol?"
bash
# Configure your API key (stored in ~/.p402/config.json)
p402 config set api-key p402_live_...

# Or use an environment variable (recommended for CI):
export P402_API_KEY=p402_live_...

Quick Examples

bash
# Ask a question (cost mode, caching enabled)
p402 chat "What is the x402 payment protocol?"

# Ask with quality routing
p402 chat "Review this Solidity contract for reentrancy vulnerabilities" --mode quality

# Create a $5 session and ask with budget cap
SESSION=$(p402 session create --budget 5 --output plain)
p402 chat "Summarise EIP-3009" --session $SESSION

# Stream a response
p402 chat "Write a short poem about Base blockchain" --stream

# Compare providers for a prompt
p402 models compare "Explain EIP-712 typed data signing"

# Check provider health
p402 health

Command Reference

Global Flags
Command / Flag
Description
--api-key <key>
alias: -k
P402 API key. Overrides P402_API_KEY env var.
--output <format>
alias: -o
Output format: json | table | plain. Default: table.
--no-color
Disable colour output.
--version
alias: -v
Print CLI version and exit.
--help
alias: -h
Show help for any command.
p402 chat
Command / Flag
Description
p402 chat "<prompt>"
Send a chat completion and print the response.
--mode <mode>
alias: -m
Routing mode: cost | quality | speed | balanced.
--session <id>
alias: -s
Use an existing session ID for budget tracking.
--no-cache
Disable semantic caching for this request.
--stream
Stream the response token by token.
--system "<prompt>"
Set the system prompt.
--model <model>
Force a specific model (e.g. gpt-4o, claude-opus-4).
p402 session
Command / Flag
Description
p402 session create
Create a new session. Prompts for budget if not set.
--budget <usd>
alias: -b
Budget cap in USD (e.g. 5.00).
--expires <hours>
alias: -e
Session TTL in hours. Default: 168 (7 days).
p402 session list
List active sessions with budget and spend.
p402 session get <id>
Show session details and real-time stats.
p402 session fund <id>
Add budget to an existing session.
p402 session close <id>
Close a session immediately.
p402 key
Command / Flag
Description
p402 key create
Generate a new API key. Prints raw key once.
--name <name>
alias: -n
Human-readable key name (e.g. "ci-pipeline").
p402 key list
List all API keys (prefix only — never raw).
p402 key revoke <id>
Immediately revoke a key.
p402 models
Command / Flag
Description
p402 models list
List all 300+ routable models with live pricing.
--provider <name>
Filter by provider (e.g. openai, anthropic, groq).
--sort <field>
Sort by: cost | quality | speed. Default: cost.
p402 models compare "<prompt>"
Compare cost/latency/quality for a specific prompt across providers.
p402 cache
Command / Flag
Description
p402 cache stats
Show cache hit rate, entries, and cost saved.
p402 cache clear
Clear all cached entries for your tenant.
p402 health
Command / Flag
Description
p402 health
Check P402 API and all provider health status.
--provider <name>
Check a specific provider only.

Environment Variables

Variable
Description
P402_API_KEY
API key. Overrides config file.
P402_BASE_URL
Override the API base URL. Useful for local dev / staging.
P402_OUTPUT
Default output format: json | table | plain.
NO_COLOR
Disable all colour output (standard env flag).