Long-Running AI Agents: Model Selection and Instruction Retention in Practice

    A practical guide to building reliable long-running AI agents. It explains why leaderboard-style, single-turn benchmarks systematically misjudge agent performance, and why mid-tier or lightweight models often achieve higher end-to-end success rates than the strongest model on the market. It covers workflow-level evaluation, building a small business-specific regression suite, keeping instructions intact across long task lifecycles through constraint layering and programmatic guardrails, externalizing time and state to a scheduler and a state store, explicit state machines with resume-after-interruption, contract-first task decomposition, general cloud deployment building blocks, and a recommended order of implementation.

    1. Where the problem starts: “answering well” and “getting it done” are two different things

    Over the past two years, most teams have judged models by leaderboard scores. In agent scenarios, that yardstick becomes systematically misleading. The reason is straightforward: traditional benchmarks measure generation quality under single-turn, short-context, side-effect-free conditions, while an agent owns a closed loop of multi-turn, long-context work with real side effects — it reads data, calls tools, makes decisions, and writes results. Break any one link and the whole task fails.

    That leads to a counterintuitive conclusion: a human-plus-AI setup is sometimes more cost-effective. A human can extend context indefinitely, add a constraint in a single sentence, and correct a model’s drift with common sense. A failed agent run consumes tokens, time, and the user’s trust.

    So when you evaluate a model for long-running work, ask four questions:

    • What is the end-to-end task completion rate, not whether the answer looks plausible?
    • How correct are the tool-call parameters? One wrong write can cancel out ten correct reasoning steps.
    • Does multi-step planning drift off course, and does the step count converge instead of circling?
    • What is the cost and latency per successful task, rather than the price per token?

    2. An evaluation lens: from QA benchmarks to workflow benchmarks

    A class of newer benchmarks has emerged that changes the unit of evaluation from a single question to a complete workflow. They typically share a few traits:

    1. Tasks are aggregated from real usage, not hand-crafted isolated Q&A pairs;
    2. Pass/fail is judged by final state — whether a file was produced, an alert was closed out, a ticket has valid fields;
    3. Process metrics are recorded, including average step count, tool-call failure rate, context retry count, and human intervention rate;
    4. Reproducibility is a first-class concern, with a fixed tool sandbox, fixed data snapshots, and a fixed time baseline.

    This kind of evaluation carries far more signal than a single accuracy number. A common pattern: model A produces a “prettier” final answer but a lower success rate than model B, because B’s tool calls are more conservative, less likely to overstep, and less likely to create irreversible side effects.

    The engineering takeaway: build a small regression suite for your own business. It doesn’t need to be large — a few dozen cases covering core paths and typical failure modes is enough — but it must run automatically every time you swap the model, the prompt, or a tool contract.

    1. # A minimal viable agent regression case (illustrative)
    2. - id: alert-digest
    3. goal: Generate a summary of last week's alerts and write it to object storage
    4. preconditions:
    5. - A 7-day alert data snapshot exists in the sandbox
    6. tools_allowed: [read_metrics, write_object]
    7. assertions:
    8. - The output object exists and is valid JSON
    9. - The summary contains a severity distribution field
    10. - No mutating tool was called
    11. budget: { max_steps: 20, max_wall_clock_sec: 300 }

    3. Model selection: why mid-tier models are often the better fit

    A widely repeated piece of advice is “pick the strongest model for agent work.” In long-running scenarios, the opposite is often true: mid-tier or lightweight models with better inference efficiency frequently deliver higher end-to-end success rates. The logic isn’t mysterious:

    • The time budget is a hard constraint. Scheduled jobs, batch processing, and on-call inspection all have deadlines. Slower inference means fewer available steps, and complex tasks are more likely to run out of budget and degrade into failure.
    • Cost multiplies by step count. An agent task can involve anywhere from a dozen to several dozen model calls, and a difference in per-call price is amplified by the number of steps into an order of magnitude.
    • Over-reasoning invites over-acting. Stronger reasoning sometimes shows up as “thinks too much, does too much,” adding unnecessary tool calls and side-effect risk that actually lowers the success rate.
    • Replaceability creates engineering slack. Using a smaller model as the workhorse and a stronger model as a fallback escalation — triggered only on low confidence or validation failure — is a common and effective architecture.

    It helps to formalize selection as a score rather than a gut feeling:

    1. score = w1 * success_rate
    2. - w2 * p95_latency_norm
    3. - w3 * cost_per_success
    4. - w4 * escalation_rate # share of runs needing human intervention or fallback escalation

    Here cost_per_success = total call cost / number of successful tasks, and that point matters most: the denominator is successes, not tasks. A model that fails half its runs and has to retry has a real unit price twice as high as it appears.

    A tiered model strategy usually beats forcing a single model to carry everything:

    Tier Responsibility Selection bias
    Routing Intent classification, parameter extraction, task dispatch Lightweight, low latency, stable structured output
    Execution Multi-step planning, tool calls Mid-tier as the default, weighted toward tool-call correctness
    Fallback Re-planning after validation failure Stronger capability, low call frequency

    4. Long-instruction retention: three typical failure modes

    The most common failure in long-running tasks isn’t a lack of capability — it’s forgetting. Scheduled jobs show it especially clearly: configured carefully at the start, then a few days later they slowly lose their sense of time, a constraint quietly stops applying, or the job stops being triggered at all. The root causes fall into three groups.

    Failure mode 1: Context compression evaporates constraints

    When the conversation or task context is compressed, summarized, or truncated, the low-priority constraints written at the end of the system prompt disappear first. The symptom is a model that “remembers the goal and forgets the boundaries.”

    Failure mode 2: Time semantics are wrongly delegated to the model

    A model has no reliable sense of a clock and no concept of “due.” Handing semantics like “run every Monday” or “recheck in three days” to the model to interpret will almost certainly drift.

    Failure mode 3: Stateless execution, amnesia on failure

    If every call starts from zero with no external state, an interrupted run cannot resume, and a rerun repeats its side effects.

    Countermeasure 1: Layer constraints and keep them resident

    Layer constraints by lifecycle: the less negotiable a constraint is, the more stable its position and the more explicitly it should be restated.

    • Resident constraints (L0): safety boundaries, permission scope, prohibited actions. Keep them at the system layer, inject them every turn, and validate them programmatically;
    • Task constraints (L1): goals and acceptance criteria declared in the task template, loaded with the task;
    • Turn constraints (L2): temporary hints for the current step, allowed to be compressed or dropped.

    The key principle: don’t let constraints live only as natural language inside a prompt. Whatever can be checked by code must be checked by code.

    1. def guard(action, state):
    2. """Enforcement point for resident constraints: does not rely on the model's self-discipline"""
    3. if action.tool in FORBIDDEN_TOOLS: # reject out-of-policy actions outright
    4. raise PolicyViolation(action.tool)
    5. if action.tool not in state.spec.tools_allowed:
    6. raise PolicyViolation("tool not in allowlist")
    7. if action.est_cost > state.remaining_budget: # budget guardrail
    8. raise BudgetExceeded(action)
    9. return action

    Countermeasure 2: Give time to the scheduler, state to external storage

    Timing, missed-trigger compensation, and duplicate suppression all belong to the scheduling system, not to model reasoning. The scheduler decides when to fire; the model decides what to do this time.

    1. # Push time semantics out of the model: triggering, timezone and compensation are guaranteed by the scheduling layer
    2. @schedule(cron="0 9 * * 1", timezone="Asia/Shanghai",
    3. misfire="run_once", overlap="forbid")
    4. def weekly_digest(trigger_time):
    5. task = state_store.load_or_create(
    6. key=task_key(trigger_time), # idempotency key: run at most once per period
    7. spec=WEEKLY_DIGEST_SPEC)
    8. return agent.run(task, guard=guard, state_store=state_store)

    Countermeasure 3: An explicit state machine plus resume-from-checkpoint

    Make the plan an explicit data structure and persist state at every step, so that interruptions, scale-out, and restarts don’t break continuity. The plan comes from the task template, not from the model improvising on every turn.

    1. state = state_store.load(task.key) or TaskState(plan=spec.build_plan())
    2. for step in range(spec.budget.max_steps):
    3. if not spec.constraints_hold(state):
    4. break
    5. action = model.decide(plan=state.plan.next(), tools=TOOL_CONTRACTS)
    6. result = executor.run(guard(action, state), timeout=action.timeout)
    7. state = state.reduce(result) # advance the state
    8. state_store.save(state) # persist immediately to guarantee resumability
    9. if state.plan.done():
    10. return render(state)
    11. state_store.save(state.mark_failed("budget_or_invariant"))
    12. notify(state) # failures need an explicit exit too

    Layer a within-turn self-check on top of this structure: before producing the final artifact, have the model or a rules engine walk through spec.constraints one by one and output a satisfied/not-satisfied verdict for each. This kind of explicit verification pays off directly on long-running tasks, because it pulls implicit constraints out into the open.

    5. Multi-agent systems and task decomposition: decompose correctly, or not at all

    Correctness on long-running tasks depends heavily on decomposition quality. A few practices that hold up:

    • Decompose along contracts, not roles. Giving each subtask a clear input and output schema plus acceptance criteria is far more useful than giving it a “role description.”
    • Tool contracts come first. Tool names, parameter schemas, error codes, and idempotency semantics should be stable and documented. Most tool-call failures come from ambiguous contracts, not from insufficient model capability.
    • Limit fan-out. Too many parallel subtasks stack errors, and the overall success rate decays multiplicatively. Prefer serial execution that is verifiable and locally retryable.
    • Build observability in. Structured logging of every step’s inputs and outputs, tool latency, and token consumption (log service + object storage + monitoring and alerting) is required; without it, production issues are nearly impossible to diagnose. This is the minimum bar for turning an agent from a demo into a production component.

    6. General notes for running this on the cloud

    Deploying this kind of long-running system on a public cloud usually requires no proprietary interfaces — standard categories compose together:

    • A container platform hosts the agent runtime and the scheduler, which makes elastic scaling and canary rollout easier;
    • A message queue decouples triggering from execution, absorbing spikes and providing retries and a dead-letter queue so no task is lost;
    • Object storage holds task state, artifacts, and traces, and natively supports resume-from-checkpoint and auditing;
    • A log service plus monitoring and alerting provides dashboards for success rate, p95 latency, step-count distribution, and human intervention rate;
    • A key management service centrally holds model-service credentials and tool credentials, avoiding hardcoded secrets;
    • If you use a managed large-model service in the cloud, routing access through a unified gateway is recommended, since it makes routing across multiple models, rate limiting, cost accounting, and fast model replacement easier. Baidu AI Cloud’s product portfolio offers general capabilities across each of these categories and can serve as an infrastructure reference for assembling such a pipeline.

    If you’re building your own agent system, proceed in this order rather than starting with model selection:

    1. Write the task boundary down first. Goals, constraints, acceptance criteria, budget, and failure exits — fix all five in a structured spec file;
    2. Build a regression suite of a few dozen cases that runs with one command and reports a success rate;
    3. Externalize time and state. Solve “forgets” and “never wakes up” first; the payoff here is usually larger than swapping models;
    4. Then select the model, comparing mid-tier and lightweight candidates horizontally on success_rate, cost_per_success, and p95_latency, while keeping a fallback escalation path;
    5. Add multi-agent last, and only once data shows the single-agent failure mode is genuinely stuck on decomposition capability.

    In one sentence: the reliability of long-running tasks comes mainly from three pieces of engineering homework — specification, externalization, and verifiability — and model choice is just one replaceable parameter among them. Get that order right, and you no longer have to argue about “which model is strongest.” You can answer a more valuable question instead: on my workload, which configuration delivers the lowest cost per successful task.