MandAPI
English ▾
Build application workflows

Resume longer tasks from local checkpoints

Keep progress across runs without resetting attempt limits or treating saved text as system instructions.

Use this recipe with your coding assistant

Read the prompt
Extend the MandAPI headless Python demo with a resume_once function that performs at most one model attempt per invocation. Reuse the existing read-only step function; persist the task/model identity, conversation, status and total attempts across invocations. Reject corrupt or mismatched checkpoints rather than silently starting again. Keep trusted system instructions in code. Never reset the attempt ceiling during a resume, and count an admitted request before network I/O. Demonstrate two offline invocations and a simulated timeout; verify the second process sees the prior attempt count. Explain that checkpoints do not guarantee exactly-once billing, and that a model's completed answer still needs content review. Use MANDAPI_API_KEY, MANDAPI_MODEL_ID and https://api.mandapi.com/v1/chat/completions only if a user later enables live calls; run no paid requests now.
TXT

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. 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 while preserving recent messages and identifiers; check that a resumed answer still uses the right evidence.

References

Topics inspired by public documentation; instructions and examples adapted for MandAPI.