After this lesson, you will be able to: Call AI APIs directly, implement tool use, streaming, function calling, and structured outputs without any framework.
Frameworks are great until they aren't. When you need cost control, latency optimization, or behavior the framework doesn't expose, you drop to the raw SDK. This lesson covers the patterns frameworks abstract: streaming, structured outputs, parallel tool calls, prompt caching.
Stream tokens as they arrive instead of waiting for the full response.
import anthropicclient = anthropic.Anthropic()with client.messages.stream(model='claude-sonnet-4-6',max_tokens=1024,messages=[{'role': 'user', 'content': 'Write a short poem about the sea.'}],) as stream:for text in stream.text_stream:print(text, end='', flush=True)print() # newline at end
Force the model to return JSON matching a schema, via tool use.
import anthropic, jsonschema = {'name': 'extract_person','description': 'Extract a person from text.','input_schema': {'type': 'object','properties': {'name': {'type': 'string'},'age': {'type': 'integer'},'role': {'type': 'string'},},'required': ['name', 'age', 'role'],},}client = anthropic.Anthropic()resp = client.messages.create(model='claude-sonnet-4-6', max_tokens=512, tools=[schema], tool_choice={'type':'tool','name':'extract_person'},messages=[{'role':'user','content':'Maria is 34 and runs a bakery.'}],)data = next(b for b in resp.content if b.type == 'tool_use').inputprint(json.dumps(data, indent=2))
If you reuse a long system prompt or document across many turns, mark it as cached. Claude charges 90% less for cached content. Add `cache_control: {'type': 'ephemeral'}` to a content block. Lifesaver for RAG and code-context apps.
asyncio = throughput. Critical for production.
import asyncio, anthropicclient = anthropic.AsyncAnthropic()async def ask(question):resp = await client.messages.create(model='claude-3-5-haiku-latest', max_tokens=200,messages=[{'role': 'user', 'content': question}],)return resp.content[0].textasync def main():questions = ['Capital of France?', 'Capital of Japan?', 'Capital of Egypt?']results = await asyncio.gather(*[ask(q) for q in questions])for q, a in zip(questions, results):print(f'{q} → {a}')asyncio.run(main())
Sign in and purchase access to unlock this lesson.