Lesson 22Advanced
Agent SDK
Programmatic access
SDK vs CLI: When to Use Which
CLI (Interactive)
- • Interactive development
- • Debugging and exploration
- • One-off tasks
- • Learning and experiments
SDK (Programmatic)
- • CI/CD automation
- • Batch processing
- • App integration
- • Custom workflows
Headless Mode
Headless mode allows running Claude Code without interactive input:
# Headless mode - no interactive prompts claude --headless --prompt "Fix all TypeScript errors" --output result.json # With specific model claude --headless --model sonnet --prompt "Add tests for UserService" # With MCP servers claude --headless --mcp-config ./mcp.json --prompt "Query database for users"
--headlessNo interactive prompts--promptTask to execute--outputOutput file--modelModel selectionSDK Examples
from anthropic_claude_code import ClaudeCode
# Initialize Claude Code
claude = ClaudeCode()
# Run a task
result = claude.run(
prompt="Fix the failing tests in auth.ts",
working_dir="/path/to/project"
)
print(result.output)
print(f"Cost: ${result.cost}")GitHub Actions
Integrate Claude Code into CI/CD via GitHub Actions:
name: Code Review with Claude
on:
pull_request:
types: [opened, synchronize]
jobs:
review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run Claude Code Review
uses: anthropic/claude-code-action@v1
with:
prompt: |
Review this PR for:
- Code quality issues
- Security vulnerabilities
- Performance problems
Provide specific feedback with line numbers.
github-token: ${{ secrets.GITHUB_TOKEN }}
anthropic-api-key: ${{ secrets.ANTHROPIC_API_KEY }}SDK Use Cases
Automatic PR Code Review
Run review on every PR
PR opened
Test Generation
Auto-write tests for new code
New file
Documentation
Update docs on changes
Merge to main
DB Migrations
Generate migrations from description
Manual
SDK Usage Tips
- 1.Use --max-turns to limit iterations
- 2.Handle errors and timeouts
- 3.Monitor costs via result.cost
- 4.Store API keys in secrets
This lesson is part of a structured LLM course.
My Learning Path