Short, self-contained, copy-paste recipes for automating real work with the Claude (Anthropic) API in Python. Every recipe is one file you can drop into your project — structured output, tool use, streaming, prompt caching, batching, retries, vision, and more. All recipes are linted in CI to use current models and avoid deprecated parameters.
Each file is standalone. Copy the one you need, set
ANTHROPIC_API_KEY, run it.
| # | Recipe | What it shows |
|---|---|---|
| 01 | Call the Claude API in Python | The minimal request/response |
| 02 | Use a system prompt | Steer tone and format |
| 03 | Extract structured JSON with Pydantic | messages.parse → typed object |
| 04 | Tool use / function calling loop | The agentic tool-use loop |
| 05 | Stream a response | Token-by-token output |
| 06 | Adaptive thinking + effort | Opus 4.8 reasoning controls |
| 07 | Prompt caching | Cut cost on repeated context |
| 08 | Count tokens accurately | count_tokens, not tiktoken |
| 09 | Retry on rate limits (429) | Exponential backoff |
| 10 | Batch many prompts (50% off) | Message Batches API |
| 11 | Analyze an image (vision) | Image input |
| 12 | Fast text classification with Haiku | Cheap, single-word labels |
git clone https://github.com/skycandykey1/llm-automation-cookbook
cd llm-automation-cookbook
pip install -r requirements.txt
export ANTHROPIC_API_KEY=sk-ant-... # Windows: setx ANTHROPIC_API_KEY ...
python recipes/01_basic_message.pycookbook/utils.py ships two small, unit-tested helpers you can reuse:
estimate_cost(input_tokens, output_tokens, model)— USD cost from token countsbackoff_delay(attempt)— exponential backoff schedule for retries
Everything is verified offline — no API key, no network:
pip install -r requirements-dev.txt
python -m pytest -qtest_utils.py— unit tests for the helperstest_recipes_lint.py— lints every recipe: approved model id, imports the SDK, has a docstring, and uses no deprecated params (temperature,budget_tokens, ...) or non-Anthropic libraries
MIT — see LICENSE.
💼 Built by
skycandykey1— available for AI agent & automation contract work. I build agents, MCP servers, and LLM automation. →skycandykey456@gmail.com·https://github.com/skycandykey1