You asked your coding agent to make the app faster, and it came back proud: half the codebase restructured, tests still green, everything "optimized". You merge it, and the app runs exactly as slow as before. That's the standard failure mode of "make it faster" as a prompt, and it costs you a review cycle plus whatever clean abstraction the agent flattened on the way.
The fix is boring and it works: measure before the agent touches anything, hand it the profiler output and one target number, and make it prove the gain by rerunning the same benchmark. It's a few minutes of setup, and it's the difference between an agent that ships speed and one that ships code shaped like speed.
The agent guesses, and the guess gets defended
Without a profile, the agent has never seen your code run. It reads it, pattern-matches against things it has seen, and picks the function with the suspicious-looking loop or the file that happens to be open in your editor. "Faster" with no data means it reaches for the moves it knows: cache this, memoize that, split the function, wrap the loop. Sometimes one of those lands on the real bottleneck. More often it lands on code that was already fine, adds indirection, and makes the next change harder.
Worse is the closing report. The agent describes its change in confident language, because that's how these tools write. Nothing in that paragraph is evidence. If you can't tell it where the time goes, you've handed it a creative writing assignment with a diff attached.
If you're building your whole workflow around agents, there's more on our blog about keeping them useful.
Measure first with tools you already have
You don't need a paid profiler. Every stack has one built in.
Python ships cProfile in the standard library (official docs):
python -m cProfile -s cumtime app.py
The -s cumtime flag sorts by cumulative time, meaning time spent in a function plus everything it calls (sort keys). That usually points you at the part of the program that eats the clock, not the function that's merely called often.
For Node, use the V8 CPU profiler built into the runtime (CLI docs):
node --cpu-prof app.js
That writes a .cpuprofile file to disk when the process exits. You can open it in DevTools, or just list its hottest functions in the agent brief. The older --prof plus --prof-process route works too if you prefer plain-text output.
For a front end, the DevTools Performance panel does the same job for rendering and script time: record the interaction, then read where the time went (Chrome DevTools guide).
Then wrap the thing you care about in a small timing harness. Run it several times and keep the median, because one run is mostly noise. Python's time.perf_counter is the clock to use here (docs):
import time, statistics
def bench():
runs = []
for _ in range(9):
t0 = time.perf_counter()
main() # the thing you're measuring
runs.append(time.perf_counter() - t0)
print(f"median: {statistics.median(runs) * 1000:.0f} ms")
bench()
Write the baseline down
Before the agent enters the story, run the harness and write the number down with its context: machine, input data, date. Something like "median 840 ms on my laptop, bench.py, same test dataset" is exactly the right shape. That 840 is an example placeholder, not a claim about anything: replace it with the number your own run printed.
This one line does two jobs. It makes the improvement checkable later, and it changes how you brief the agent, because "get it under 500 ms" is a different instruction than "make it faster". Put it in the repo next to the harness, so the next session starts from the same page.
The brief: profile, target, constraint
Now the prompt. Include the profiler's top lines, one target, and the guardrails. The file names and numbers are placeholders; swap in yours:
Goal: cut the median runtime of bench.py. Baseline: 840 ms on my
machine (same dataset as before, see BASELINE.md).
Profiling output, top of `python -m cProfile -s cumtime app.py`:
<paste the first ~15 lines here>
Rules:
- Do not change behaviour. The test suite must stay green.
- Only touch code the profile shows on the hot path. If you believe
the bottleneck is elsewhere, say so and stop instead of refactoring.
- When done, run `python bench.py` and paste the before/after median
next to each other. No summary prose.
Two details carry most of the weight. "Paste the before/after" turns the agent's closing report from a story into a measurement. And "stop instead of refactoring" is the clause that keeps it from wandering into a rewrite.
If you don't have an agent wired up yet, you can try the zcode agent harness free (our invite link) and paste the same brief in.
Check the result like a skeptic
Same machine, same input, several runs, median. That's the whole method. Say two identical runs of your baseline differ by 5%: a change that "improved" things by 4% is noise taking a victory lap. Reject it, and either ask for a real fix or revert.
Read the diff too. A good result touches the functions the profiler named, and little else. A diff that spreads across half the codebase is a rewrite dressed as an optimization, whatever the benchmark says, because the next bug you chase will be hiding in it.
And keep the benchmark script in the repo. It's the shared contract between you and the next agent run: rerun this, show me the number. The loop costs a handful of agent runs per experiment, and if you're paying per token, our coupon for 10% off GLM coding plans softens that bill.
The 60-second checklist
Screenshot this, or paste it into your project notes:
[ ] Profiled first (cProfile / --cpu-prof / DevTools), top lines saved
[ ] Timing harness written: N runs, median
[ ] Baseline written down: number, machine, date, script name
[ ] Brief has: profile output, one target number, "tests stay green"
[ ] Agent re-ran the SAME benchmark and pasted before/after
[ ] Same machine, same input, several runs, median compared
[ ] Gain is larger than run-to-run noise
[ ] Diff touches the hot path, not half the app
[ ] bench.py committed for the next round
This is the same instinct as testing the agent's work before trusting it. For the companion move, Test first, then let the agent code (and prove it) covers the other half: making it prove correctness, not just speed.
If this saved you a pointless rewrite, a small tip on WURK is a nice way to say thanks.






Latest comments
0