Using the API
API Reference
Base URL
All API requests should be sent to https://api.anthropic.com/
.
Authentication
API requests require that an API key be passed in an X-API-Key
header. New API keys can be generated via the console. Both of our SDKs accept an API key when you instantiate the client object:
# Python
client = anthropic.Client(api_key)
// Typescript
const client = new Client(apiKey);
Format
All arguments must be passed as a JSON object. As a result, you must also pass the Content-Type: application/json
header with your request to signal that you are sending JSON.
All responses are given in JSON. If using streaming, each new instance of data sent will be a full JSON object.
/v1/complete
This POST
endpoint sends a prompt to Claude for completion.
Parameters
prompt string • required | The prompt you want Claude to complete. For proper response generation you will need to format your prompt as follows: See our comments on prompts for more context. | ||
| As we improve Claude, we develop new versions of it that you can query. This controls which version of Claude answers your request. Right now we are offering two model families: Claude and Claude Instant. Specifiying any of the following models will automatically switch to you the newest compatible models as they are released:
You can also select specific sub-versions of the above models:
| ||
max_tokens_to_sample int • required | A maximum number of tokens to generate before stopping. | ||
stop_sequences list of strings • optional | Our models stop on "\n\nHuman:" , and may include additional built-in stop sequences in the future. By providing the stop_sequences parameter, you may include additional strings that will cause the model to stop generating. | ||
stream boolean • optional defaults to false | Whether to incrementally stream the response using SSE. | ||
temperature float • optional defaults to 1 | Amount of randomness injected into the response. Ranges from 0 to 1. Use temp closer to 0 for analytical / multiple choice, and temp closer to 1 for creative and generative tasks. | ||
top_k int • optional defaults to -1 | Only sample from the top K options for each subsequent token. Used to remove "long tail" low probability responses. Defaults to -1, which disables it. Learn more technical details here. | ||
top_p float • optional defaults to -1 | Does nucleus sampling, in which 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 . Defaults to -1, which disables it. Note that you should either alter temperature or top_p , but not both. | ||
metadata object • optional defaults to {} | An object describing metadata about the request. Child parameters:
|
Response
completion string | The resulting completion up to and excluding the stop sequences |
stop_reason "stop_sequence" or "max_tokens" | The reason we stopped sampling:
|
Examples
curl
# Synchronous request: only replies with the full response
export API_KEY=my_api_key
curl https://api.anthropic.com/v1/complete\
-H "x-api-key: $API_KEY"\
-H 'content-type: application/json'\
-d '{
"prompt": "\n\nHuman: Tell me a haiku about trees\n\nAssistant: ",
"model": "claude-v1", "max_tokens_to_sample": 300, "stop_sequences": ["\n\nHuman:"]
}'
# Streaming request: sends the response as it's generated
# The -N arg tells curl not to buffer
export API_KEY=my_api_key
curl -N https://api.anthropic.com/v1/complete\
-H "x-api-key: $API_KEY"\
-H 'content-type: application/json'\
-d '{
"prompt": "\n\nHuman: Tell me a haiku about trees\n\nAssistant: ",
"model": "claude-v1", "max_tokens_to_sample": 300, "stop_sequences": ["\n\nHuman:"],
"stream": true
}'
Python
See the Python library GitHub repo.
Typescript
See the Typescript library GitHub repo.