DEV Community

Cover image for Why browser agents waste tokens, and how to stop it
Tisha Chawla
Tisha Chawla

Posted on

Why browser agents waste tokens, and how to stop it

⭐ If you build agents, please star TokenOps on GitHub. It is free and open source, and stars help other developers find it.

Working with @susheem-k on TokenOps.

What is a browser agent?

A browser agent is an AI that uses websites for you. You give it a task, for example "check last night's NBA scores". It opens a browser, reads the page, and clicks, types or scrolls, one step at a time, until it has the answer.

OpenAI, Google, Anthropic and Perplexity all offer one. Browser Use is an open-source library for building them, with more than 120,000 GitHub stars. We used it for all the runs in this post.

Each step costs money

At each step, the agent sends the page and its history to the AI model. You pay for that text. In our runs, one step was about 10,000 tokens.

We asked an agent:

Show the blood relationship fraction between you and your father's mother's sister's son.

Steps Tokens sent Answer
Best run 1 8,291 1/16
Worst run 24 314,924 1/16

Both runs used the same model and gave the same answer. The worst run cost 38 times more. It had the answer by step 4, then searched the same page 18 more times.

Most of the waste we saw came from four problems.

4 ways browser agents waste tokens

1. It repeats an action that does nothing

The agent clicked a button and the page did not change. It clicked again. 25 of its 38 steps were the same click.

Fix: TokenOps sees the same action give the same result, and tells the agent: "You are not making progress. Try something else, or give your answer." If the agent keeps going, TokenOps stops the run.

2. It is blocked and keeps trying

The site showed a "verify you are human" page at step 4. The agent waited, switched tabs and tried other search engines for more than 30 steps.

Fix: the actions changed, but the page did not. TokenOps notices the same page coming back and sends the same note.

3. A long page makes later steps cost more

A normal step was about 10,000 tokens. One step was 50,723, because a full page of sports scores went into the request. It stayed in the history after that.

Fix: when a request gets too big, TokenOps shrinks it before sending. It removes repeated content and keeps the task and the latest steps.

4. It does not know what it has spent

The model cannot tell step 30 from step 3.

Fix: TokenOps adds one line to each request, for example "50% of your budget is used." You can also set a hard limit. TokenOps stops the run before it goes over.

Setup

TokenOps sits between your agent and the AI model, so it sees every step. Your agent's code does not change.

pip install agent-tokenops
Enter fullscreen mode Exit fullscreen mode

Wrap the function your agent uses to call the model. In Browser Use, that is the model you pass in as llm.

from tokenops import tokenops_run
from tokenops.capabilities import wrap_complete
from tokenops.framework import Halt
from tokenops.providers import complete

GOVERNANCE = {"governance": {
    "budgets": [{"id": "run_cap", "limit_micros": 2_000_000, "dimension": "trace"}],  # $2 per run
    "policies": {
        "progress_guard":     {"input_repeats": 3},            # problems 1 and 2
        "context_compaction": {"ctx_max": 100_000},            # problem 3
        "budget_pace":        {"soft_limit_micros": 1_000_000},  # problem 4
        "cost_budget":        {"budget": "run_cap"},           # problem 4: hard limit
    },
}}

with tokenops_run(config=GOVERNANCE, service="browser-agent",
                  provider="openai", model="gpt-4o") as bound:
    governed = wrap_complete(bound.governor, bound.controls, bound.attr,
                             provider="openai", model="gpt-4o",
                             dispatch=complete, service="browser-agent")
    try:
        agent.run(task, complete_fn=governed)   # give the agent `governed`
    except Halt as stopped:
        print(f"run stopped: {stopped}")
Enter fullscreen mode Exit fullscreen mode

If you use Claude Code, run /integrate-tokenops in a copy of the repo to set this up.

It warns before it stops

An agent can be one click from the answer, and stopping it would lose the task. So TokenOps always sends a note first. Test it on a few tasks, check the runs it stopped, and adjust the settings.

Coming next

In the blocked run, the agent wrote "Failure" in its own notes. TokenOps does not read those notes yet. The feat/browser-agent-governance branch adds this:

  • It treats the agent as stuck when its notes say three steps in a row failed.
  • It tells the agent to try another site when it hits a bot check.
  • It asks for the best answer before stopping, so you keep what the agent found.

The branch is not merged yet. We want feedback on it.

Share your runs

Where does your agent get stuck? Tell us in the comments or in this GitHub Discussion.

👉 github.com/theagentplane/tokenops

Top comments (0)