llm-anthropic plugin

repository·main·Indexed 42 days ago

https://github.com/simonw/llm-anthropic

A plugin for the `llm` CLI tool providing access to Anthropic's Claude models. Version 0.28 supports advanced features including prompt caching, thinking mode, fast mode, and structured outputs via --schema. It includes tools for WebSearch, WebFetch, CodeExecution, and an Anthropic MCP Connector for remote Model Context Protocol servers.

Tokens
5.2K
Snippets
25
Records
25
Agent score
4%

What's inside llm-anthropic

  1. Enable adaptive thinking

    main

    Instead of manual budget modes, Claude 4.6 models use adaptive thinking (thinking: {type: "adaptive"}). The model dynamically decides when and how much to think. You can enable this using the -o thinking 1 option.

    To verify if a model used thinking, you can inspect the response logs for content block types like ['thinking', 'text'].

    uv run llm -m claude-sonnet-4.6 -o thinking 1 "What is the square root of 97 to 3 decimal places?"
  2. Configure Extended Thinking (Reasoning)

    main

    Anthropic models can use "thinking tokens" to reason before responding. Reasoning is streamed to stderr; use -R/--hide-reasoning to hide it.

    Claude 5 models:

    • Think by default.
    • Use -o thinking_effort <level> to tune effort. Levels: low, medium, high, xhigh, or max.
    • Turn off thinking with -o thinking 0.

    Claude 4.6 and older models:

    • Do not think unless asked. Enable with -o thinking 1.
    • Claude 4.6 and Opus 4.5 also support thinking_effort (which implies thinking 1).

    Note on Display:

    • Claude 4.7+ models hide the thinking trace by default. This plugin requests display: summarized when thinking is on.
    • If -R/--hide-reasoning is used, it requests display: omitted, which removes the trace from logs (but tokens are still billed).

    Deprecation Note: thinking_budget, thinking_display, and thinking_adaptive were removed in version 0.26. Use the options above instead.

    # Maximize thinking effort (Claude 5)
    llm -m claude-opus-5 -o thinking_effort max 'Design a fair algorithm for splitting rent'
    
    # Enable thinking for older models (Claude 4.6)
    llm -m claude-sonnet-4.6 -o thinking 1 'Write a speech'
    
    # Turn off thinking (Claude 5)
    llm -m claude-sonnet-5 -o thinking 0 'Quick task'
  3. Use Mid-conversation System Messages

    main

    Claude Opus 4.8 and Claude 5 models support updating system instructions part-way through a conversation. This is useful for changing behavior while preserving prompt cache hits on earlier turns.

    To use this, pass a system message within an explicit messages= chain. The plugin automatically handles the API placement rules.

    Warning: Models older than Opus 4.8 will raise a ValueError if a system message appears anywhere other than the start of the chain.

    import llm
    from llm import system, user, assistant
    
    model = llm.get_model("claude-opus-5")
    response = model.prompt(messages=[
        system("You are a helpful assistant."),
        user("Say hi to me, briefly"),
        assistant("Hi there!"),
        system("New instruction: reply only in French from now on"),
        user("Say goodbye to me, briefly"),
    ])
    print(response.text())  # Au revoir !
  4. Configure Anthropic API Key

    main

    Before using the plugin, you must set your Anthropic API key. You can do this via the llm keys command or by setting the ANTHROPIC_API_KEY environment variable.

    To set it via the CLI:

    1. Run llm keys set anthropic.
    2. Paste your key when prompted.
    llm keys set anthropic
    # Paste key here
  5. Configure stop sequences

    main

    Stop sequences tell the model to stop generating text when it encounters specific strings. You can pass a single string for one sequence or a JSON array for multiple sequences.

    # Single stop sequence
    llm -m claude-sonnet-5 'Fun facts about pelicans' \
      -o stop-sequences "beak"
    
    # Multiple stop sequences (JSON array)
    llm -m claude-sonnet-5 'Fun facts about pelicans' \
      -o stop-sequences '["beak", "feathers"]'
  6. Basic Usage of Anthropic models via CLI

    main

    You can interact with Claude models using the llm -m <model_name> command.

    To see available models, run llm models. To see available options for each model, run llm models --options.

    Common usage patterns:

    • Text prompts: llm -m <model> '<prompt>'
    • Image attachments: Use the -a flag with a URL or local path. Supported by Claude 3.5+.
    • PDF attachments: Use the -a flag with a .pdf file. Supported by Claude 3.5+.
    • Structured output (Schemas): Use the --schema flag to define the desired JSON structure.
    # Text prompt
    llm -m claude-opus-5 'Fun facts about walruses'
    
    # Image attachment
    llm -m claude-sonnet-5 'describe this image' -a https://static.simonwillison.net/static/2024/pelicans.jpg
    
    # PDF attachment
    llm -m claude-sonnet-5 'extract text' -a page.pdf
    
    # Using schemas for structured JSON output
    llm -m claude-sonnet-5 --schema 'name,age int,bio: one sentence' 'invent a surprising dog'
  7. Record and manage API cassettes for testing

    main

    The project uses pytest-recording to capture Anthropic API responses. To record new responses or update existing ones, use the --record-mode flag. You must have a valid Anthropic API key stored in llm first.

    # 1. Set your key first
    llm keys set anthropic
    
    # 2. Record a new API interaction for a test
    PYTEST_ANTHROPIC_API_KEY="$(llm keys get anthropic)" uv run pytest --record-mode once
    
    # 3. Re-record all cassettes and fix inline snapshots
    rm tests/cassettes/test_anthropic/*.yaml
    PYTEST_ANTHROPIC_API_KEY="$(llm keys get anthropic)" uv run pytest --record-mode all --inline-snapshot=fix
    
    # 4. Re-record a specific test
    rm tests/cassettes/test_anthropic/test_thinking_prompt.yaml
    PYTEST_ANTHROPIC_API_KEY="$(llm keys get anthropic)" uv run pytest -k test_thinking_prompt --record-mode once --inline-snapshot=fix
  8. Set up the llm-anthropic plugin for development

    main

    To develop locally, create a virtual environment, install the package in editable mode with dev dependencies, and run tests using pytest or uv.

    # Setup environment
    cd llm-anthropic
    python3 -m venv venv
    source venv/bin/activate
    
    # Install dependencies
    pip install -e . --group dev
    
    # Run tests
    pytest
    
    # Using uv
    uv run pytest
    uv run llm 'your prompt here'
  9. List Claude 4.6 models

    main

    You can verify the availability of the new Claude 4.6 models by listing all registered models and filtering for '4.6'. The models are available via the following aliases:

    • claude-sonnet-4.6 (Sonnet 4.6)
    • claude-opus-4.6 (Opus 4.6)
    uv run llm models | grep "4\.6"
  10. Use prefill to control response format

    main

    The prefill option allows you to set the beginning of the model's response. This is highly effective for forcing specific formats like JSON by starting the response with a character like {.

    You can also use hide_prefill (set to true) to prevent the prefill text from being echoed back in the final output. This is useful when using code block delimiters as prefill to get clean code responses.

    # Force JSON output by prefilling with '{'
    llm -m claude-sonnet-5 'Fun data about pelicans' \
      -o prefill '{'
    
    # Get clean Python code without Markdown wrappers
    llm -m claude-haiku-4.5 'Short python function describing a pelican' \
      -o prefill '```python' \
      -o hide_prefill true \
      -o stop_sequences '```'
  11. Configure thinking effort

    main

    The thinking_effort parameter allows you to control token spend independently of whether adaptive thinking is enabled.

    Supported values:

    • low
    • medium
    • high
    • max (Note: max is only supported by claude-opus-4.6. Attempting to use max with Sonnet 4.6 will result in an error.)

    Use the -o thinking_effort <level> flag to set the effort.

    uv run llm -m claude-sonnet-4.6 -o thinking_effort low "What is the capital of France?"