How to use stop sequences in Claude API
stop_sequences parameter in the client.messages.create call with the Anthropic SDK to specify one or more strings where Claude should stop generating text. This parameter accepts a list of strings, and generation halts when any of these sequences is encountered.PREREQUISITES
Python 3.8+Anthropic API keypip install anthropic>=0.20
Setup
Install the Anthropic Python SDK and set your API key as an environment variable before running the code.
- Install SDK:
pip install anthropic>=0.20 - Set environment variable:
export ANTHROPIC_API_KEY='your_api_key_here'(Linux/macOS) orset ANTHROPIC_API_KEY=your_api_key_here(Windows)
pip install anthropic>=0.20 Step by step
This example demonstrates how to use the stop_sequences parameter to stop Claude's output when a specific token or phrase is generated.
import os
import anthropic
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
response = client.messages.create(
model="claude-3-5-haiku-20241022",
max_tokens=100,
system="You are a helpful assistant.",
messages=[{"role": "user", "content": "Write a short poem about spring."}],
stop_sequences=["\nHuman:"]
)
print(response.content[0].text) Write a short poem about spring. Spring wakes the earth with gentle breeze, Soft petals dance on blooming trees, Birds sing songs of bright new days, Life returns in vibrant ways.
Common variations
You can specify multiple stop sequences as a list to halt generation on any of them. The stop_sequences parameter works with all Claude models. For asynchronous usage, use asyncio with the Anthropic client. Adjust max_tokens and system prompts as needed.
import os
import asyncio
import anthropic
async def main():
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
response = await client.messages.acreate(
model="claude-3-5-haiku-20241022",
max_tokens=100,
system="You are a helpful assistant.",
messages=[{"role": "user", "content": "Explain photosynthesis briefly."}],
stop_sequences=["\nHuman:", "\nAssistant:"]
)
print(response.content[0].text)
asyncio.run(main()) Photosynthesis is the process by which green plants use sunlight to convert carbon dioxide and water into glucose and oxygen, providing energy for growth.
Troubleshooting
If the model does not stop where expected, verify your stop_sequences exactly match the output tokens including whitespace and punctuation. Increase max_tokens if the output is cut off prematurely. Check for API errors or rate limits in your environment.
Key Takeaways
- Use the
stop_sequencesparameter to control where Claude stops generating text. - Pass a list of strings to
stop_sequencesto specify multiple stopping points. - Ensure stop sequences exactly match expected output tokens including whitespace.
- The Anthropic SDK supports both synchronous and asynchronous calls with stop sequences.
- Adjust
max_tokensto avoid premature truncation of responses.