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
promptstring • required | The prompt you want Claude to complete. For proper response generation you will most likely want 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_sampleint • required | A maximum number of tokens to generate before stopping. | ||
stop_sequenceslist of strings • optional | A list of strings upon which to stop generating. You probably want ["\n\nHuman:"], as that's the cue for the next turn in the dialog agent. Our client libraries provide a constant for this value (see examples below) | ||
streamboolean • optional defaults to false | Whether to incrementally stream the response using SSE. | ||
temperaturefloat • 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_kint • 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_pfloat • 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. | ||
metadataobject • optional defaults to {} | An object describing metadata about the request. Child parameters:
|
Response
completionstring | The resulting completion up to and excluding the stop sequences |
stop_reason"stop_sequence" or "max_tokens" | The reason we stopped sampling, either stop_sequence if we reached one of your provided stop_sequences, or max_tokens if we exceeded max_tokens_to_sample or the model's maximum. |
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.