Black Friday Deals For Authors Are On Now!

Leanpub Header

Skip to main content

Claude Agent SDK, Step by Step

Build a GitLab bot in Python: tools, permissions, sessions, hooks, subagents, cost and sandboxing

Claude Agent SDK, Step by Step

19 chapters, every output from a real recorded run. Build a GitLab bot with the Claude Agent SDK for Python, from a ten-line agent to a sandboxed bot that runs all day.

Minimum price

$9.99

$9.99

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
About

About

About the Book

The Claude Agent SDK gives you the agent loop behind Claude Code as a Python library. This book teaches it by building one real project: a bot that lives in a GitLab server you run in Docker, answers @claude-agent mentions, works on the code, and reports back. You start with a ten-line agent and finish with a bot that runs all day, stays inside a sandbox, and tells you what it costs.

You learn how a run works and how to read it; how to give the agent your own tools and what the model sees when a tool fails; how instructions, context and setting sources fit together; how permissions work (rules, modes, the approval callback) and why a rule is not a wall; sessions, resume and memory; structured output; hooks that guard, log and stop; subagents, plugins and skills; parallel sessions; running always on; connecting existing MCP servers; cost and long conversations; sandboxing and deployment; and a Telegram front end.

It starts from zero for Python developers who have called a model API but never built an agent. Each new term is explained when it first appears, with a plain-language picture, a minimal runnable example, a figure and then the formal definition. Every chapter ends with self-check questions (answers at the back) and exercises.

Every printed output comes from a recorded run, and the book shows what went wrong as well as what worked. The recordings were made against DeepSeek's Anthropic-compatible endpoint, only to keep the cost low. Chapter 0 explains how to point the code at the Anthropic API instead by changing ANTHROPIC_BASE_URL, the key and BOOK_MODEL. Claude's own behavior was not recorded, and the book says so where it matters.

Not affiliated with, reviewed by or endorsed by Anthropic, GitLab or Telegram.The Claude Agent SDK gives you the agent loop behind Claude Code as a Python library. This book teaches it by building one real project: a bot that lives in a GitLab server you run in Docker, answers @claude-agent mentions, works on the code, and reports back. You start with a ten-line agent and finish with a bot that runs all day, stays inside a sandbox, and tells you what it costs.

You learn how a run works and how to read it; how to give the agent your own tools and what the model sees when a tool fails; how instructions, context and setting sources fit together; how permissions work (rules, modes, the approval callback) and why a rule is not a wall; sessions, resume and memory; structured output; hooks that guard, log and stop; subagents, plugins and skills; parallel sessions; running always on; connecting existing MCP servers; cost and long conversations; sandboxing and deployment; and a Telegram front end.

It starts from zero for Python developers who have called a model API but never built an agent. Each new term is explained when it first appears, with a plain-language picture, a minimal runnable example, a figure and then the formal definition. Every chapter ends with self-check questions (answers at the back) and exercises.

Every printed output comes from a recorded run, and the book shows what went wrong as well as what worked. The recordings were made against DeepSeek's Anthropic-compatible endpoint, only to keep the cost low. Chapter 0 explains how to point the code at the Anthropic API instead by changing ANTHROPIC_BASE_URL, the key and BOOK_MODEL. Claude's own behavior was not recorded, and the book says so where it matters.

Not affiliated with, reviewed by or endorsed by Anthropic, GitLab or Telegram.

Example code for this book (MIT): https://github.com/airplanecraft/books-claude-agent-sdk

Author

About the Author

Eric Wang
    I write practical books about building AI agents in Python. Every output in my books comes from a real recorded run, and every example ships as runnable code you can follow offline. My book, Deep Agents, Step by Step, teaches LangChain's deepagents library from the agent loop up: planning, files, subagents, human approval, permissions, memory and skills,  ending with a research-assistant project. Example code: https://github.com/airplanecraft/books-deepagents

Contents

Table of Contents

  • Preface
  • How to use this book
  • Chapter 0. Foundations
    • 0.1 Setting up
    • 0.2 Chat models and messages
    • 0.3 Asking for help: the idea
    • 0.4 Tool use by hand: two rounds
    • 0.5 Async and await
    • 0.6 What the Agent SDK does for you
    • 0.7 Recordings, replay and where the answers came from
    • 0.8 Common pitfalls
    • 0.9 Summary
  • Chapter 1. Your first agent
    • 1.1 What you build
    • 1.2 A clerk who runs the errands
    • 1.3 The first run
    • 1.4 The agent loop and the turn
    • 1.5 The message stream
    • 1.6 Built-in tools
    • 1.7 Stopping early: max_turns and the result subtype
    • 1.8 Common pitfalls
    • 1.9 Summary
  • Chapter 2. Custom tools
    • 2.1 What the bot needs
    • 2.2 A form you write yourself
    • 2.3 A tool with four parts
    • 2.4 Serving the tools from your own program
    • 2.5 Optional inputs and JSON Schema
    • 2.6 When a tool fails
    • 2.7 A hint about reading and writing
    • 2.8 Common mistakes
    • 2.9 Summary
  • Chapter 3. Connect to GitLab
    • 3.1 What the bot reads and writes
    • 3.2 Reading GitLab through a REST API
    • 3.3 A stand-in for GitLab
    • 3.4 The access token
    • 3.5 Keeping the token away from the agent
    • 3.6 Prompt injection
    • 3.7 Who may ask, and what a comment may say
    • 3.8 The first answer
    • 3.9 Common mistakes
    • 3.10 Summary
  • Chapter 4. Instructions and context
    • 4.1 What the agent knows when it starts
    • 4.2 The note on the first day
    • 4.3 Starting from Claude Code's own prompt
    • 4.4 A long prompt in a file {advanced}
    • 4.5 The context window
    • 4.6 Setting sources and CLAUDE.md
    • 4.7 The design request
    • 4.8 Common mistakes
    • 4.9 Summary
  • Chapter 5. Permissions
    • 5.1 What the bot may do on its own
    • 5.2 Six questions, in order
    • 5.3 Permission modes
    • 5.4 Asking a person: the approval callback
    • 5.5 Saying no, and saying why
    • 5.6 Allow rules and deny rules
    • 5.7 Rules read words
    • 5.8 Tools that hold the token
    • 5.9 Locking it down {advanced}
    • 5.10 Questions the agent asks {advanced}
    • 5.11 Common mistakes
    • 5.12 Summary
  • Chapter 6. Sessions
    • 6.1 One issue, one conversation
    • 6.2 What a session is
    • 6.3 One session per issue
    • 6.4 A client that keeps the session open
    • 6.5 Continue, resume and fork
    • 6.6 When the system prompt changes
    • 6.7 Going back to an earlier message {advanced}
    • 6.8 Talking to a run that is still going
    • 6.9 Watching it write {advanced}
    • 6.10 Common mistakes
    • 6.11 Summary
  • Chapter 7. State and memory
    • 7.1 What the bot keeps, and where
    • 7.2 Six places
    • 7.3 Auto memory
    • 7.4 Turning it off
    • 7.5 The preset and the tools
    • 7.6 CLAUDE.md, auto memory, GitLab or a file
    • 7.7 A store for the transcript {advanced}
    • 7.8 When the store fails {advanced}
    • 7.9 Common mistakes
    • 7.10 Summary
  • Chapter 8. Structured output
    • 8.1 A comment a program can build
    • 8.2 A form with boxes
    • 8.3 The tool you did not write
    • 8.4 When the data does not pass
    • 8.5 The schema as a class
    • 8.6 From data to a comment
    • 8.7 When it does not work
    • 8.8 Common mistakes
    • 8.9 Summary
  • Chapter 9. Hooks
    • 9.1 Code that Claude Code calls for you
    • 9.2 Hook events
    • 9.3 A first hook: refusing a force-push
    • 9.4 Matchers
    • 9.5 Hook decisions
    • 9.6 When the hook itself fails
    • 9.7 Logging every call
    • 9.8 Stop: after the last answer
    • 9.9 A guard for the token file
    • 9.10 Hooks and the approval callback
    • 9.11 Common mistakes
    • 9.12 Summary
  • Chapter 10. Subagents
    • 10.1 One job, several subagents
    • 10.2 Defining the subagents
    • 10.3 Who did what
    • 10.4 What a subagent knows
    • 10.5 Waiting, two at once, and limits
    • 10.6 Hooks inside subagents {advanced}
    • 10.7 Notes that a subagent keeps
    • 10.8 A mail tool, and who gets asked
    • 10.9 An isolated copy of the repository {advanced}
    • 10.10 More about subagents {advanced}
    • 10.11 Common mistakes
    • 10.12 Summary
  • Chapter 11. Plugins and skills
    • 11.1 Rules that load when they are needed
    • 11.2 A recipe on an index card
    • 11.3 The model asks for the skill
    • 11.4 What the model sees, and when
    • 11.5 Choosing the skills a run may use
    • 11.6 Calling a skill by name
    • 11.7 A plugin: the team's kit in one folder
    • 11.8 Plugins you installed in Claude Code {advanced}
    • 11.9 Skill, subagent or CLAUDE.md
    • 11.10 Common mistakes
    • 11.11 Summary
  • Chapter 12. Parallel sessions
    • 12.1 Three small jobs at once
    • 12.2 One process for each session
    • 12.3 Two sessions in one folder
    • 12.4 A folder and a branch for each session
    • 12.5 Publishing three branches
    • 12.6 Worktrees that Claude Code makes {advanced}
    • 12.7 Common mistakes
    • 12.8 Summary
  • Chapter 13. Always on
    • 13.1 From a command to a service
    • 13.2 Polling
    • 13.3 Webhooks
    • 13.4 Setting up a webhook and running the receiver {advanced}
    • 13.5 The ledger
    • 13.6 Who may start a run
    • 13.7 Waiting for a person
    • 13.8 A time limit and the running loop
    • 13.9 Common mistakes
    • 13.10 Summary
  • Chapter 14. Existing MCP servers
    • 14.1 Tools that other people wrote
    • 14.2 A server on the internet {advanced}
    • 14.3 A server you can read {advanced}
    • 14.4 Did the server connect? {advanced}
    • 14.5 Passing a credential {advanced}
    • 14.6 Whose code runs? {advanced}
    • 14.7 Tool search {advanced}
    • 14.8 Limits, and the GitLab server {advanced}
    • 14.9 Common mistakes
    • 14.10 Summary
  • Chapter 15. Cost, effort and long conversations
    • 15.1 What a bot that runs for weeks has to watch
    • 15.2 Reading the bill {advanced}
    • 15.3 Prompt caching {advanced}
    • 15.4 Effort level {advanced}
    • 15.5 The budget cap {advanced}
    • 15.6 Long conversations and compaction {advanced}
    • 15.7 Common mistakes
    • 15.8 Summary
  • Chapter 16. Sandbox and deployment
    • 16.1 Why a second wall
    • 16.2 Turning the sandbox on {advanced}
    • 16.3 Files {advanced}
    • 16.4 Worktrees and the commit {advanced}
    • 16.5 The token file {advanced}
    • 16.6 The network {advanced}
    • 16.7 When the sandbox cannot start {advanced}
    • 16.8 Leaving the sandbox {advanced}
    • 16.9 Containers {advanced}
    • 16.10 What the agent can see inside a container {advanced}
    • 16.11 Common mistakes
    • 16.12 Summary
  • Chapter 17. Telegram: a chat front end
    • 17.1 A third front door
    • 17.2 The chat gateway {advanced}
    • 17.3 Telegram updates and long polling {advanced}
    • 17.4 Commands, comments and the Telegram loop {advanced}
    • 17.5 Buttons: approving on the phone {advanced}
    • 17.6 Questions on the phone {advanced}
    • 17.7 Trying it on a real bot {advanced}
    • 17.8 Common mistakes
    • 17.9 Summary
  • Chapter 18. Project: one task, many agents
    • 18.1 One task, five lines
    • 18.2 Reading the checklist and cutting the work
    • 18.3 Phase one: two sessions at once
    • 18.4 Phase two: tests and docs from subagents
    • 18.5 Acceptance: facts first, then a verdict
    • 18.6 The e-mail waits for a person
    • 18.7 The ledger, the comment, and the whole run
    • 18.8 Plugging in chapters 14 to 17 {advanced}
    • 18.9 What to add so nobody needs to nod
    • 18.10 What to add before a team uses it
    • 18.11 Common mistakes
    • 18.12 Summary
  • Answers to the self-checks
  • Glossary

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub