Short answer. Budget the task, not the request. Cap steps, cap tokens, and keep state outside the prompt. On a 250K-token daily cap, an eight-step agent averaging 4,000 tokens per step allows about seven tasks a day, and a single 8K-token step can exhaust a free 8K TPM minute.
Why agent loops break free tiers
A single-turn chatbot sends one prompt and gets one answer. An agent sends a prompt, receives a tool call, sends the prompt plus the tool result, receives another call, and repeats. The history grows at every step, so the tenth request contains the first nine exchanges. Even a modest agent can spend 30,000 to 80,000 tokens on one task.
The limits fail in a predictable order. Tokens per minute bind first when a single step is large: Groq's free 8,000 TPM means a 6,000-token prompt plus a 2,000-token completion consumes the whole minute. The daily token cap is the second wall: 200,000 tokens on Groq's free plan is about six tasks at 32,000 tokens each.
Concurrency is the third wall. Plugsky's free plan allows two concurrent requests and new accounts get one, so a four-worker fan-out will 429 even when tokens remain.
A working bounded loop
This loop caps steps, caps tokens, and returns the trace. The registry keeps tool implementations separate from the JSON schemas the model sees.
import json
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["PLUGSKY_API_KEY"],
base_url="https://api.plugsky.com/v1",
)
def search_docs(query: str) -> dict:
return {"query": query, "hits": [{"id": "doc_7", "title": "Refund policy"}]}
registry = {"search_docs": search_docs}
specs = [{
"type": "function",
"function": {
"name": "search_docs",
"description": "Search the help centre. Use for policy questions.",
"parameters": {
"type": "object",
"properties": {"query": {"type": "string"}},
"required": ["query"],
"additionalProperties": False,
},
},
}]
def run_agent(task: str, max_steps: int = 6, token_budget: int = 20_000):
messages = [
{"role": "system", "content": "Use tools when needed. Stop when you can answer."},
{"role": "user", "content": task},
]
used = 0
for _ in range(max_steps):
resp = client.chat.completions.create(
model="plugsky-lite",
messages=messages,
tools=specs,
tool_choice="auto",
parallel_tool_calls=False,
max_tokens=600,
)
used += resp.usage.total_tokens
msg = resp.choices[0].message
messages.append(msg)
if not msg.tool_calls:
return msg.content or "", used
for call in msg.tool_calls:
fn = registry.get(call.function.name)
args = json.loads(call.function.arguments)
result = fn(**args) if fn else {"error": "unknown tool"}
messages.append({
"role": "tool",
"tool_call_id": call.id,
"content": json.dumps(result)[:2_000],
})
if used >= token_budget:
return "Budget exhausted", used
return "Step limit reached", used
The 2,000-character truncation is deliberate. Return IDs and short snippets, then fetch the full document only when the model asks.
State is cheaper than context
Keep the task state in a database or a file, not in the prompt. Send the current step, a short summary of what has been tried, and references to artifacts. If the agent needs a document, give it a document ID and a retrieval tool; do not paste the document into every turn.
Summarize history after four or five turns: the goal, confirmed facts, failed attempts and the next action. That is 200 tokens replacing 4,000, and it usually improves focus.
Store side effects idempotently. If a tool sends an email or creates a ticket, give it a key derived from the task and step so a retry does not duplicate the action. Agents fail mid-loop; the recovery path should not create a second refund.
Concurrency, latency and failure
Tool latency dominates agent wall-clock time. A search that takes two seconds and a database call that takes 300 milliseconds add up across six steps. Parallelize read-only tools, but keep write actions sequential and behind an approval step.
Set a timeout per tool and a deadline for the whole task. If the deadline passes, return the best partial answer and the trace rather than letting the loop run. On a 429, honor retry-after, add jitter, and fail over to a second provider or model only if the response shape is compatible.
| Agent pattern | Steps | Tokens/task | Free-tier verdict | Paid fit |
|---|---|---|---|---|
| Single tool lookup | 2 | About 4K | About 12/day at 50K | Free |
| Research loop | 6 | About 24K | About 2/day at 50K | Hobby |
| Coding agent | 10 | About 80K | 1-3/day at 250K | Builder |
| Multi-agent fan-out | 4 x 5 | About 80K | Concurrency fails first | Scale |
Guardrails that pay for themselves
Cap steps at six to ten, cap tokens per task, allowlist tools, require human approval for writes, and log a trace with the model, request ID, tool name and token count. Measure cost per successful task, not cost per call; an agent that fails twice and succeeds on the third try is more expensive than its per-call price suggests.
If the success rate is below 80%, fix retrieval, tool descriptions or the stopping condition before upgrading. A bigger model on a broken loop burns the larger budget faster.
FAQ
Can I build an AI agent on a free LLM API?
Yes for prototypes and low-volume tasks. The free plan caps steps indirectly through tokens, allows two concurrent requests and excludes the hosted agents feature. Production agents need a paid plan or a self-hosted model.
Why does my agent run out of tokens so quickly?
Every step resends the conversation, including earlier tool results. A ten-step task can cost 60K to 100K tokens even though each individual answer is short. Trim tool output, summarize history and cap the steps.
How do I stop a runaway agent loop?
Set a maximum step count, a token budget per task and a wall-clock deadline. Check usage after each step and return a partial result when a limit is reached. Add idempotency keys so a retry does not repeat a side effect.
When should I upgrade for agents?
When a typical task exceeds 30K tokens, when you need more than five tasks per day, or when two concurrent requests cannot serve your users. Builder is the first plan with 10 concurrent requests and no daily token cap.
Reviewed 20 November 2026. Primary sources checked 20 November 2026: OpenAI function calling, Groq rate limits, Plugsky pricing. Free-tier limits are published ceilings, not guarantees; confirm them in the provider console before capacity planning.