November 23, 2026

Building an AI feedback loop for performance improvements

Rubem Nakamura
,
Senior Platform Engineer
@ Appcues

Background

Using Appcues in both your web and mobile applications allows you to deliver beautiful, contextual, targeted messaging to your users - and drive deeper adoption of your products.

Our API is an Elixir/Phoenix application, and SDK traffic runs on its own fleet of nodes. Every time one of our customers' users does something in their product, like opening a page or clicking a button, the Appcues SDK sends that event over a websocket to our API. The API answers with what that user should see right now: which flows, banners, surveys and checklists apply. We call this qualification.

Each published experience becomes a rule: a tree of conditions about the page, the user's properties and segments, the events they performed, and how often they have seen it. For example, "show the upgrade tour to Pro users on the pricing page, once".

Problem to solve

Earlier this year we set out to reduce the compute cost of that fleet. At peak, a single host used about 150 GB of memory, and CPU time grew with every piece of content a customer published. We were fairly sure a good part of that work wasn't needed.

There was no bug to point at. Every feature that touched qualification made sense on its own, and the cost had built up quietly, one reasonable change at a time. It only stood out once we measured the whole path under realistic traffic. The hard part was finding the waste and removing it without changing what a single user sees.

This post is about how we did that, and how much it saved. It's also about how we worked: a tight local feedback loop where AI agents did most of the measuring and people made the calls.

Qualification in a nutshell

On every qualifying event (one that can show content, like a page view), the API loaded the account's published content (already cached), then turned each item into a rule before checking a single condition:

Most of those steps depend only on what was published, not on the event. Only the prefilter and the final evaluation need the event itself.

It wasn't always like this. Rules used to be built once per publish, until a change to how we store published content moved that work onto every event. At the time it was cheap. Then features piled on, each adding a bit more work per rule.

Our feedback loop

Before getting to the fix, here is the part we would reuse on any other problem: the loop we used to find it and test it.

Everything ran in a local environment. The API runs as a named Erlang node, so we could connect to it while it served traffic: flip a feature flag, read a cache, attach a telemetry counter, or sample CPU with :erlang.statistics(:runtime) and :msacc. On top of that we built three things:

  • Seeded accounts. Some had a few to 400 published rules, to see how cost grows. Others were built to hit the risky spots: segments with event triggers or URL conditions, content tied on priority, frequency limits like "show once", dismissed content, and an A/B experiment.
  • great_flood. A tool built in Rust by our Appcues engineering team for load testing. It opens thousands of websockets and sends SDK events built from a catalog of real event shapes, with a fixed random seed so two runs send exactly the same traffic. As with Netflix's replay testing, real shapes reach corners a hand-written happy path never does.
  • Logs as a signal. After every run we scanned the API logs. An unexpected line was often the first sign that something was off, in the code or in the experiment.

AI agents drove most of the loop: they wrote the seeds, ran the floods, read the numbers and reviewed each other's work. It worked because every task ended in something they could run. Anthropic's guide to building effective agents calls this getting "ground truth" from the environment, and research shows models struggle to correct themselves without outside feedback. So "double-check your work" was never the check. A benchmark, a flood or a diff was.

A full round takes minutes, and as Martin Fowler says about continuous integration, rapid feedback is the whole point. To check the first prototype of the fix (more on it below), we sent 60,000 page views per run with the cache off, on, off and on again, flipping the flag at runtime on the same node. It took about 15 minutes, and both times the cache cut CPU per event by about 20%.

‍

The loop kills bad ideas just as fast. The top recommendation of a smaller agent audit, before the tournament below, was to add expiry times to some in-memory caches. One local run showed the change flooding a single process with messages, burning a full CPU core without evicting anything. We dropped it, instead of falling for what Brendan Gregg calls the "drunk man anti-method": changing things at random until the problem goes away.

Investigation

We started the investigation with an agent tournament. Instead of guessing where the CPU went, 25 agents each audited the code and proposed ways to save it. The proposals competed in rounds, other agents tried to prove the survivors wrong against the code, and what was left was measured locally. Caching the compiled rules came out on top.

[IMAGE: agent-tournament-diagram.png]

The agents estimated it would cut up to 20% of the CPU a typical account spends on an event. Measuring showed it was better than that: the final design cut about half.

To see why, we watched the node while the benchmarks ran, a habit known as active benchmarking. The cost of rebuilding grew with every published rule, while reading from a cache stayed flat. So the more rules an account has, the bigger the win: reading the rules was about 17 times faster than rebuilding them with 10 rules, and about 100 times faster with 200.

[IMAGE: rule-build-cost-chart.png]

There was a second, sneakier cost. Our in-memory cache sits on ETS (Erlang Term Storage), and every ETS lookup copies the stored data into the process that reads it. So every qualifying event also copied the content of everything the account had published, even though only the few items that qualified would be sent back.

Memory got the same treatment, and measuring overturned our first guess. An early audit blamed in-memory caches that never expire. A snapshot of a production node showed those caches held under 2% of its memory. More than half sat in the websockets themselves, in the compression and network buffers that each connection keeps, and a node holds well over a hundred thousand connections.

Solution

Memory came first, in two steps. Shrinking the compression buffers each websocket holds helped bring peak memory per host from about 150 GB to about 125 GB. Then an objection came up in review: idle websockets should cost almost nothing, because the Erlang VM would garbage-collect them. Measuring said otherwise. Garbage collection freed about 3% of what an idle websocket held, while closing it gave back over 90%. So the SDK now closes a websocket after 20 idle minutes. That cut peak memory per host by another third, to about 80 GB.

For CPU: compile once per publish, read many times. The first event after a publish builds the account's rules and caches them, and the next publish clears them. Cache invalidation is one of the two hard things in computer science, so we reused the invalidation our published content already relies on.

Three decisions shaped the design:

  • Cache only the rules. Content is fetched just for the rules that qualify, so each event copies about 60% less data.
  • Rebuild once per publish. When many events miss the cache at once, one process rebuilds and the others wait.
  • Keep a fallback. If the cache can't be read, the event is served the old way.

Proving it changed nothing

A faster qualification that shows the wrong flow to the wrong user is a bug, not an optimization. So the real question was "is the answer exactly the same?"

The agents built a parity harness, in the spirit of GitHub's Scientist. Each parity check sends the same request three times: cache on, off, and on again. The responses are compared byte for byte, including content, order, experiment groups and checklists. Twitter's Diffy uses a mirrored version of this idea, comparing a new version against two copies of the old one to tell real differences from noise. Our repeated run also proves the answer holds once the cache is warm.

The results: 29 scenarios, 0 differences, and about 30,000 load test events with 0 failures, including a run that published and unpublished a flow in the middle of it.

Along the way the agents found real bugs, some in the new code and some that had been in production for a while.

The loop caught its own mistakes too. Once, the harness reported a match because both sides returned the same error. Now that counts as a failure.

And the agents didn't get the last word. Models tend to guess confidently rather than admit uncertainty, and a reviewer asked to find problems will always find something. When one added an extra fallback, we asked why. Its answer: "I can't show that the scenario is real." We took it out.

Rolling it out

The cache shipped behind a runtime setting: on for every account, on for a list of accounts, or off. Off is the kill switch, what Pete Hodgson calls an ops toggle.

The code merged with the cache off in production. Staging went first. Then we turned it on in production for the 20 accounts that send the most events, and watched events served from the cache, rebuilds per publish and fallbacks. Next we turned it on for everyone. Once it proved stable we removed the setting, and the old path stays only as the fallback.

Results

In our local environment, qualifying events needed 1.9 times less CPU at 50 rules and 2.4 times less at 200 rules or more. In a qualification-only benchmark with 32 concurrent requests, one node handled about 1.75 times as many events per second.

[IMAGE: cpu-per-event-chart.png]

Under realistic test traffic, the typical request stayed about the same, since most of its time goes to writing events, not qualifying. The 8% on the median in the chart below is smaller than the gap between two runs of the same code. The gain showed in the slowest requests: the slowest 1% (the p99) got about 1.6 times faster.

[IMAGE: flood-latency-percentiles-chart.png]

In production, the fleet adds and removes hosts based on CPU, so the number of hosts is the clearest signal. Two weeks after the cache went on, the fleet averaged about 13% fewer hosts than the week before it, and the daily peak was about 13% lower too, while the volume of events stayed the same.

[IMAGE: sdk-fleet-hosts-chart.png]

Savings 🤑

Weekly EC2 compute spend in October was 40% to 44% lower than in late August.

[IMAGE: weekly-compute-cost-chart.png]

The cache didn't do that alone. Two other changes helped:

  • Cheaper instances. With peak memory down, the fleet no longer needed memory-optimized instances. In early September we moved it to general-purpose instances with half the memory, about 20% cheaper each.
  • Fewer hosts, before the cache. In mid-September the fleet started running about 20% fewer hosts for the same traffic. Other optimizations to the same path shipped that week, outside this post, so we don't count that step as the cache's.

By our estimate, about 40% of the saving came from the memory work, through the cheaper instances, and the cache's share is somewhere between a fifth and a third. These are estimates read off weekly totals, not a controlled experiment. One more caveat: part of our compute is prepaid through a savings plan, so some of the saving only reaches the bill when that commitment is renewed.

Lessons learned

An AI agent loop with constant feedback is a great way to improve performance. On their own, agents guess, and people can't run every experiment. Put agents in a loop where every idea ends in a measurement, and you get both: many experiments, and only the ones backed by data survive. Here, the loop confirmed an idea the agents estimated at up to 20% less CPU, then pushed it to about half, without changing a single answer in our parity tests.

Make the loop fast enough to run all day. When a full experiment takes 15 minutes, you run it again instead of arguing about it. Bad ideas died quickly, and the cache had a lot of evidence behind it before it reached a single customer. It's the same reason DORA (DevOps Research and Assessment) recommends working in small batches.

Seed for the edge cases. Some behavior only shows up in rare setups, like a segment that contains an event trigger or a URL condition. No happy-path test would hit it, so we built an account that does and checked it on every run.

Let agents measure, and let people decide. Agents are tireless at rerunning an experiment, reading logs, or reviewing a branch from several angles, and agents challenging each other catch more than one alone. They are also confidently wrong now and then. Kent Beck describes the same split in augmented coding: fewer boring decisions for the human, and more consequential ones.

If your service does the same expensive work over and over, you don't need a big rewrite. Give a team of agents a local environment, well-chosen seed data and a loop that measures every idea, keep the decisions with people, and it goes a long way!

Further reading

‍

Rubem Nakamura
Senior Platform Engineer
@ Appcues