Token Optimization and Script Replay for AI Browser Automation
LLM-driven browser agents often waste tokens by re-reasoning the same steps. This article explains why ReAct-style loops are costly for repeat work, then covers a record-once, replay-forever design: capture trajectories, compile them into scripts, and use fuzzy-point markers so only uncertain steps call the model.
In AI agent applications, using a large language model to drive a browser for web automation is now a common pattern. As tasks grow more complex and run more often, token use can climb quickly and keep operating costs high. This article looks at why that happens and describes an architecture based on trajectory recording and script replay, so developers can cut inference cost without giving up task flexibility.
The ReAct trap: repeated reasoning on the same flow
Most LLM-based automation stacks use a ReAct (Reasoning and Acting) loop to drive the browser. A typical case is logging into an internal system on a schedule and pulling a report.
The run is usually split into small steps:
- The model reasons about the task and opens the browser.
- It reasons again, locates the username field, and types credentials.
- It reasons again and clicks login.
- It reasons again and extracts the report.
Each tool call is wrapped in a full context window and another inference pass. If the same job runs 100 times a day, the system is paying the model to rethink an unchanged procedure every time. Binding LLM reasoning tightly to mechanical browser actions wastes tokens and adds latency.
Record once, automate forever
People think carefully the first time they use an unfamiliar system; later they replay the sequence without re-deciding every click. The same idea can be applied to browser automation: first run with the model, later runs as pure script. The idea is “record once, automate forever.”
The flow has three stages:
- Recording. On the first run, the model understands the goal in natural language and uses ReAct to complete the flow.
- Trajectory capture. While the model talks to the browser, the control server logs every valid action.
- Script replay. Trajectories are turned into executable automation. Later triggers run in a script engine and skip the model entirely.
Intelligence and mechanical execution are decoupled. The model is used for unknown or changing pages; a successful path becomes a deterministic script, so later runs can use zero tokens.
Trajectory data and conversion
A trajectory record must describe more than the action type. It should keep locators, input values, and page context.
A simplified example:
{"step": 1,"action": "browser_input","selector": "#username","value": "admin_user","timestamp": 1698765432000,"page_url": "https://example.com/login"}
After recording, discrete steps are compiled into reusable scripts. Many engines can map this JSON to Playwright- or Puppeteer-compatible code. When a scheduler or message queue wakes the job, the engine runs the steps in order. There is no prompt and no cloud model call, so token use is zero.
Fuzzy-point markers for messy pages
Pure replay is fast but brittle. DOM changes, CAPTCHAs, and multi-option decisions often break classic RPA. Fuzzy-point markers in the trajectory can handle that.
The idea is to mark steps that cannot be fully deterministic. On replay, those steps can pause for a human or call the model only locally.
Markers can be inferred from action type or set by the developer:
# Developer marks a fuzzy pointawait controller.click("#dynamic-report-link",fuzzy_reason="Multiple links share the same name",fuzzy_hint="Choose by report date")
Exported scripts keep this metadata. On replay, a fuzzy point can:
- Pause and alert. Notify operators through logs or monitoring for a human check.
- Call the model on demand. Send a small slice of HTML or a screenshot in a short prompt so only a few tokens are spent on that decision.
The bulk of the flow stays cheap and fast; only exceptions stay flexible.
Engineering and cloud-native deployment
In production, trajectories and scripts can live in object storage for versioning. Replay jobs can run on serverless platforms, triggered by events, so capacity scales with demand.
For high concurrency, container platforms and function compute from providers such as Baidu AI Cloud can host a highly available execution cluster. Heavy model reasoning is limited to first-time exploration and fuzzy points. Routine high-frequency runs stay on a lightweight script engine in the cloud, which can cut compute and inference cost to a small fraction of a fully model-driven design.
Conclusion
Driving every browser step with a full LLM loop is neither cheap nor efficient. A record–generate–replay pipeline, plus fuzzy-point markers for edge cases, balances exploratory intelligence with deterministic execution. That is a practical way to optimize tokens and to take agent-style browser automation into large-scale production.
