# Resume longer tasks from local checkpoints

Keep progress across runs without resetting attempt limits or treating saved text as system instructions.

## Split a long task into recoverable slices

A task that spans several sessions needs durable progress rather than one uninterrupted model request. Reuse `mandapi_worker.py` from the [headless agent guide](/en/cookbook/building-agents/headless-agent/). Its application-owned checkpoint stores the task, selected model, conversation, attempt count, status, and answer. The system instruction stays in application code.

Treat the checkpoint as private application data. Use one writer per file, keep the same task and model when resuming, and let malformed or mismatched checkpoints fail visibly. Creating a new file starts a new allowance, so a scheduler should keep a stable file for each job.

## Resume one step

Save this beside the worker as `resume_task.py`. The included test simulates two separate invocations without contacting MandAPI. The first reads a reference; the second reloads that exact conversation and produces an answer.

```python
from pathlib import Path
from tempfile import TemporaryDirectory
from mandapi_worker import load_state, step, demo, MAX_STEPS


def resume_once(job, checkpoint, ask, model):
    state = load_state(checkpoint, job, model)
    events = []
    step(state, checkpoint, ask, events.append)
    return state, events


if __name__ == "__main__":
    with TemporaryDirectory() as folder:
        checkpoint = Path(folder) / "demo-run.json"
        job = "How long does shipping take at the demo shop?"
        first, _ = resume_once(job, checkpoint, demo, "offline-demo")
        assert first["attempts"] == 1 and first["status"] == "ready"
        resumed, events = resume_once(job, checkpoint, demo, "offline-demo")
        assert resumed["attempts"] == 2 and resumed["status"] == "done"
        assert resumed["attempts"] <= MAX_STEPS
        print(events)
```

## Run a bounded slice

Run `python resume_task.py` to check local resumption. For the real worker, `python mandapi_worker.py "YOUR TASK" --once --checkpoint task-run.json` admits at most one chat request. Set `MANDAPI_API_KEY` and `MANDAPI_MODEL_ID` first. Repeating the command resumes the same task; the persisted four-attempt ceiling does not reset. The ordinary endpoint is `https://api.mandapi.com/v1/chat/completions`.

## Test interruption and review

Replace `ask` with a function that raises `TimeoutError`. Reload the file and confirm the attempt increased before the failure. This prevents unlimited retries after restarts, although it cannot determine whether a timed-out remote request was billed. There is no automatic retry or exact spending guarantee.

Keep tools read-only while testing. Add a human review step before introducing external actions. For longer conversations, use [context compaction](/en/cookbook/building-agents/compact-context/) while preserving recent messages and identifiers; check that a resumed answer still uses the right evidence.

## References

- [https://openrouter.ai/docs/cookbook/building-agents/long-horizon-agents](https://openrouter.ai/docs/cookbook/building-agents/long-horizon-agents)
