Uzair Rather
← Writing

6 min read

When Filters Stopped Being an LLM Job

We let a model decide what our filters meant, and pulling it out ended up rewriting how the whole product reads data.

Wrong enum, no explanation

The enum values were wrong. Context from the question went missing somewhere between what the person typed and the query that ran. And when a filter came out wrong, nobody could see why it had come out that way.

That was our filtering in early spring. A small, fast language model read the request and turned it into query behavior. I’d thought that was a smart place for a model. People don’t ask for data in typed parameters. They mention a date, a status, a person or a project in whatever words come to mind, and turning messy language into structure is what these models are good at. Letting the model shape the filter meant we didn’t have to write every variation into code first.

What I’d missed was that a filter decides which rows are in the answer. I’d handed a correctness job to a component whose decisions we couldn’t inspect. A fluent interpretation isn’t the same as a faithful query.

Taking the model out of the WHERE clause

In April I removed the model-driven filter engine. Structured parameters and a deterministic query builder took its place, with timing instrumentation around the new path. The team had to spell out what each filter meant, which was more work up front. In return, the same input now led to the same query, for reasons anyone could read.

If you’ve ever debugged a filter a model wrote, you know the hard part is working out which answers were affected and why. With a query builder, a bad filter is a bug with a line number. You fix it once and it stays fixed.

It started as a filter fix. It didn’t stay one. Once the model wasn’t deciding the query, the obvious next question was why other parts of the analytics path still leaned on it: retrieval drift, KPI math written by the model, reads coming from several sources, and result pages that could shift underneath you.

The retrieval path had the same problem

Our data path paired vector retrieval with model synthesis. One general mechanism found relevant material and then the model turned it into an answer, KPI calculations included. That’s an attractive design until you need the number to be right every time. Accuracy depended on what retrieval happened to return and on math the model did in its head. Drift, cost and latency came with it, and so did a weak audit trail. When a KPI looks off, you need to know which rows and which formula produced it, and math the model did on retrieved text can’t tell you that.

I decided to replace that path with direct, typed queries over curated database views. We looked at keeping retrieval, and at a hybrid where text stayed on retrieval and KPIs moved to SQL. We rejected both. Direct queries became the main analytics path.

This didn’t mean taking language models out of the product. It meant moving them off jobs where we already had real structure. A model is useful for understanding what someone is asking and for explaining the result. It’s a poor stand-in for a typed filter, a known measure or a query plan.

It cost us. Accepting the decision meant accepting a reversal measured in weeks, and walking away from work already invested in retrieval and synthesis. Defending that investment would have been the easier conversation.

One way to read the data

By early May the problem was bigger than retrieval. Reads were split across the product’s own APIs, retrieval, and paths built for one capability at a time. Two similar questions could reach two different versions of the business logic. A correct filter sitting on top of inconsistent reads would only make one slice of the answer trustworthy.

I locked an architecture around layered database reads, tenancy scoped to each request, permission resolution, evaluation-first delivery, and session focus. We planned the cutover one capability at a time instead of as a single big swap, so the team could move onto one substrate without losing control of the migration.

Three days later, project and time capabilities switched their reads, and everything that consumed tenant configuration, to a shared request context. Reads that had been spread across APIs, local data and per-capability database paths now came through one route.

I made that cutover because two live paths meant two possible answers to the same question. Running old and new side by side would have felt safer for a while. It would also have kept two definitions of the same answer alive indefinitely, so the team migrated the agents and every tenant-config consumer instead.

The database wasn’t what made it trustworthy. Curated views, typed templates, request-scoped tenancy and one route you could test were. Raw SQL without those would only have moved the opacity from the model into the query.

Page two isn’t always page two

Pagination turned out to be the same problem in a smaller box. Sending full result sets overwhelmed the model. Offset-based windows got slower the deeper you went, and they shifted when records changed between requests. The second page wasn’t guaranteed to be the continuation of the first page the user had just read. If a record lands while someone is paging through a list, an offset window can skip a row or show one twice, and the model has no way to notice.

I chose keyset pagination, tied to the query’s fingerprint, its sort values and the primary key. We rejected offset pagination and per-session snapshot tables. Large result sets got a stable way to continue, and the model no longer had to swallow everything at once.

If that sounds like ordinary backend engineering, it is. That was the point. Putting an AI in front of structured business data doesn’t make the old database worries go away. Query identity, stable ordering, payload size, concurrency and tenant scope all end up deciding whether the answer is right.

The schema moved under us

In mid-May, the data platform underneath changed shape. The newer schema closed some important data gaps, but it broke assumptions baked into our existing migrations. The naming changed, and some of our mapping assumptions no longer lined up.

I assessed the new shape and rewrote our data rules to match it. The team rewrote migrations, data movement, agent code, drift checks and tests to match. The change went well past the edge, into every place that had assumed the old mapping.

The tempting move was a translation layer around the old design. But we’d just committed to one read path, and carrying the old mapping forward would have quietly undercut it. The schema change forced us to finish the job instead of hiding it behind compatibility code.

What the model still does

By the end of May, four kinds of guesswork were gone from the read path. Filters were typed parameters. KPI numbers came from direct queries instead of retrieved fragments. Reads came through one database-backed route. Long result sets paged with stable cursors.

The model still reads what a person meant and helps explain the answer. It no longer gets to invent the mechanics of a filter or do KPI math from whatever retrieval handed it. The team built explicit paths for those jobs because getting the number right, and being able to show how, mattered more than the flexibility of the first design. Status values, tenant scope, pagination and measure calculations only looked ambiguous because we’d put off writing their rules down.

A question could still start in plain language. Everything under it was getting less conversational on purpose. That raised the next question: with one read path in place, what should each layer of it promise the one above?