Udemy
Turn what you know into an opportunity and reach millions around the world.
Learn More
Your cart is empty.
Keep shopping
Claude API in Python: Build AI Agents and RAG Apps
Role Play
Rating: 4.3 out of 5(945 ratings)
3,484 students

Claude API in Python: Build AI Agents and RAG Apps

Call Claude from Python, then build RAG with reranking, tool use, a bounded agent loop, per-user memory, and MCP
Created byJustin Barnett
Last updated 11/2026
English
Arabic [Auto],English

What you'll learn

  • Set up Python and the Anthropic SDK, make a Messages API call, count tokens, and work out what each call costs
  • Stream replies, retry only retryable errors, and plan for the rate limits and 429 errors on your account
  • Build a RAG pipeline in plain Python: chunk, embed, retrieve from a local vector store, rerank, and get a cited answer from Claude
  • Define tool schemas, use structured outputs and strict tool use, validate inputs against an allowlist, and handle parallel tool calls
  • Write a bounded agent loop that keeps tool history valid, and use Claude's web search tool to produce cited answers
  • Store per-user memory that survives a restart, stays isolated between users, and can be expired, deleted, and filtered for secrets
  • Run a local read-only MCP server from Python, list its tools, block path traversal, and bridge its tools to Claude tool use
  • Practice with 15 in-browser coding exercises, 4 quizzes, a practice test, and an AI role play

Coding Exercises

This course includes our updated coding exercises so you can practice your skills as you learn.

See a demo
Image of coding exercise example

Course content

6 sections • 47 lectures • 4h 22m total length
  • Introduction2:01

    Welcome to Claude API in Python: Build AI Agents and RAG Apps. This is a 2026 hands-on developer path.

    Start here in Introduction: create an Anthropic API key, set up Python, and make your first Messages API call. Then continue through Claude API Overview, AI Application Architecture (including a from-scratch RAG pipeline and lab), Tool Calling with Claude, and Memory. The Memory section includes persistent per-user memory, expiry and filtering, and a local read-only MCP server, with hands-on labs on these three lectures: “Memory that survives restart, and stays per user,” “Expiring, deleting, and filtering memory,” and “A local read-only MCP server.”

    If you already know LLMs and prompting, treat the last section, AI Foundations, as an optional refresher. You can start coding in the next setup lecture, Create Your Anthropic API Key.

    A one-page course map is attached to this lecture. If I sound fast, use the player's playback speed. Turn on captions — captions are available in English (with auto-generated Korean). Post questions in Q&A. Never paste a real API key into a notebook, a question, or a file you might upload. Usage is billed.

  • Familiarize yourself with the Udemy User Interface1:50
  • Create Your Anthropic API Key1:00

    Create an Anthropic API key in the Claude Console and store it only as the ANTHROPIC_API_KEY environment variable. This is the first setup step on the 2026 Messages API path. Never paste a real key into a notebook, lecture file, or Q&A. API usage is billed.

  • Set Up Your Python Environment3:59

    Set up a clean Python environment for the rest of the course in about four minutes: Python, a virtual environment, the Anthropic SDK, and your API key loaded safely, without the key ever appearing in your code.


    You'll learn:

    - Check your Python version: the Anthropic Python SDK needs Python 3.10 or later.

    - Create and activate a virtual environment: python3 -m venv .venv, then source .venv/bin/activate (macOS/Linux) or .venv\Scripts\activate (Windows).

    - Install the pinned SDK so your output matches the course: pip install anthropic==2.12.0 python-dotenv.

    - Keep your key out of your code: the SDK reads ANTHROPIC_API_KEY from the environment. Put it in a .env file (loaded with python-dotenv, and listed in .gitignore) or export it in your shell.

    - Verify everything with check_env.py, which confirms the key is set without ever printing it, then make a first call with first_call.py.

    - If a key ever leaks (a commit, a screenshot, a screen recording), delete it in the Claude Console and create a new one.


    Resources:

    - 11_python_env_setup.zip: check_env.py, first_call.py (add --fake to run offline, no key needed), fake_claude.py, .env.example (placeholder only), .gitignore, requirements.txt, offline tests, and the coding exercise starter.

    - Python_Env_Setup_Cheat_Sheet.pdf: every command, the .env and Python snippets, the key rules, and a 3-question self-check.


    Versions checked 22 Nov 2026 (anthropic 2.12.0, python-dotenv 1.2.4, claude-haiku-5-5). Never paste a real key into code, notebooks, screenshots or chat.


    Next: Coding Exercise "A Key Checker That Can't Leak Your Key" (8 tests, runs in the browser).

  • Coding Exercise: A Key Checker That Can't Leak Your Key
  • Your First Messages API Call6:31

    Every feature in this course, from RAG to agents, is built on one call: the Messages API. In this lecture you make that call, read the reply properly, and work out exactly what it cost.

    You'll learn:

    - The three required parts of a request: model, max_tokens (a ceiling on output tokens) and messages.

    - How to read a reply safely: join the text blocks instead of reading content[0], because Claude 5.x models think adaptively and a reply can start with a thinking block.

    - What stop_reason tells you: end_turn means finished, max_tokens means the reply was cut off.

    - What a token is, and how to count tokens before you send with client.messages.count_tokens (free to call).

    - How pricing works: output tokens cost 5x input tokens on every current Claude model (Haiku 5.5: $0.10 in / $0.50 out per million tokens), and thinking tokens are billed as output.

    - How to write a cheap, reliable classifier: a small max_tokens, thinking turned off, and a fallback label.

    Resources:

    - 08_first_messages_call.zip: hello_messages.py, tokens_demo.py, classify_demo.py, an offline fake client (--fake, no API key needed), tests, and the coding exercise.

    - First_Messages_Cheat_Sheet.pdf: the call, the response fields, the price table, and a 3-question self-check.

    Prices are a snapshot from 22 Nov 2026. Check the Anthropic pricing page before relying on them. Set ANTHROPIC_API_KEY in your shell; never put a key in your code.

    Next: Coding Exercise "Read a Reply and Price It" (8 tests, runs in the browser).

  • Coding Exercise: Read a Reply and Price It
  • Streaming, Errors, and Usage7:10

    Make your first Claude call production-ready with three small upgrades: stream the reply with messages.stream so users see words right away, handle API errors so you retry only what's retryable (429 and 5xx/529, never 400/401/404), and log tokens, cost, latency and request IDs for every call. You'll run stream_demo.py, errors_demo.py and call_logger.py against claude-haiku-5-5, and see simulated 429 and 529 responses with --fake. Download the example code and the cheat sheet in Resources, then write your own retry policy and cost meter in the practice lab.

  • Connect with Me0:53

    Join a private Facebook group called ChatGPT and Gen I for content for enrolled students to connect with instructors and classmates, ask questions, and discuss gen AI topics.

Requirements

  • Basic Python: you can write functions, install packages with pip, and run scripts from a terminal (Python 3.10 or newer)
  • No prior AI or machine learning experience is needed
  • Your own Anthropic API key for the live demos. Usage is billed, and many examples also run offline without a key

Description

This is a hands-on course on building applications with Anthropic's Claude API in Python. You start with an API key, a Python virtual environment, and your first Messages API call. From there you build retrieval-augmented generation (RAG), tool use, a bounded agent loop, and persistent per-user memory. It is aimed at developers who know basic Python and are new to building with LLMs.

What you build and practice:

  • A first Messages API call: read the reply safely, count tokens, and work out what the call cost

  • Streaming replies, retrying only retryable errors, and logging tokens, cost, and latency

  • A rate-limit plan: which per-minute limit your traffic hits first, and how to handle each kind of 429

  • A RAG pipeline in plain Python with no framework: chunk a document, embed it into a local LanceDB table, retrieve, rerank, and have Claude answer with citations

  • Tool calls: tool schemas, structured outputs and strict tool use, input validation against an allowlist, valid tool history, and parallel tool calls with tool errors

  • A bounded agent loop that stops at a step cap and returns an honest partial answer

  • Cited answers with Claude's web search tool, and a research assistant built with tool use

  • Persistent memory under a per-user root that survives a restart, keeps users isolated, and can be expired, deleted, and filtered for secrets

  • A local read-only MCP server over stdio that lists its tools and rejects path traversal, plus an exercise that bridges MCP tools to Claude tool use

You practice with 15 in-browser coding exercises that run automated tests, 4 quizzes, 1 practice test, and 1 AI role play. Several lectures also include a hands-on Udemy lab, including Streaming, Structured Outputs, Parallel Tool Calls, and three Memory lectures. Newer lectures attach a code pack and a one-page cheat sheet. English captions are available.

The course was updated in 2026. Newer lectures use the current Python SDK and Claude Haiku 5.5. Some earlier recordings remain, and where an older video's on-screen code differs, use the updated code pack attached to that lecture. The last section, AI Foundations, is an optional refresher on LLMs, transformers, prompting, evaluating prompts and models, RAG, and MCP. It is mostly conceptual.

You need basic Python (3.10 or newer) and should be comfortable running scripts from a terminal. No prior AI experience is needed. The live demos use your own Anthropic API key, and usage is billed. Many examples also run offline with a fake client, and the Memory and local MCP lectures need no key at all. This course does not cover fine-tuning, multi-agent systems, or deploying to cloud infrastructure.

This course is for Python developers who want to build Claude-powered applications, and for technical leads who want to see how these pieces fit together.

Who this course is for:

  • Python developers who want to add RAG, tool use, agents, and memory to applications with the Claude API
  • Developers new to LLMs who want working code with tests, not just theory
  • Technical leads and project managers who want to see how a Claude-based application fits together