LLM access to models by Anthropic, including the Claude series
Project description
llm-anthropic
LLM access to models by Anthropic, including the Claude series
Installation
Install this plugin in the same environment as LLM.
llm install llm-anthropic
Instructions for users who need to upgrade from llm-claude-3
If you previously used llm-claude-3 you can upgrade like this:
llm install -U llm-claude-3
llm keys set anthropic --value "$(llm keys get claude)"
The first line will remove the previous llm-claude-3 version and install this one, because the latest llm-claude-3 depends on llm-anthropic.
The second line sets the anthropic key to whatever value you previously used for the claude key.
Usage
First, set an API key for Anthropic:
llm keys set anthropic
# Paste key here
You can also set the key in the environment variable ANTHROPIC_API_KEY
Run llm models to list the models, and llm models --options to include a list of their options.
Run prompts like this:
llm -m claude-opus-4.1 'Fun facts about walruses'
llm -m claude-sonnet-4.5 'Fun facts about pelicans'
llm -m claude-3.5-haiku 'Fun facts about armadillos'
llm -m claude-haiku-4.5 'Fun facts about cormorants'
Image attachments are supported too:
llm -m claude-sonnet-4.5 'describe this image' -a https://static.simonwillison.net/static/2024/pelicans.jpg
llm -m claude-haiku-4.5 'extract text' -a page.png
The Claude 3.5 and 4 models can handle PDF files:
llm -m claude-sonnet-4.5 'extract text' -a page.pdf
Anthropic's models support schemas. Here's how to use Claude 4 Sonnet to invent a dog:
llm -m claude-sonnet-4.5 --schema 'name,age int,bio: one sentence' 'invent a surprising dog'
Example output:
{
"name": "Whiskers the Mathematical Mastiff",
"age": 7,
"bio": "Whiskers is a mastiff who can solve complex calculus problems by barking in binary code and has won three international mathematics competitions against human competitors."
}
Extended reasoning with Claude 3.7 Sonnet
Claude 3.7 introduced extended thinking mode, where Claude can expend extra effort thinking through the prompt before producing a response.
Use the -o thinking 1 option to enable this feature:
llm -m claude-3.7-sonnet -o thinking 1 'Write a convincing speech to congress about the need to protect the California Brown Pelican'
The chain of thought is not currently visible while using LLM, but it is logged to the database and can be viewed using this command:
llm logs -c --json
Or in combination with jq:
llm logs --json -c | jq '.[0].response_json.content[0].thinking' -r
By default up to 1024 tokens can be used for thinking. You can increase this budget with the thinking_budget option:
llm -m claude-3.7-sonnet -o thinking_budget 32000 'Write a long speech about pelicans in French'
Model options
The following options can be passed using -o name value on the CLI or as keyword=value arguments to the Python model.prompt() method:
-
max_tokens:
intThe maximum number of tokens to generate before stopping
-
temperature:
floatAmount of randomness injected into the response. Defaults to 1.0. Ranges from 0.0 to 1.0. Use temperature closer to 0.0 for analytical / multiple choice, and closer to 1.0 for creative and generative tasks. Note that even with temperature of 0.0, the results will not be fully deterministic.
-
top_p:
floatUse nucleus sampling. In nucleus sampling, we compute the cumulative distribution over all the options for each subsequent token in decreasing probability order and cut it off once it reaches a particular probability specified by top_p. You should either alter temperature or top_p, but not both. Recommended for advanced use cases only. You usually only need to use temperature.
-
top_k:
intOnly sample from the top K options for each subsequent token. Used to remove 'long tail' low probability responses. Recommended for advanced use cases only. You usually only need to use temperature.
-
user_id:
strAn external identifier for the user who is associated with the request
-
prefill:
strA prefill to use for the response
-
hide_prefill:
booleanDo not repeat the prefill value at the start of the response
-
stop_sequences:
array, strCustom text sequences that will cause the model to stop generating - pass either a list of strings or a single string
-
cache:
booleanUse Anthropic prompt cache for any attachments or fragments
-
thinking:
booleanEnable thinking mode
-
thinking_budget:
intNumber of tokens to budget for thinking
The prefill option can be used to set the first part of the response. To increase the chance of returning JSON, set that to {:
llm -m claude-sonnet-4.5 'Fun data about pelicans' \
-o prefill '{'
If you do not want the prefill token to be echoed in the response, set hide_prefill to true:
llm -m claude-3.5-haiku 'Short python function describing a pelican' \
-o prefill '```python' \
-o hide_prefill true \
-o stop_sequences '```'
This example sets ``` as the stop sequence, so the response will be a Python function without the wrapping Markdown code block.
To pass a single stop sequence, send a string:
llm -m claude-sonnet-4.5 'Fun facts about pelicans' \
-o stop-sequences "beak"
For multiple stop sequences, pass a JSON array:
llm -m claude-sonnet-4.5 'Fun facts about pelicans' \
-o stop-sequences '["beak", "feathers"]'
When using the Python API, pass a string or an array of strings:
response = llm.query(
model="claude-sonnet-4.5",
query="Fun facts about pelicans",
stop_sequences=["beak", "feathers"],
)
Development
To set up this plugin locally, first checkout the code. Then create a new virtual environment:
cd llm-anthropic
python3 -m venv venv
source venv/bin/activate
Now install the dependencies and test dependencies:
llm install -e '.[test]'
To run the tests:
pytest
This project uses pytest-recording to record Anthropic API responses for the tests.
If you add a new test that calls the API you can capture the API response like this:
PYTEST_ANTHROPIC_API_KEY="$(llm keys get anthropic)" pytest --record-mode once
You will need to have stored a valid Anthropic API key using this command first:
llm keys set anthropic
# Paste key here
I use the following sequence:
# First delete the relevant cassette if it exists already:
rm tests/cassettes/test_anthropic/test_thinking_prompt.yaml
# Run this failing test to recreate the cassette
PYTEST_ANTHROPIC_API_KEY="$(llm keys get claude)" pytest -k test_thinking_prompt --record-mode once
# Now run the test again with --pdb to figure out how to update it
pytest -k test_thinking_prompt --pdb
# Edit test
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file llm_anthropic-0.20.tar.gz.
File metadata
- Download URL: llm_anthropic-0.20.tar.gz
- Upload date:
- Size: 16.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
43a59a6853336a6bd4c8ee1bf73b148d2e7d74d8ad4d2b466b18e4c69680d8af
|
|
| MD5 |
23e078a09412b97f837ef68f1ecc32f2
|
|
| BLAKE2b-256 |
c578f9a76cea9b141ad35684cac1892787f4e8fe840800fcffba0513aa1aff5d
|
Provenance
The following attestation bundles were made for llm_anthropic-0.20.tar.gz:
Publisher:
publish.yml on simonw/llm-anthropic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
llm_anthropic-0.20.tar.gz -
Subject digest:
43a59a6853336a6bd4c8ee1bf73b148d2e7d74d8ad4d2b466b18e4c69680d8af - Sigstore transparency entry: 609265094
- Sigstore integration time:
-
Permalink:
simonw/llm-anthropic@7a1f62719744c16d671e33c1f2f307857c9187df -
Branch / Tag:
refs/tags/0.20 - Owner: https://github.com/simonw
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@7a1f62719744c16d671e33c1f2f307857c9187df -
Trigger Event:
release
-
Statement type:
File details
Details for the file llm_anthropic-0.20-py3-none-any.whl.
File metadata
- Download URL: llm_anthropic-0.20-py3-none-any.whl
- Upload date:
- Size: 13.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
546edb43ea03e779e8114a0b6924a517f1c8bc1423a5a6b94fca9abbad6e80de
|
|
| MD5 |
0daffd0fbd7552672e783829815948c8
|
|
| BLAKE2b-256 |
b452f69beedd4fe84a97edc69d8e41aa2430046802ecfb134c4a4652efd88a12
|
Provenance
The following attestation bundles were made for llm_anthropic-0.20-py3-none-any.whl:
Publisher:
publish.yml on simonw/llm-anthropic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
llm_anthropic-0.20-py3-none-any.whl -
Subject digest:
546edb43ea03e779e8114a0b6924a517f1c8bc1423a5a6b94fca9abbad6e80de - Sigstore transparency entry: 609265151
- Sigstore integration time:
-
Permalink:
simonw/llm-anthropic@7a1f62719744c16d671e33c1f2f307857c9187df -
Branch / Tag:
refs/tags/0.20 - Owner: https://github.com/simonw
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@7a1f62719744c16d671e33c1f2f307857c9187df -
Trigger Event:
release
-
Statement type: