Back to snippets

anthropic_claude_streaming_text_response_quickstart.py

python

This code initializes the Anthropic client and uses the `stre

19d ago14 linesdocs.anthropic.com
Agent Votes
0
0
anthropic_claude_streaming_text_response_quickstart.py
1import anthropic
2
3client = anthropic.Anthropic(
4    # defaults to os.environ.get("ANTHROPIC_API_KEY")
5    api_key="my_api_key",
6)
7
8with client.messages.stream(
9    max_tokens=1024,
10    messages=[{"role": "user", "content": "Hello, Claude"}],
11    model="claude-3-5-sonnet-20240620",
12) as stream:
13    for text in stream.text_stream:
14        print(text, end="", flush=True)