Managing run memory and continuity in smolagents
Use memory's step structure, callbacks and single-step execution to carry an auditable record from one run into the next task.
Prerequisites and input · Engineering checks · 1. Step 1: Fix a baseline record of the run
At a glance
The smolagents docs reduce an agent to a few components: tools, prompts, and above all a memory of past steps drawing a history of planning, execution and errors. The hard part of continuing a task is not saving history but judging which parts still hold and which later actions overturned. This task works through three access surfaces: replaying a run, reading and rewriting memory steps, and taking control back with one-step-at-a-time execution.
Runtime and applicability
- Article type
- Engineering practice
- Verified
- Applies to
- smolagents Managing run memory and continuity in smolagents
Execution locations and responsibilities
- smolagents Python runtime Use the workflow below to distinguish responsibilities of the caller, runtime, and external actions.
Arrows show the order of checks in the guide. Follow each step’s prerequisites, branches and stop conditions; unexecuted steps are not completed work.
When to use
- The documented entry point is
agent.memory: system_prompt holds the prompt given to the agent, and steps is the sequence whose first entry is the task step. ActionStep carries fields such as step_number, observations and error, so iterating can branch by type — print the error where one exists, print the observations where it does not.get_full_steps()returns full steps as dictionaries for external records, andagent.replay()replays the last run. A third surface, instrumentation, displays the run in a zoomable UI. The three differ in purpose: replay for process, memory for data, instrumentation for timing.
When not to use
- Not for treating memory as durable storage: the docs describe an in-run step sequence and a snapshot export, not cross-process restore or versioned storage.
- Callback edits affect the in-process memory object only and do not undo external actions already taken; side effects of completed tools can be recorded but not rolled back here.
- Single-step execution makes ordering and numbering your responsibility; if you cannot guarantee that, use one run plus post-run inspection instead of hand-assembling steps.
Prerequisites and input
- A completed agent run, or a readable list of memory steps from one.
- The follow-up task, and which premises it shares with the earlier one.
- An agreed scope for edits: which steps may be deleted and which may only be appended.
Engineering checks
Layer 1: The visible structure of memory — The documented entry point is agent.memory: system_prompt holds the prompt given to the agent, and steps is the sequence whose first entry is the task step. ActionStep carries fields such as step_number, observations and error, so iterating can branch by type — print the error where one exists, print the observations where it does not. get_full_steps() returns full steps as dictionaries for external records, and agent.replay() replays the last run. A third surface, instrumentation, displays the run in a zoomable UI. The three differ in purpose: replay for process, memory for data, instrumentation for timing.
Layer 2: Callback intervention at step granularity — The docs state that step callbacks can access the agent itself and therefore any memory step, changing it when needed. The illustration is a browser agent capturing screenshots: log the newest screenshot each step while clearing images from steps two or more back to save token cost. That makes memory a workspace trimmed continuously by callbacks rather than a read-only log. The question at this layer is whether information needed for continuity belongs in the steps at all, or should be pruned by step number inside a callback. Callbacks are passed via step_callbacks at initialization, alongside settings such as additional_authorized_imports, max_steps and verbosity_level.
Layer 3: Single-step execution and hand-built memory — For tool calls that take days, the docs recommend running the agent step by step, which also allows updating memory on each step. The pattern: send tools into the executor with agent.python_executor.send_tools({**agent.tools}), append a TaskStep to agent.memory.steps yourself, then in a loop build an ActionStep, call agent.step(memory_step), append that step back, until final_answer is returned or your step allowance runs out. The docs also note that another agent's memory can be assigned directly to agent.memory.steps. This is the strongest manual control and the easiest to get wrong: ordering of task and action steps, continuity of step_number, and the final_answer test all become yours to maintain.
1. Step 1: Fix a baseline record of the run
Where: the host audit record. Input: agent.memory system_prompt and steps. Save memory.system_prompt separately, export steps using get_full_steps(), and explicitly build a local record containing both fields. Do not claim get_full_steps() itself includes the system prompt. Record tasks and ActionStep numbers, errors and observations by step type with cross-run provenance. Completion: the assembled audit record contains separately saved prompt and complete steps with failures identified. Stop continuation for missing records or unknown provenance and assess reset after retaining the gap.
2. Step 2: Mark premises as reusable or stale
Where: a review copy of the baseline snapshot. Input: the snapshot and the follow-up task. Action: judge each recorded action for whether it still holds for the new task — for instance whether an intermediate result was later corrected by a subsequent step. Completion check: every record carries a reusable or stale mark plus one line of reasoning. Stop condition: where you cannot tell whether a step still holds, mark it unreusable; redo rather than reuse.
3. Step 3: Choose retention granularity and a pruning rule
Where: step_callbacks or post-run cleanup. Input: heavy fields and still-valid content. Preserve required information. The documented screenshot example sets observations_images=None when previous_step.step_number <= latest_step - 2; it clears images, not automatically the full ActionStep or its text. Completion: fields, trigger and audit copy are explicit. Stop pruning that would remove required subsequent input.
4. Step 4: Choose reuse-in-place or rebuild
Where: agent.memory and the next agent.run. Input: valid/stale labels and the new task. With entirely valid context, run(new_task,reset=False) keeps the conversation; the default reset=True resets it. For mixed history first preserve an audit snapshot, then remove or explicitly isolate stale items and conclusions depending on them in the working memory before reusing valid context. With mostly stale or conflicting tasks choose reset=True for a fresh run. AgentMemory.reset() clears steps while keeping system_prompt; it does not imply rollback of external actions. Completion: continued working context contains no unsupported stale facts, a reset branch has no old steps, and the branch plus snapshot are recorded. Stop reuse when dependencies are unclear and start fresh while retaining the audit copy.
5. Step 5: Assemble task and action steps by hand
Where: the selected execution entry. Input: the new task and tools. For normal automatic execution call run with the reset decision from step 4. Only when manual control is needed prepare python_executor.send_tools, append TaskStep, construct each ActionStep, call step and append it to memory until final_answer or a local step cap. Prepare manual memory according to the valid-history/reset decision, without adding a default run that would clear it. Completion: entry, reset decision and retained-history scope agree, with explicit manual numbering and bounds. Stop with incomplete status at the step cap; pause if mixed entries cleared context and revisit the audit decision.
6. Step 6: Make controlled edits between steps
Where: working memory between steps. Input: observations, newly confirmed conclusions and unresolved errors. Condense or replace noisy observations only with new evidence, preserving original text, changes and reasons. Keep unresolved errors visible without rewriting erroneous observations as unsupported success. Completion: each edit is evidenced and the working context can be compared with its audit copy. Stop edits that would hide errors or unknowns and gather evidence first.
7. Step 7: Review the continuation's auditability
Where: the final memory. Input: the whole continued run. Action: export get_full_steps() again, confirm the system prompt, task step and action steps are self-consistent in order and numbering, and check for unmarked gaps; use replay() as a second process-level view when needed. Completion check: a reader of this snapshot can say which conclusions came from the earlier round and which are new. Stop condition: if ordering or numbering is inconsistent, this run cannot serve as the starting point for anything later.
Task example
Unexecuted walkthrough: an old run holds valid field explanations and a failed query. Export an audit snapshot, remove or isolate the failed query and dependent conclusions from working steps, then continue the valid explanations with reset=False. If dependencies are unclear, choose reset=True for a fresh run. Manual mode separately appends TaskStep and runs step-by-step without mixing in a default resetting run. Screenshot pruning uses step_number<=latest_step-2 and clears only those images. Condense noisy observations only with new evidence and keep unresolved failure causes visible. Export the final snapshot to check valid provenance, reset choice and new steps; none of this was actually executed.
Completion checks
- The local audit record explicitly combines
memory.system_promptandget_full_steps()step representations instead of assuming a step export is a full memory snapshot. - Retained context remains evidenced; stale mixed-history items and dependent conclusions are removed/isolated in working memory, reset matches the chosen branch and the audit copy remains intact.
- The pruning rule names what is kept, what is cleared, and the trigger.
- The single-step loop has an explicit ceiling and a stated exit when final_answer never arrives.
- In the final snapshot, step order and numbering are consistent and cross-round origins are distinguishable.
Verification scope
Official Memory, Guided tour and Agents API documentation was read. No agent was installed or created and run/replay/step/reset were not executed; continuity and reset effects were not verified. Working-context pruning is a review method, expected examples are not runtime receipts and resetting does not imply external-state rollback.
Sources
All links are official pages; verification dates are listed per source, and links point to the version checked.