Lab: Build and talk to the agent
Assemble the loop, prompt, and tools into a working agent and hold a real conversation with your data.
A terminal agent you can ask real business questions — and watch it call the semantic layer to answer them truthfully.
Step 1 — Get an API key
- 1Go to console.anthropic.com and sign up (or sign in).
- 2Open Billing and add a payment method plus a small amount of credit — $5 covers this course comfortably.
- 3Open API Keys → Create Key. Copy it immediately (it's shown only once) and store it somewhere safe, like a password manager.
- 4Set it in your shell:
export ANTHROPIC_API_KEY=sk-ant-... - 5With the venv active, make sure deps are installed:
pip install -r requirements.txt(that includes the anthropic SDK).
If the key is wrong or unfunded, you'll know on the very first model call: a 401 / authentication_error means the key itself is bad (typo, or not exported in this shell), and a "credit balance is too low" message means billing isn't set up. Either way the fix is in the console, not your code.
Each question is a few thousand tokens across one or two model calls — a few cents per question on current pricing. An afternoon of heavy testing costs a few dollars. Adaptive thinking manages how deeply the model reasons, not the price — so keep max_tokens modest, and switch the MODEL constant to a smaller model for bulk eval runs if you want it cheaper.
Step 2 — Run the agent
cd course-assets/semantic-agent
python3 agent/agent.py
# Percolate analytics copilot. Ask about revenue, CAC, MRR, etc.Step 3 — Interrogate your business
Ask these and watch the [tools: ...] line show it calling the semantic layer:
- "What was total revenue and gross margin?"
- "Which acquisition channel has the lowest CAC?" (it should surface Organic, ~$88)
- "Show me revenue by month in 2025."
- "Compare AOV across channels and tell me which is highest."
- "What's our current MRR and how many active subscriptions?"
Step 4 — Match the shipped shape
If you typed the loop yourself in the last lesson, wrap it in a class before moving on: an AnalyticsAgent whose answer(user_message, history) takes the new message plus the prior history and returns a dict with text, the updated messages, and the tool_calls it made. That's exactly the shape of the shipped agent/agent.py — and it's what the Module 7 eval suite and the Module 8 web app import, so matching it now means everything later plugs in unchanged.
Step 5 — Try to make it fail
- 1Ask for something impossible: "marketing spend by product category." The layer refuses; watch the agent explain the limitation instead of inventing a number.
- 2Ask a vague question: "how are we doing?" See how it picks reasonable headline metrics.
- 3Ask for a metric that doesn't exist: "what's our NPS?" It should say it can't measure that, not fabricate one.
It answers open-ended natural-language questions with governed, correct numbers — and refuses what it can't truthfully answer. That combination is exactly what makes an AI safe to put in front of business users.