v49

latestOpenAPI 3.1.0raw.githubusercontent.com2026-07-261319281.7 MB

Create a Text Completion

[Legacy] Create a Text Completion.

The Text Completions API is a legacy API. We recommend using the Messages API going forward.

Future models and features will not be compatible with Text Completions. See our migration guide for guidance in migrating from Text Completions to Messages.

post/v1/complete

Headers

anthropic-versionstring

The version of the Claude API you want to use.

Read more about versioning and our version history here.

The version of the Claude API you want to use.

Read more about versioning and our version history here.

anthropic-betastring

Optional header to specify the beta version(s) you want to use.

To use multiple betas, use a comma separated list like beta1,beta2 or specify the header multiple times for each beta.

Optional header to specify the beta version(s) you want to use.

To use multiple betas, use a comma separated list like beta1,beta2 or specify the header multiple times for each beta.

Request body

promptstring required

The prompt that you want Claude to complete.

For proper response generation you will need to format your prompt using alternating \n\nHuman: and \n\nAssistant: conversational turns. For example:

"\n\nHuman: {userQuestion}\n\nAssistant:"

See prompt validation and our guide to prompt design for more details.

max_tokens_to_sampleinteger required

The maximum number of tokens to generate before stopping.

Note that our models may stop before reaching this maximum. This parameter only specifies the absolute maximum number of tokens to generate.

stop_sequencesstring[]

Sequences that will cause the model to stop generating.

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.

temperaturenumber

Amount 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_pnumber

Use 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.

Recommended for advanced use cases only.

top_kinteger

Only sample from the top K options for each subsequent token.

Used to remove "long tail" low probability responses. Learn more technical details here.

Recommended for advanced use cases only.

streamboolean

Whether to incrementally stream the response using server-sent events.

See streaming for details.

Example request

{
  "max_tokens_to_sample": 256,
  "model": "claude-2.1",
  "prompt": "\n\nHuman: Hello, world!\n\nAssistant:"
}

Response

Text Completion object.

completionstring required

The resulting completion up to and excluding the stop sequences.

idstring required

Unique object identifier.

The format and length of IDs may change over time.

stop_reasonstring nullable required

The reason that we stopped.

This may be one the following values:

  • "stop_sequence": we reached a stop sequence — either provided by you via the stop_sequences parameter, or a stop sequence built into the model
  • "max_tokens": we exceeded max_tokens_to_sample or the model's maximum
type'completion' required

Object type.

For Text Completions, this is always "completion".

Example response

{
  "completion": " Hello! My name is Claude.",
  "id": "compl_018CKm6gsux7P8yMcwZbeCPw",
  "model": "claude-2.1",
  "stop_reason": "stop_sequence",
  "type": "completion"
}