Building this agent
The agent on this site: grounded only in these pages, every tool call visible, steerable mid-run, on a free-tier budget.
Context
This site is about agent work, so it has an agent on it that a visitor can watch work. Ask it something at /ask and every step is on the page as it happens: the searches it runs, the sections it reads, the sections it cites, and what the answer cost.
It knows only what this site publishes. Its material is the built HTML of the home page, About and the case studies, this one included. Its instructions tell it to answer only from what its tools return and not to guess. /prompt prints them with the four tool schemas and the limits, built from the same module the agent sends to the model. It prints a SHA-256 of the three, asks the running agent for its own, and says whether they match.
It speaks as an agent, about me in the third person. It never claims to be me, and when asked it says it is an agent that reads this site.
I started the build on 2026-10-03 from a written plan, with one implementing agent and one reviewing agent per task.
Problem
- No paid keys. The model runs on Groq’s free plan. Its limits, per model: 30 requests a minute, 1,000 requests a day, 8,000 tokens a minute and 200,000 tokens a day.
- Only the models the key could call. When the build started, the key could call
openai/gpt-oss-120b,openai/gpt-oss-20b,openai/gpt-oss-safeguard-20bandqwen/qwen3.8-27b, besides guard, speech and Whisper models, and no Llama chat model. The agent runs on gpt-oss-120b, with gpt-oss-20b behind it. - No session on the server. One Cloudflare Worker serves the site and the agent, and it keeps no conversation between requests. The browser holds the conversation and sends it with every question.
- 10 ms of CPU a request. That is what Workers Free allows, and the site stays on the Free plan. In the live smoke on staging, an answered turn used 68 ms of the Worker’s CPU, and in the second run every turn used 11 to 40 ms. Nothing was cut off, but Cloudflare stops a Worker that goes over the limit consistently.
- A strict content security policy. No inline scripts, no inline styles. The agent added one origin, Cloudflare’s Turnstile, for the browser check.
- Visible and steerable. Every tool call has to appear on the page as it happens, and the visitor has to be able to stop a run or rule on a call before it runs.
What I built
A corpus from the published pages. At build time a script reads the built HTML of the home page, About and every case study, and cuts it at each section heading. A figure contributes its written description, not its labels. The result, agent/generated/corpus.json, is committed with the pages, and CI fails when the two drift apart. With this page it holds 8 pages in 70 chunks. Each citation chip under an answer links to one of those sections.
Snippets first. search returns up to five hits, each about 60 words around the match. read returns one full section, only when the model asks for it. In the spike, the prompt, the tool schemas and the question cost about 470 tokens a call, and five snippets about 440 more. A turn with one search cost 1,581 tokens on gpt-oss-120b.
Approval mode. With the box ticked, the agent stops before each tool call the model proposes and shows its arguments. The visitor can edit them, run the call or skip it. The decision comes back with the whole conversation, and the agent runs every earlier tool call again to rebuild what the model saw. It never uses a tool result sent by the browser: a browser can send any text, and the model would take it for the site’s own words. Edited arguments pass the same validation as the model’s.
A visible retry. A search with no hit above the score threshold shows in the trace as “retry 1 of 2”, and the model is told to search again with other words. After a third empty search it is told to say the material does not cover it and to call the contact tool, which gives the visitor my email. If the model answers after an empty search without that call, and nothing was found since, the loop makes the call after the answer, with no stop in approval mode, marked AUTO in the trace, and the contact chip follows.
Two model rungs. Each model call goes to gpt-oss-120b first. When that rung is rate limited or fails before any of its text has reached the visitor, the same call goes to gpt-oss-20b. Text already on screen is never taken back: a rung that fails mid-answer ends the turn. A rung that has spent its daily tokens is skipped until the next day. Two upstream failures in a row take the agent offline for 300 seconds.
A daily token cap in D1. Groq’s response headers report requests per day and tokens per minute, not tokens per day. So the agent adds each call’s tokens to a counter per model in D1, Cloudflare’s SQLite database, and stops using a model at 170,000 tokens, below the plan’s 200,000. When every rung is spent, the page says so and the form closes for the day. The same counters hold the cap of 400 requests a day, the hourly limits per connection and the breaker.
A flag becomes a draft eval case. A visitor can flag an answer with a note and, optionally, the answer they expected. The Worker turns the flag into a draft eval case without calling a model: the question, what a right answer must contain (from the expected answer), the sections it must cite (from that turn’s tool calls, run again), and the output that failed. The page shows the case, and D1 stores it with the conversation for me to review.
The turn runs in a Durable Object. The Worker checks Turnstile and the limits and hands the turn to its own Durable Object, which runs the loop and streams back through the Worker. On staging the Worker went from 68 ms on an answered turn to 5 to 14 ms a turn, 7 in the middle; the object uses 10 to 79 ms of the 30 seconds it may. It keeps nothing: it is SQLite-backed because the Free plan offers no other kind. The stream on the page is unchanged.
How I knew it worked
Tests. No test calls Groq or Turnstile: a scripted mock provider stands in for the model.
The unit tests run in Vitest. The runtime tests run the Worker and its Durable Object in workerd, Cloudflare’s runtime, each file against a fresh local D1. The browser tests are 32 Playwright tests, each run at a desktop and a mobile size against a local Worker with the mock provider. The agent’s code in agent/ is 1,778 lines of TypeScript in 17 files, without its tests.
Evals. 33 cases run against the real model every Sunday: /evals.
A spike before the loop. On 2026-10-03 I ran a script against both models and recorded what came back. Three findings changed the design:
- Citation brackets. Told to cite as
[slug#section], gpt-oss-120b wrote 【work/immigration-platform#what-i-built】 and gpt-oss-20b used square brackets. The parser accepts both, and a chip appears only for a section a tool returned in that turn. - One tool call per response. Each call arrived whole, in one chunk. A question that needed two tools got a call to the first only. The loop runs one call at a time and asks the model again after each, and the trace shows each as its own step.
- Queue time on the second rung. gpt-oss-20b queued for 4.8 to 8.4 seconds on every one of 14 calls; gpt-oss-120b for 0.05 to 0.38. So the stats row shows queue time apart from the total, and names the model.
/ask on my machine. A local workerd made no outbound requests, while curl and Node on the same machine could. I had Claude Code add agent/local.ts, which runs the same loop, provider, tools and corpus in Node, without the Worker. The home page’s sample and the measurements below come from it: no Turnstile, D1 or Worker time.Measured with the real model. On 2026-10-05 I ran the four starter questions through agent/local.ts, one question the site cannot answer and one off-topic question: one turn each, approval off, about 40 seconds apart. Times are in milliseconds.
| Question | Model | Calls | Tokens in / out | Total / queue | Verdict |
|---|---|---|---|---|---|
| DWG 02 starter | 120b | 2 | 1,833 / 148 | 2,306 / 440 | Mostly right |
| DWG 01 starter | 120b | 2 | 1,911 / 205 | 6,935 / 5,737 | Partly right |
| DWG 05 starter | 120b | 2 | 1,922 / 121 | 1,001 / 97 | Right, no chip |
| About starter | 120b | 3 | 2,961 / 238 | 2,059 / 482 | Right |
| Favourite language (not on the site) | 120b | 2 | 1,684 / 99 | 1,021 / 135 | Right |
| Banana bread (off-topic) | 20b | 1 | 679 / 43 | 6,006 / 419 | Right, declined |
What was wrong:
- Embellishment. The DWG 02 answer cited the right section and added claims the page does not make, among them a “relational database schema that mirrors the IRCC forms”. The page says v1 of the extension read hand-loaded JSONL files. The About answer, otherwise right, added its own gloss on an employer’s name.
- The wrong mechanism. The DWG 01 answer described brain-remote’s validation gate and atomic commit. It left out the page’s answer: a worktree per write, with remote
mainserializing pushes. Its last sentence also tied two of the page’s requirements to brain-remote. - A citation to nowhere. The DWG 05 answer was right, but its bracket named a section that does not exist. The parser counted it as unknown and showed no chip, so the answer went out uncited rather than with a broken link.
- No hand-off. On the question the site cannot answer, the agent said so and told the visitor to ask me directly. Its search had three hits, so no retry ran. The prompt asks for the contact tool when the tools return nothing relevant, and the model did not call it, so no contact chip appeared. The loop now makes that call itself, but only after an empty search: on staging, an empty search was also followed by an answer without it.
- The second rung answered. The off-topic question was declined in one sentence, by gpt-oss-20b. The first rung failed, likely on the per-minute window after a day of recording, and the switch cost most of the 6 seconds.
What I’d change
No score threshold tells on-topic from off-topic. Search drops hits below a score of 3, or below a quarter of the top hit. That only tells “nothing found” from “something found”. The eval suite’s sweep tried floors from 1 to 40, and none kept the on-topic questions and dropped the off-topic ones: the About starter’s first hit scored 5.31 and an off-topic question’s 5.77, and “best way to cook rice at home” scored 113.55, its first hit this section, which quotes it. So the floor stays at 3. Declining off-topic questions is the prompt’s job, and a question the site cannot answer whose search has hits, like the measured one, gets no contact call from the loop.
Three eval cases are known failing. The suite had 31 cases, in six buckets: factual, citation, refusal, robustness, efficiency and steer. Each gets up to two attempts; deterministic checks decide. The run of 2026-10-07, on GitHub Actions, passed 28, 2 on the second attempt, failed none and counted 3 as known failing: 100% by the pass rate on /evals, passes over passes plus fails. In those three, over the two committed runs, the first rung wrote “full specification” in three of four answers to the DWG 02 starter, mentioned the worktree per write in none of four to the DWG 01 starter, and cited the DWG 06 page as #"", which matches no section, in three of four. In three other answers it wrote “[contact]” as text instead of calling the tool. The first committed run, on 2026-10-06, scored 86%: 25 passed, 4 failed. Before the second I had the checks read a no-break space as a space, loosened one that asked for a fact the answer did not need, and marked two failures known failing, so the two rates do not rest on the same checks. A flag on /ask drafts a case; I review it and add it to the set, which runs every Sunday.
The second rung was slow and less grounded in the spike. Its queue time made a turn on it take 10 to 15 seconds, and it added a sentence the snippets did not support: “These components streamlined the client intake and application submission process”. In the measured off-topic turn it queued for 419 milliseconds. Grounding on that rung is an eval target.
The first rung embellishes too. After the measurements I tried to re-record the home page’s sample: 64 takes of three questions, none faithful to the page. Each added a word such as “full”, “automatically” or “relational”, or a closing sentence that tied the data model to the autofiller. That is a measured limit of the model, not a gap in the corpus, and the evals have to measure it. On 2026-10-05 I moved the sample to the starter question that the About page answers and recorded five takes. One was faithful, and it is the one on the home page. In three takes the agent read this page’s measurement table, which then quoted the starter questions word for word, so a search for a starter’s words found the table. The table now names each starter by the page that answers it.
The free plan is small. At 1,783 to 3,199 tokens a turn, the 170,000-token cap is about 53 to 95 turns a day on each model, and 8,000 tokens a minute is about 2.5 to 4.5 turns a minute. Later turns in a conversation carry up to four earlier turns as history and cost more. The cap is also advisory: tokens are counted after each call, so requests in flight can overshoot it by a turn each, with Groq’s own limit behind it.
The Worker still goes over 10 ms at times. In the smoke after the move, 3 of its 15 turns used 11, 12 and 14 ms on the Worker, and none was cut off. In that smoke (staging, 2026-10-06) the About starter took four model calls and used 12 ms on the Worker, and a one-call approval pause used 14. What runs there now is the guard and the hand-off: the Turnstile check, the limits read and written in D1, the ticket check on an approval decision, and the payload sent to the object. I have not measured which of those costs the most; profiling the guard comes first if Cloudflare’s dashboard ever shows a request cut off for CPU.
Links
- Ask the agent: the agent, with its trace, approval mode and flag.
- Instructions and tools: its system prompt, tool schemas and limits, checked against what runs.