After this lesson, you will be able to: Use AI APIs in code: send requests, stream responses, manage tokens and costs.
Prompts in a chat UI are the start. Building products means using the API. This lesson covers Anthropic and OpenAI APIs end-to-end.
Get an API key (Anthropic console or OpenAI platform). Auth via Bearer header. Send POST with JSON. Get JSON response. Same shape for both providers (mostly).
Install with `pip install anthropic`:
import osfrom anthropic import Anthropicclient = Anthropic(api_key=os.environ['ANTHROPIC_API_KEY'])response = client.messages.create(model="claude-sonnet-4-6",max_tokens=1024,messages=[{"role": "user", "content": "Explain RAG in 3 sentences."}],)print(response.content[0].text)
Stream tokens to user as they come:
with client.messages.stream(model="claude-sonnet-4-6",max_tokens=1024,messages=[{"role": "user", "content": "Tell me a story."}],) as stream:for text in stream.text_stream:print(text, end="", flush=True)
1. Token counting, set max_tokens conservatively.
2. Rate limits, implement exponential backoff.
3. Caching. Anthropic prompt caching cuts cost on long system prompts.
4. System prompt vs user message, system is for instructions, user for input.
5. Logging, store prompts + responses for debugging + evals.
6. Never put API keys in client code.
Sonnet is ~5x cheaper than Opus. Use the smallest model that works. Use prompt caching for static system content. Aggressive max_tokens. Streaming = perceived latency, not real cost reduction.
Sign in and purchase access to unlock this lesson.