How I Use Claude's Tool Use to Build Self-Correcting Pipelines
Claude's tool use feature is designed to call functions. Most people use it once: call the tool, get the result, done. I use it differently.
I use it to build loops where the model validates its own output and corrects course when something goes wrong.
This is the pattern that catches bugs before they hit your database.
The Problem We're Solving
You're processing documents. Claude extracts structured data. Should be simple.
But Claude hallucinates. It might extract:
- An email field with a phone number.
- A date in the wrong format.
- A price that's negative.
- A customer ID that doesn't exist.
Most teams just save it and debug later. That's expensive.
What if Claude validated its own output and fixed it before you saved it?
The Pattern: Structured Tool Calls with Validation
Here's how we do it in production:
How This Works
- First call: Claude extracts data using the
extract_invoicetool. - Validation: We check if the data is valid (email format, positive amount, date format).
- If valid: Return the data. Done.
- If invalid: Add the validation errors to the message history and ask Claude to fix it.
- Loop: Claude tries again, up to 3 times.
The model sees the errors and adjusts. Most of the time, it gets it right on the second attempt.
Why This Works
Claude understands context. If it gets the email wrong, and you tell it "that's not a valid email," it learns from that feedback and fixes it.
It's deterministic for your business logic. You define what "valid" means (regex, format checks, business rules). Claude handles the fuzzy part (understanding intent) and iterates when it gets the deterministic part wrong.
Reduces downstream errors. Invalid data never reaches your database. You save engineering time debugging garbage data later.
When to Use This Pattern
This pattern works well for:
- Structured extraction: Invoices, receipts, forms, contracts.
- Data enrichment: Validating that a phone number exists, an email is deliverable, a customer ID matches your database.
- Multi-step workflows: Each step has validation. If validation fails, the model reformulates and tries again.
When not to use it:
- When the model's first attempt is 99%+ correct. If validation fails once in a million attempts, the overhead isn't worth it.
- When validation is expensive. If checking validity requires a database call, caching that cost might be higher than dealing with errors downstream.
Production Considerations
Latency: Each retry adds ~1-2 seconds. For batch processing, that's fine. For real-time APIs, keep attempts low (2-3 max).
Cost: Each API call costs. Three attempts = three calls. If extraction is cheap but validation is expensive, maybe accept some errors and handle them in a second pass.
Logging: Log every attempt. Track which validations fail most often. Use that signal to improve your validation logic or your prompt.
Example log:
The Bigger Picture
This pattern scales. Build a validation layer once, apply it everywhere. Your invoice processor, your contract extractor, your customer intake form - all benefit from the same self-correction loop.
Claude doesn't just extract data. It validates and corrects itself. That's the difference between a brittle system and a resilient one.
Build your first self-correcting pipeline. We've used this pattern on invoice processing, customer data extraction, and contract analysis. The validation layer is reusable - build it once and apply it to every extraction workflow you ship.
Get the free AI Readiness Checklist
15 questions to diagnose your team’s AI readiness, where you’ll see ROI fastest, and what to tackle first.
No spam. Unsubscribe anytime.
Ready to build AI that actually works?
Let’s talk about how SRE discipline transforms AI from a risky experiment into a reliable business system.
Book Your Free Discovery Call