3Accessing Claude with the API
Chat exercise
2 min read350 words
Time: 10–15 minutes
Implement the multi-turn chat pattern from scratch and prove it works with a follow-up question that depends on earlier context.
Step 1—Set Up Your Environment
Open a notebook or Python file. Import the Anthropic SDK, call load_dotenv(), and initialise the client. Create an empty messages list that you will grow across turns.
from dotenv import load_dotenv
load_dotenv()
from anthropic import Anthropic
client = Anthropic()
model = "claude-sonnet-4-0"
messages = []
1 / 4
You can now build applications that maintain meaningful conversations with Claude across multiple turns.