Interactive Demo

CLI Integration

See how AI agents embed directly into your terminal workflow — reading files, understanding git context, calling tools, and streaming structured output through standard Unix pipes.

stdin/stdout Git-Aware Agent Modes Tool Calling
Command Palette
Run a Command
Available Flags
--agent Named agent mode
--pipe Read from stdin
--output Format: json, md, text
--model Select model variant
--context Add files to context
--verbose Show tool calls
--dry-run Preview, no writes
--max-tokens Limit response size
claude-agent — bash
# Click a command from the palette, then press Run # or click the play icon next to any command ~/project $
Idle
~/project
main

Context Flow

How the CLI gathers context from your environment and passes it to the agent for grounded, accurate responses.

CLI Input
Command, flags, prompt text, and piped stdin data
Context Engine
Git diff, file tree, env vars, project config, open files
Agent Core
Reasoning, planning, multi-step execution with memory
Tool Layer
File read/write, search, shell exec, git operations
Structured Output
Streamed text, JSON, diffs, or markdown to stdout
Source files Git history Project config Dependency graph Env variables CLAUDE.md

How It Works

Three core integration patterns that make CLI agents practical for real development workflows.

stdin / stdout Integration

The agent reads from standard input and writes to standard output, making it composable with any Unix tool. Pipe error logs, diffs, build output, or any text stream directly into the agent. Output can be plain text, structured JSON, or markdown depending on the --output flag.

# Pipe build errors for diagnosis
npm run build 2>&1 | claude "fix these errors"

# Chain with other tools
claude --output json "list TODOs" | jq '.items[]'

# Process file contents
cat schema.sql | claude "generate migrations"

Context Injection

Before the prompt reaches the model, the CLI automatically collects ambient context: current git branch and recent diffs, directory structure, open file contents, project configuration from CLAUDE.md, and environment metadata. This grounds every response in your actual project state.

# Auto-detected context:
git branch → feature/auth-v2
git diff → +47 -12 lines staged
tree → src/, tests/, config/
CLAUDE.md → project rules
package.json → dependencies

# Explicit context addition
claude --context src/auth.ts "review"

Agent Modes

Named agents specialize the model for specific tasks. A researcher agent searches documentation and summarizes findings. A reviewer agent checks code quality and security. Each mode configures system prompts, tool permissions, and output formats for its domain.

# Research agent
claude --agent researcher "OAuth2 PKCE flow"

# Code review agent
claude --agent reviewer "check PR #42"

# Test generation agent
claude --agent tester "cover auth module"

# Custom agent from config
claude --agent my-agent "run analysis"

Architecture Overview

A vertical view of the CLI agent pipeline, from user input to structured response output.

CLI Input command + flags + prompt API Layer context + model routing Tool Execution read, write, search, shell Response Output text / json / diff stream

Deep Dive

Learn the patterns behind building production-grade CLI tools with AI integration.

Modern CLI tools are evolving beyond static flag parsing. By integrating large language models directly into the command-line interface, developers gain an interactive assistant that understands project context, reads files, and generates structured output -- all within familiar terminal workflows.

Key integration patterns include:

  • Prompt-as-argument: Pass natural language directly as a CLI argument, letting the model interpret intent alongside project context.
  • Piped input: Stream data from other Unix tools (logs, diffs, build output) into the model via stdin, making AI composable with existing toolchains.
  • Tool calling: The model can invoke sandboxed tools (file read/write, search, shell commands) to gather information or make changes, closing the loop between reasoning and action.
  • Streaming output: Responses are streamed token-by-token to stdout, giving users immediate feedback and enabling downstream piping.

This architecture makes the AI agent a first-class Unix citizen -- it reads from stdin, writes to stdout, returns meaningful exit codes, and composes with |, >, and xargs just like any other tool.

A minimal Python CLI skeleton using Click with AI integration:

import click
import sys
from anthropic import Anthropic

client = Anthropic()

@click.group()
@click.option("--model", default="claude-sonnet-4-20250514", help="Model to use")
@click.pass_context
def cli(ctx, model):
    """AI-powered CLI assistant."""
    ctx.ensure_object(dict)
    ctx.obj["model"] = model

@cli.command()
@click.argument("prompt")
@click.option("--pipe", is_flag=True, help="Read from stdin")
@click.option("--output", type=click.Choice(["text", "json", "md"]),
              default="text")
@click.option("--context", multiple=True, help="Files to include")
def ask(prompt, pipe, output, context):
    """Ask the AI a question with optional context."""
    messages = []

    # Read piped input
    if pipe and not sys.stdin.isatty():
        stdin_data = sys.stdin.read()
        messages.append({
            "role": "user",
            "content": f"Input data:\n```\n{stdin_data}\n```\n\n{prompt}"
        })
    else:
        messages.append({"role": "user", "content": prompt})

    # Add file context
    for filepath in context:
        with open(filepath) as f:
            messages[0]["content"] += f"\n\nFile {filepath}:\n```\n{f.read()}\n```"

    # Stream the response
    with client.messages.stream(
        model=ctx.obj["model"],
        max_tokens=4096,
        messages=messages,
    ) as stream:
        for text in stream.text_stream:
            click.echo(text, nl=False)

    click.echo()  # Final newline

if __name__ == "__main__":
    cli()

This skeleton supports piped input, file context injection, output format selection, and streaming responses. Extend it with tool definitions for file operations, search, and shell execution.

Production CLI tools with AI integration should follow these patterns:

Streaming Output
  • Always stream responses token-by-token rather than waiting for the full completion. This provides immediate feedback and allows users to interrupt early.
  • Use Server-Sent Events or chunked transfer encoding for HTTP-based implementations.
  • Buffer partial tokens on word boundaries for cleaner display in terminals that don't handle partial UTF-8 well.
Error Handling
  • Return meaningful exit codes: 0 for success, 1 for user errors, 2 for API failures, 3 for tool execution failures.
  • Write error messages to stderr so they don't pollute piped output. Use click.echo(..., err=True).
  • Implement exponential backoff with jitter for rate-limited API calls. Set sensible timeouts (30s for simple queries, 120s for agent tasks).
  • Catch and surface tool execution errors gracefully -- the model should be able to retry with a different approach.
Authentication Patterns
  • Store API keys in environment variables (ANTHROPIC_API_KEY), never in config files or command history.
  • Support a config file hierarchy: project-level .claude.json overrides user-level ~/.config/claude/config.json.
  • Implement token refresh for OAuth-based setups. Cache tokens securely using OS keychain (macOS Keychain, Linux Secret Service, Windows Credential Manager).
  • Validate credentials on startup and provide clear error messages with setup instructions when authentication fails.