Agent Wranglers

AGT-24Recommended

Set quotas per agent grant

Count usage per agent grant, the pair of one user and one agent, as well as per user and per operator. One agent can use at most its share of the user's quota.

How to do it

Agents call far faster than people and retry in loops. Without a limit per grant, one runaway agent uses up the quota that the user's other agents and the user's own browser need. Keep three counters, for the grant, the user and the verified operator from Set a policy for each agent operator. Refuse when any of them is over. Let the user set each agent's share in the account page, with a default such as half the user's quota. Answer an agent over its limit with 429, Retry-After and the headers in Tell clients their rate limit. Tenant-wide fairness is in Give each customer a fair share.

Examples

JavaScript example

export async function withinQuota(redis, { userId, agentId, operator }, limits) {
  const minute = Math.floor(Date.now() / 60_000);
  const keys = [`q:grant:${userId}:${agentId}:${minute}`, `q:user:${userId}:${minute}`, `q:op:${operator}:${minute}`];
  const counts = await Promise.all(keys.map(async key => { const n = await redis.incr(key); await redis.expire(key, 120); return n; }));
  return counts[0] <= limits.grant && counts[1] <= limits.user && counts[2] <= limits.operator;
}

Python example

def within_quota(redis, user_id: str, agent_id: str, operator: str, limits: dict) -> bool:
    minute = int(time.time() // 60)
    keys = [f"q:grant:{user_id}:{agent_id}:{minute}", f"q:user:{user_id}:{minute}", f"q:op:{operator}:{minute}"]
    pipe = redis.pipeline()
    for key in keys:
        pipe.incr(key)
        pipe.expire(key, 120)
    grant, _, user, _, op, _ = pipe.execute()
    return grant <= limits["grant"] and user <= limits["user"] and op <= limits["operator"]

How to check

A test gives one user two agent grants, each with half the quota. The first agent sends calls until it gets 429. The second agent's next call and the user's own next call both succeed.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/agents-as-users/quotas-and-pricing/quota-per-grant">Agent Wranglers agents as users playbook</a>. In code you write, add a comment next to it with the same link.