Exercise 8 of 10 · 0 passed
The Service Layer
Services orchestrate. They sit between your interfaces (CLI, API, bot) and your infrastructure (LLM providers, databases).
A service:
- Accepts interfaces (Protocol types), not concrete implementations — this is dependency injection
- Handles the "what" — call LLM → parse → save → return
- Is ignorant of how storage works or which model is called
Why care? The same ClassificationService powers the cohort's CLI, Telegram bot, and REST API without any changes. Swap MockProvider for ClaudeProvider at the call site — nothing else changes.
Task
Complete classify_and_save(description, amount):
- Build a message asking the LLM to classify the description, requesting JSON
{"category": "..."} - Call
self.provider.complete(messages) - Parse the JSON to extract the category
- Create and save an
Expenseviaself.repo.add() - Return the saved expense
Click "Run tests". First time loads Python in your browser (~5s). After that, runs are instant on this page and ~1s on the others.
Loading tests…
Pass the tests to reveal the reference solution.
The point of the exercise is the struggle, not the answer. Once your tests are green, the reference solution unlocks here so you can compare patterns.
Loading solution…
That's the warm-up.
You ran ten exercises against a mocked SDK. The course builds the real thing: one agent with 150+ pre-written tests, a Telegram bot, FastAPI, Streamlit and Docker. Self-paced, from €99.
Free guide: Beyond the LLM Call
14 pages on the Python system around an AI agent: validation, Protocols, a service layer, human review and a guarded agent loop. Confirm your email to download the PDF, then get the occasional newsletter on building agentic systems in Python. What's inside →
No spam. Unsubscribe anytime.