Rebuilding our Deterministic Tax Engine for an AI Native World

Sphere is tackling revenue-based compliance globally, which means we need to understand how the sales of goods and services are viewed by every region in the world. How much sales tax can be collected, when and how those sales need to be reported, can the obligation of taxes be pushed onto the purchaser, how much of a purchase can be recovered, how invoices are presented, and other compliance requirements related to selling your products around the world. While TRAM is our AI-native system for understanding and evaluating tax treatments around the world, our deterministic tax engine is what sits in the middle of critical invoice issuance paths and tax filings systems, so it must be fast, highly available, and well, deterministic.
The tax engine processes millions of requests per day with a 5s timeout window, and while our SLAs state three 9s of availability, we like to target five. Bugs related to performance or correctness can result in missed or incorrect taxes, and in extreme cases even block our customers' checkout flow. The tax engine also powers the data underlying our tax filings system, where mistakes can result in fees, penalties, and audit risk for our customers. It also powers a handful of customer analytics features that our customers rely on for reconciling their books and records. Needless to say, this part of our system has no space for AI slop. But with strong software engineering foundations, we can leverage AI-native development practices to move faster without compromising correctness.
Design and Planning
As with any fast growing startup, the initial data model for our tax engine became outdated as we went upmarket and entered more nuanced products and regions. Trying to keep the pace with our customer demands, we added features onto our codebase instead of modeling them in the database, and the system became slower and harder to reason about. It was the right choice for our growth, but it doesn’t change where we ended up. As we were onboarding a customer onto a new product category, we hit an inflection point - do we continue bolting on features and wrangling data into our limited data model, or do we finally rethink the system from scratch, given our more mature understanding of the domain? Critical system overhauls are typically risky projects, but given our understanding of the domain, current abilities of SOTA models and coding agents, the presence of an existing codebase, and a lot of historical traffic to compare against, we had the appetite to take this on. Pre-AI, an overhaul like this would have taken many months, if not years, of development and testing, making it more practical to continue to bolt on features onto the legacy tax engine. AI changed the economics of that decision, allowing us to prioritize long-term quality. We decided to take it.
We started the rebuild with a lot of planning - engineers and our deeply experienced tax team talking about our current state and its shortcomings, as well as the medium term initiatives we wanted to support, alongside the long-term product vision. In between design sessions, we'd use AI agents with access to our internal tax knowledge base as a sounding board to surface gaps in understanding, pick out esoteric details from regions we were unaware about, and draw up diagrams to establish a shared language. The goal was to build a unified high-level model across tax regimes, including SALT, VAT, GST, and others, and then break it up into small, composable modules that can be included or omitted as needed. We designed what we called the Anatomy of a Tax Engine Request, and broke it up into a pipeline of steps with clear boundaries between them. Domain specific steps like "resolving appropriate boundaries", "discovering applicable taxes", and "applying the appropriate rates", as well as performance enhancing steps like "loading tax facts" didn’t just make the system easier for engineers to reason about, they also gave AI agents smaller, well-defined problems to work on. These steps were all happening in the legacy engine, but there weren’t clear boundaries between them, some steps were split across multiple modules, some steps were implicit, and some steps were way too slow.

Building the new Tax Engine
We started with a new database schema and a new code module as a clean slate, and we stored all of our diagrams and conversations in a docs directory in this module, which we (and our AI agents) kept updating as the system evolved. Our new data model kept some tables, removed some tables that did not represent the shape of the problem well, and added new tables directly in the middle of critical relationships that would have made it hard to wire into the live production path without sticking feature-flagged branches of code everywhere. Starting with a clean slate meant we didn’t have to spend extra time debating whether each new table was worth the extra code complexity, we only debated whether it was the right way to model this tax concept. We started by migrating some of the more complicated regions to flesh out our data model, and it exposed some gaps in our initial anatomy. The modular design made it very easy to add, remove, combine and split out modules based on how we saw the data laid out. At this part of the development, engineers and AI agents were working hand-in-hand writing code, rewriting tables, moving things around, and trying things out to see what felt natural. This process also led us to an important insight: evolving our data model from unitemporal (tracking effective dates) to bitemporal (also tracking transaction time) made it significantly easier to debug historical changes. As writing code was not the bottleneck anymore, experimentation was cheap, and having a clean slate made it possible.
In tandem, we also set out to build a way to verify our changes systematically. We built what we called our Offline Shadow Deployment Replayer, inspired by Netflix's Shadow Deployments. The system itself was simple - we stored request/response pairs in S3 in a background thread in our live production pods, and we deployed a separate cluster of pods serving the same code production does, just with the new architecture feature flag turned on. This separate cluster was not wired in to our live traffic path, instead it only served requests kicked off by the Replayer. Engineers and AI agents could then kick off replay runs for changes they made, and review a diff report for anomalies. Of course the diff reports were never clean and had a lot of false positives (and a few regressions), ranging between data shifting in production, rounding errors, and even the new engine being more accurate. Where humans will take shortcuts and try to spot patterns to get through a diff analysis faster (I know in the past I have!), AI agents will happily toil away digging into every diff like its a SEV1, often finding the root cause and implementing a fix before the new code reached production.
Because this system is so critical to what we do at Sphere, we don’t believe a lights-out software factory approach is the right one, at least not yet. Instead, the tracer bullet approach is a better fit: engineers establish the architecture and a working end-to-end implementation (as we did with our initial implementation), and AI agents follow existing patterns for subsequent work. As we improved our data model through the initial implementations, AI agents became very effective at implementing more regions. With the Offline Shadow Deployment Replays, AI agents were successful at finding and fixing regressions before they made it to production. Engineers still reviewed the code, but AI agents waded through a lot of noise to put up PRs that made sense. Had we continued to bolt on our legacy tax engine, there would be no tracer bullet (but rather scattered pellets?) making changes for AI agents harder to get right, and harder for engineers to review.
The challenge is that our domain is difficult, and not all changes are created equal. What may seem like an acceptable change to an agent, actually surfaces a gap in modeling a tax rule. And conversely, a module redesign to fix what seems like a major tax rule, is actually a small edge case that will likely never happen. Human judgement, from engineers working in the system and from tax experts with decades in the industry, is needed to discern between the signal and the noise.

Final Thoughts
It took us 4 weeks to ship our initial redesign of our deterministic tax engine, from design to live production requests. While the full migration of each region took longer than that, the initial foundation was set, and the pattern was laid out for us to guide AI agents in migrating regions. Some key wins from this redesign include:
- New capabilities - an established data pattern allowed us to support more features
- Performance - our slowest requests became 20x faster
- Development speed - a better foundation made for AI-native development
We’ve found that a lot of the concepts that make our system easier for AI agents to navigate are just good engineering practices. Moving business logic into data, building composable modules, establishing good patterns and foundations so that doing that right thing is easier than doing the wrong thing, and building tools to help us test and verify correctness. Establishing that foundation came from years of operating our systems, understanding their shortcomings, and working closely with tax experts who know the domain inside out. AI gave us the ability to act on that knowledge at a pace that previously wouldn’t have been practical. We want to model the world we work in as accurately as possible, and we want to do it in a way that allows us to make the most of the current (and future) AI capabilities.
We’re still solving these problems every day, and if building critical systems without sacrificing quality in an AI-native world sounds interesting to you, visit our careers page!

Ready to see Sphere’s tax engine in action?
Schedule a demo with Sphere today


