← All patterns

Cost-Bounded Research

Deep Research

The Problem

Open-ended agent tasks — research, analysis, discovery — have no natural stopping point. Without a cost cap, a runaway loop or unexpectedly large input can consume an unbounded amount of tokens. You need a hard ceiling that the system respects, not a soft advisory that gets ignored.

The Pattern

Task starts
     │
     ▼
┌─────────────────┐
│ budget tracker  │  $0.00 / $2.00
└────────┬────────┘
         │
         ▼
    Agent executes
         │
    Budget check
    after each step
         │
     ┌───┴────┐
     ▼        ▼
  Under     Over budget
  budget        │
     │          ▼
     │    Halt gracefully
     │    Return partial results
     ▼    + budget_exhausted: true
  Continue

When to Use

When NOT to Use

Code Example

import { QueueAI } from "@queueai/sdk"

const queue = new QueueAI({ apiKey: process.env.QUEUEAI_API_KEY })

const task = await queue.submit({
  name: "deep-research",
  input: {
    question: "What is the competitive landscape for AI orchestration tools?",
    depth: "comprehensive",
  },
  model: { primary: "anthropic/claude-opus-4-8" },
  budget: {
    maxTokens: 100_000,
    maxCostUsd: 2.00,
  },
})

const result = await task.result()
if (result.budgetExhausted) {
  console.log("Partial results returned — budget was hit")
}
from queueai import QueueAI

queue = QueueAI(api_key=os.environ["QUEUEAI_API_KEY"])

task = queue.submit(
    name="deep-research",
    input={
        "question": "What is the competitive landscape for AI orchestration tools?",
        "depth": "comprehensive",
    },
    model={"primary": "anthropic/claude-opus-4-8"},
    budget={"max_tokens": 100_000, "max_cost_usd": 2.00},
)

result = task.result()
if result.get("budget_exhausted"):
    print("Partial results returned — budget was hit")

See it in action: The budget.maxCostUsd field is available on every QueueAI task.