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.
ParametersExpand Collapse
max_tokens_to_sample: IntegerThe maximum number of tokens to generate before stopping.
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.
model: ModelThe model that will complete your prompt.
The model that will complete your prompt.
See models for additional details and options.
:"claude-opus-4-6" | :"claude-sonnet-4-6" | :"claude-opus-4-5-20251101" | 19 moreThe model that will complete your prompt.
The model that will complete your prompt.
See models for additional details and options.
Most intelligent model for building agents and coding
Frontier intelligence at scale — built for coding, agents, and enterprise workflows
Premium model combining maximum intelligence with practical performance
Premium model combining maximum intelligence with practical performance
High-performance model with early extended thinking
High-performance model with early extended thinking
Fastest and most compact model for near-instant responsiveness
Our fastest model
Hybrid model, capable of near-instant responses and extended thinking
Hybrid model, capable of near-instant responses and extended thinking
High-performance model with extended thinking
High-performance model with extended thinking
High-performance model with extended thinking
Our best model for real-world agents and coding
Our best model for real-world agents and coding
Our most capable model
Our most capable model
Our most capable model
Our most capable model
Excels at writing and complex tasks
Excels at writing and complex tasks
Our previous most fast and cost-effective
prompt: StringThe prompt that you want Claude to complete.
The prompt that you want Claude to complete.
For proper response generation you will need to format your prompt using alternating `
Human:and
Assistant:` conversational turns. For example:
"
Human: {userQuestion}
Assistant:"
See prompt validation and our guide to prompt design for more details.
metadata: Metadata { user_id } An object describing metadata about the request.
An object describing metadata about the request.
user_id: StringAn external identifier for the user who is associated with the request.
An external identifier for the user who is associated with the request.
This should be a uuid, hash value, or other opaque identifier. Anthropic may use this id to help detect abuse. Do not include any identifying information such as name, email address, or phone number.
stop_sequences: Array[String]Sequences that will cause the model to stop generating.
Sequences that will cause the model to stop generating.
Our models stop on `"
Human:"`, 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: falseWhether to incrementally stream the response using server-sent events.
Whether to incrementally stream the response using server-sent events.
See streaming for details.
temperature: FloatAmount of randomness injected into the response.
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_k: IntegerOnly sample from the top K options for each subsequent token.
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. You usually only need to use temperature.
top_p: FloatUse nucleus sampling.
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. You should either alter temperature or top_p, but not both.
Recommended for advanced use cases only. You usually only need to use temperature.
anthropic_beta: Array[AnthropicBeta]Optional header to specify the beta version(s) you want to use.
Optional header to specify the beta version(s) you want to use.
:"message-batches-2024-09-24" | :"prompt-caching-2024-07-31" | :"computer-use-2024-10-22" | 17 more
ReturnsExpand Collapse
class Completion { id, completion, model, 2 more }
id: StringUnique object identifier.
Unique object identifier.
The format and length of IDs may change over time.
The resulting completion up to and excluding the stop sequences.
model: ModelThe model that will complete your prompt.
The model that will complete your prompt.
See models for additional details and options.
:"claude-opus-4-6" | :"claude-sonnet-4-6" | :"claude-opus-4-5-20251101" | 19 moreThe model that will complete your prompt.
The model that will complete your prompt.
See models for additional details and options.
Most intelligent model for building agents and coding
Frontier intelligence at scale — built for coding, agents, and enterprise workflows
Premium model combining maximum intelligence with practical performance
Premium model combining maximum intelligence with practical performance
High-performance model with early extended thinking
High-performance model with early extended thinking
Fastest and most compact model for near-instant responsiveness
Our fastest model
Hybrid model, capable of near-instant responses and extended thinking
Hybrid model, capable of near-instant responses and extended thinking
High-performance model with extended thinking
High-performance model with extended thinking
High-performance model with extended thinking
Our best model for real-world agents and coding
Our best model for real-world agents and coding
Our most capable model
Our most capable model
Our most capable model
Our most capable model
Excels at writing and complex tasks
Excels at writing and complex tasks
Our previous most fast and cost-effective
stop_reason: StringThe reason that we stopped.
The reason that we stopped.
This may be one the following values:
"stop_sequence": we reached a stop sequence — either provided by you via thestop_sequencesparameter, or a stop sequence built into the model"max_tokens": we exceededmax_tokens_to_sampleor the model's maximum
type: :completionObject type.
Object type.
For Text Completions, this is always "completion".
class Completion { id, completion, model, 2 more }
id: StringUnique object identifier.
Unique object identifier.
The format and length of IDs may change over time.
The resulting completion up to and excluding the stop sequences.
model: ModelThe model that will complete your prompt.
The model that will complete your prompt.
See models for additional details and options.
:"claude-opus-4-6" | :"claude-sonnet-4-6" | :"claude-opus-4-5-20251101" | 19 moreThe model that will complete your prompt.
The model that will complete your prompt.
See models for additional details and options.
Most intelligent model for building agents and coding
Frontier intelligence at scale — built for coding, agents, and enterprise workflows
Premium model combining maximum intelligence with practical performance
Premium model combining maximum intelligence with practical performance
High-performance model with early extended thinking
High-performance model with early extended thinking
Fastest and most compact model for near-instant responsiveness
Our fastest model
Hybrid model, capable of near-instant responses and extended thinking
Hybrid model, capable of near-instant responses and extended thinking
High-performance model with extended thinking
High-performance model with extended thinking
High-performance model with extended thinking
Our best model for real-world agents and coding
Our best model for real-world agents and coding
Our most capable model
Our most capable model
Our most capable model
Our most capable model
Excels at writing and complex tasks
Excels at writing and complex tasks
Our previous most fast and cost-effective
stop_reason: StringThe reason that we stopped.
The reason that we stopped.
This may be one the following values:
"stop_sequence": we reached a stop sequence — either provided by you via thestop_sequencesparameter, or a stop sequence built into the model"max_tokens": we exceededmax_tokens_to_sampleor the model's maximum
type: :completionObject type.
Object type.
For Text Completions, this is always "completion".
require "anthropic"
anthropic = Anthropic::Client.new(api_key: "my-anthropic-api-key")
completion = anthropic.completions.create(
max_tokens_to_sample: 256,
model: :"claude-opus-4-6",
prompt: "\n\nHuman: Hello, world!\n\nAssistant:"
)
puts(completion){
"id": "compl_018CKm6gsux7P8yMcwZbeCPw",
"completion": " Hello! My name is Claude.",
"model": "claude-2.1",
"stop_reason": "stop_sequence",
"type": "completion"
}Returns Examples
{
"id": "compl_018CKm6gsux7P8yMcwZbeCPw",
"completion": " Hello! My name is Claude.",
"model": "claude-2.1",
"stop_reason": "stop_sequence",
"type": "completion"
}