TL;DR
I built a Claude Code subagent that takes a sales CSV and runs a full ABC XYZ analysis automatically. Same output format every time, no manual prompting. Here's the exact setup, what broke, and what each of 5 prompt rewrites actually changed.
The problem I was trying to solve
In my last post I walked through ABC XYZ analysis manually - spreadsheet formulas, manual sorting, copy-pasting results between sheets.
It worked. But I do this every few months. And every time I had to:
Reformat the data to match what I needed
Rewrite the same analysis logic from scratch
Get slightly different output depending on how I phrased things that day
I wanted to drop a CSV file, say "analyze this", and get back a consistent report. Same structure, same thresholds, every time.
Claude Code supports subagents - specialized agents you configure once and call on demand. That was the right tool.
What is a Claude Code subagent
Claude Code runs Claude directly from your terminal with access to your local files. Inside it, you can create subagents - agents with their own system prompt and tool access, stored as markdown files in .claude/agents/.
The main Claude automatically routes tasks to the right subagent based on the task description and the agent's description field.
You write the agent once. After that you just describe what you want, and Claude Code figures out which agent to use.
The agent file
Here's what the final version looks like:
name: abc-xyz-analyst
description: "Analyzes sales data using ABC XYZ segmentation. Use when given "
a CSV or table with monthly sales figures and asked to segment products or
customers by revenue and demand stability.
tools:
- Read
- Write
You are an analytics specialist. When given sales data, run a full ABC XYZ
analysis.
ANALYSIS RULES:
- Sort all items by total revenue (descending)
- Calculate each item's share of total revenue
- Calculate cumulative revenue share
- ABC groups: A = cumulative up to 80%, B = 80-95%, C = 95-100%
- XYZ: calculate coefficient of variation (stdev / mean) per item across months
- X = CoV under 0.10, Y = 0.10-0.25, Z = over 0.25
OUTPUT FORMAT:
- Full table: Item | Total Revenue | Revenue % | Cumulative % | ABC | CoV | XYZ | Segment
- Key findings - max 5 bullet points, specific observations only
- Recommendations per segment - concrete next actions, not generic advice
- Data issues section - flag missing values, inconsistent format, outliers
IMPORTANT: Do not guess when data is unclear. State your assumptions explicitly.
To call it:
Just describe the task - Claude Code handles routing
"Analyze sales_q1.csv using ABC XYZ analysis"
Claude Code reads the description field, matches it to the task, routes to the subagent. The agent reads the file, runs the analysis, writes a report.
What actually worked
Routing is reliable. Once the description field is specific enough, Claude Code consistently picks the right agent without me naming it. I just say "analyze this sales data" and it goes to the right place.
Consistency. This was the whole point - same thresholds, same table structure, same section order every single run. No more "did I use 75% or 80% as the A cutoff last time?"
Catches things I rush past. The agent applies the same rules to every row without getting tired. Running this on a 40-product catalog, I had skimmed over a BZ item that was borderline AZ - the agent flagged it explicitly. Not because it's smarter, just more consistent.
What didn't work
Data format assumptions. The agent expects one column per month. Give it annual totals, weekly data, or a transposed table - it either errors or silently produces wrong numbers. I added data validation instructions to the prompt but it's still fragile with unusual formats.
The "why" gap. The agent tells you a product is AZ - high revenue, unstable demand. It cannot tell you if that's seasonality, a competitor launch, or a one-off campaign from 8 months ago. I expected the recommendations to be more diagnostic. They're not - they're prescriptive based on the segment, not the cause. That context still has to come from you.
Early recommendations were useless. Version 1 returned things like "Group AZ requires further investigation." That's not a recommendation. Fixing this took two rewrites.
The 5 rewrites - what actually changed
v1: "Run ABC XYZ analysis on the data"
Result: inconsistent thresholds, no defined output format,
sometimes skipped the XYZ part entirely
v2: Added exact thresholds (80/95 for ABC, 0.10/0.25 for XYZ)
Result: thresholds consistent now, but output format still varied
v3: Added explicit output format with column names
Result: table was consistent, but recommendations section was vague
v4: Added "concrete next actions, not generic advice"
Result: recommendations improved, but agent would guess when
data had gaps instead of flagging them
v5: Added "do not guess - state assumptions explicitly"
Result: this is the version I'm using now
Every rewrite was triggered by something specific that annoyed me in the output. I didn't plan any of this upfront - I ran it, saw what was wrong, fixed that one thing.
The thing I underestimated
Writing a good agent prompt is harder than it looks.
Claude does exactly what the prompt says. Not what you meant - what you wrote. "Run ABC XYZ analysis" sounds clear to a human who knows the method. To Claude it's ambiguous: which thresholds? What output format? What to do with missing data?
Every vague instruction in the prompt becomes a source of inconsistency in the output. The technical setup (creating the file, placing it in the right directory) took maybe 10 minutes. Getting the prompt right took a few hours across those 5 iterations.
What's next
Adding a comparison mode - pass two CSV files for different time periods, get back a diff showing which products changed segments and in which direction. A product moving from AX to AZ between quarters is more interesting than its current classification alone.
Tags: #ai #claude #automation #tutorial #productivity #beginners
Top comments (0)