Back to Bytes

Create AnthropicStreamAdapter with AsyncAnthropic client setup — lab audio overview

2026-04-21

Build the foundational AnthropicStreamAdapter class that wraps anthropic.AsyncAnthropic with proxy-based configuration. Define the ChatMessage, StreamChunk, TokenUsage, and FinishReason models that form the unified SSE interface, and initialize the async client with proper base_url and api_key setti

GenAI Application Engineering › Full-Stack GenAI Applications › Chapter 1 · Chat Completion API with Streaming › Implement an Anthropic Claude streaming adapter

5:54
Build the foundational AnthropicStreamAdapter class that wraps anthropic.AsyncAnthropic with proxy-based configuration. Define the ChatMessage, StreamChunk, TokenUsage, and FinishReason models that form the unified SSE interface, and initialize the async client with proper base_url and api_key setti
Share

More from this chapter

Transcript
Host: Welcome back. You're working through Full-Stack GenAI Applications — a course about building real, production-grade AI products, not just demos. This chapter is all about streaming chat. That means showing an AI's response as it's being written, word by word, instead of making the user stare at a spinner for ten seconds. That single capability is what makes a chat product feel alive. Let me bring in our expert to explain why this specific skill matters so much. Expert: Picture this scenario. You're an engineer at a mid-sized company — maybe a legal tech startup with twenty engineers, or a customer support platform serving enterprise clients. Your team has been asked to ship a chat feature powered by AI. Leadership wants it launched in six weeks. Now here's the catch: they don't want to be locked into one AI vendor. They want to use Anthropic's Claude for legal reasoning, OpenAI's models for general chat, maybe Google's Gemini for long documents, and a cheaper open-source option for simple queries. Four different AI providers, each with their own quirks, their own way of streaming data, their own error messages. Without the right foundation, your team ends up with four completely different pieces of code that all kind of work but can't be swapped or tested the same way. What breaks is maintainability. Six months later, nobody on the team can confidently change anything without risking a production outage. The skill you're building today — creating a clean, uniform wrapper around one AI provider so it can later slot into a bigger multi-provider system — is exactly what prevents that mess. Host: So this is your starting point. The chapter overview introduced the big picture of streaming across multiple providers. Now let's get specific. What are we actually building in this first exercise? Expert: You're going to build the foundation piece for talking to Anthropic — the company that makes Claude. Specifically, you're building a wrapper. Think of a wrapper as a polite translator that sits between your application and an outside service. Your app speaks one clean, simple language. The outside service — Anthropic's API, which is just a web address your code sends requests to — speaks its own particular dialect. The wrapper translates between them. Now, the key idea — the conceptual "aha" — is this. You're not just connecting to Anthropic. You're defining a shape. A contract. You're deciding, right now, what a chat message looks like in your system, what a streaming chunk of text looks like, what token usage information looks like, and what the possible reasons are for a response ending — things like "the model finished normally" or "it hit a length limit" or "it was stopped for safety reasons." These shapes you define today will be reused tomorrow when you add OpenAI, and the day after when you add Gemini. So even though you're only wiring up one provider in this exercise, you're actually designing the universal language that all four providers will eventually speak. That's why this lab feels foundational — because it literally is the foundation. Also, because Anthropic's tool for this, called an async client — which just means a client that can handle many requests at the same time without blocking — needs to be set up carefully with a web address and an API key, which is basically a password that proves your app is allowed to use the service. Host: Before someone starts coding — what's the thing that tends to trip people up on this one? Expert: The gotcha is subtle. When your code starts up, it needs to read the API key — that password I mentioned — from the environment, meaning from the computer's settings. If that key is missing, you have two choices. You can let the program crash with a confusing error deep inside the AI library. Or — and this is what you want — you can detect it up front and throw a clean, specific error that says "hey, your Anthropic API key is missing." You'll define your own custom error type for exactly this case. It sounds small, but in production this is the difference between a five-minute fix and a two-hour debugging session at 2 a.m. The tip: check for the missing key before you try to create the client, not after. Fail loudly, fail early, and fail with a message a human can actually read. One more thing — in this course, your code doesn't talk to Anthropic directly. It talks through a proxy, which is just a middle server that forwards requests. So the web address you configure isn't Anthropic's public address — it's the proxy's address. Read that part of the instructions carefully. Host: Let's land this. What will the student walk away able to do, and where does this lead? Expert: After this exercise, you'll be able to take any AI provider's software library and wrap it in a clean, predictable interface that your whole team can rely on. You'll know how to define the data shapes — the messages, the streaming chunks, the usage numbers — that form a universal contract across providers. And you'll know how to handle the boring-but-critical setup correctly: keys, addresses, and missing-configuration errors. This is exactly the kind of building block your team can bring into architecture discussions when someone asks, "how do we stay vendor-neutral?" You'll have a concrete answer. Next up, you'll extend this wrapper to actually do the streaming — you'll write the piece that listens to Anthropic send back tokens one at a time and hands them off to the rest of your application as they arrive. That's where streaming stops being an idea and starts being real. But all of that depends on the foundation you lay right now. So take your time on this one, get the shapes right, and the next two exercises will feel much smoother. Thanks for listening, and good luck.

Want to go deeper? Explore disciplines with hands-on labs, quizzes, and chapter podcasts.