Skip to chapter
Peanut learns to agentRead with page turns →

Chapter 03 of 12 · Aishwarya Ashok

Instructions, context, and memory

Peanut keeps the right things close

Instructions tell the system what to do. Context supplies information for the next model response. State and memory preserve what the application needs as the job continues. The same fact may move between these places.

System prompt

Instructions supplied at the system level to guide a model's behavior across an interaction.

Peanut stirs an orange soup pot beside standing kitchen rules and a separate soup-order ticket.
Lunch comes with an order and a set of kitchen rules.Peanut is the agent. The kitchen rules represent standing instructions; the order ticket supplies the current task.

An application can supply standing instructions about the model's role, behavior, and response requirements. Many platforms provide a system-level message for this purpose, with priority defined by their instruction hierarchy. Use it to describe how the model should behave. Access restrictions belong in software controls as well.

Instruction hierarchy

The priority order used to resolve conflicting instructions from different sources.

Peanut carries boxes away from an open exit. A building rule says to keep it clear, while a delivery note says to stack boxes in the doorway.
The delivery note has found a very inconvenient place for the boxes.Peanut is the agent resolving conflicting instructions. The building rule has higher authority than the delivery note.

Many AI platforms assign different authority to instructions based on their source. The model should follow that ordering when instructions conflict. A retrieved page is usually task material, so a command inside it should not acquire the authority of the application's own rules. Preserving that distinction is part of defending against prompt injection.

Tokens and tokenization

Tokens are units used to represent model inputs and outputs; tokenization converts content into those units.

Builder Peanut feeds word-fragment tiles from the sentence Peanuts work into a tokenizer machine, which produces numbered tiles. The split is labeled illustrative.
One sentence, several pieces. Some words need more than one.Peanut is the builder here. The word-fragment tiles and numbered output illustrate text tokens and their identifiers; the split and numbers are examples.

A text tokenizer converts text into token identifiers using a particular encoding. Its units may represent whole words, word parts, punctuation, or other fragments. Different tokenizers can split the same sentence differently. Those choices affect context capacity and usage accounting, which is why word counts are only a rough guide to token counts.

Source & further reading

Technical reference: Tokenization ↗

Context engineering

Designing how an application selects, assembles, and maintains the information supplied to a model.

Peanut packs job, measurements, and tool-result cards into a work tray while other-job papers stay on a shelf.
Pack for the next step. The unrelated papers can stay on the shelf.Peanut selects the information for the agent’s next step. The tray represents the supplied context; the shelf holds material left out of this call.

What does the model need to make the next decision? Context engineering turns that question into application behavior: selecting instructions, retrieving documents, retaining useful results, and removing clutter. The selection changes as work progresses. A good context strategy supplies relevant evidence when it is needed and gives the system a way to recover details later.

Source & further reading

Technical reference: Context engineering ↗

Context window

The maximum amount of tokenized information a model can handle within a single inference context.

Peanut reads a short section of a long paper roll through an orange rectangular frame labeled capacity, with most of the roll outside the opening.
The paper keeps going beyond the opening.Peanut is using the model’s available context. The opening represents its bounded capacity; the rest of the roll is information that exists outside that context.

A model has a finite capacity for the information involved in a single inference context. Supplied input and space needed for generation must fit the model's limits. Information stored elsewhere has to be brought into context to be used directly. Increasing the window gives you more capacity; choosing useful content remains part of the job.

Context compaction

Reducing accumulated context while retaining the information needed to continue a task.

Peanut selects destination, completed work, and a detour question from a trip diary, then travels with a small summary card while the full diary stays on a shelf.
Keep the destination, the completed stops, and that unresolved detour.Peanut is the agent continuing its task. The card is compacted context, including the detour question; the retained diary represents the fuller record.

Long-running work accumulates more history than the next step needs. Compaction reduces that history, often by summarizing earlier messages and removing redundant material. Preserve decisions, constraints, and unfinished work with care. Since compression loses detail, retain a way to consult the underlying records when the summary leaves an important question unanswered.

Source & further reading

Technical reference: Compaction ↗

Session state

Data that records the current interaction or run and its progress.

Peanut writes on a laundry ticket listing towels, the gentle cycle, and washing status beside a running washing machine.
Towels. Gentle cycle. Still washing. The ticket keeps track.Peanut is the agent. The ticket represents the application's record of the current job.

Session state lets an application keep track of the current interaction. It may include messages, intermediate results, progress, and a pending approval. Some state is supplied to the model; other parts remain in application storage. Keeping that distinction clear helps avoid stuffing every operational detail into the prompt.

Working memory

Information kept readily available for the agent's immediate task.

Peanut attaches a hinge to a wooden box while two nearby notes specify an M3 screw and an outward-facing hinge.
Screw size and hinge direction stay within reach.The two notes stand for information kept available for this step: which screw to use and which way the hinge faces.

An agent may keep immediate task information in its current context, a scratchpad, or task state. This is often called working memory, though implementations vary. Refresh the selection as the task progresses so the useful notes remain easy to find.

Persistent memory

Information stored beyond a single interaction so it can be retrieved and used later.

On Monday, Agent Peanut files a customer's paper-wrap preference. On Friday, the agent retrieves the same card beside a new parcel and wrapping paper.
Friday's parcel gets Monday's packing preference.The drawer is application storage. Retrieving the saved card makes the preference available on the later visit.

Useful information can survive a session in a file or database. Later, the application retrieves relevant records and supplies them to the model. That retrieval is what makes a saved preference available for the current response. Storing the record changes application memory, not the model's trained parameters.

Checkpointing

Saving task state at defined points so execution can recover or resume from recorded progress.

Peanut closes a clear-lidded tray labeled saved progress, containing a partly assembled wooden model, loose parts, and assembly notes.
Save the parts and the progress notes together.The tray stands for a checkpoint: saved task state and completed results, kept together for recovery.

Imagine a run stopping halfway through a long job. A checkpoint gives its recovery logic a recorded place to begin, including state and completed results.

External changes need separate attention. Restoring a local checkpoint does not undo an order already submitted to another service.

Source & further reading

Technical reference: Persistence ↗

Resumability

The ability to continue interrupted work using retained state and progress.

After power returns, Peanut opens a saved-progress tray holding notes and a base with two attached uprights. In the next scene, Peanut adds a crossbar to that same intact base.
The lights return. Start with the next part.Peanut is the agent resuming work. The base and its two uprights stay assembled; the saved notes guide the next step.

Resuming means restoring the work well enough to continue from retained progress. That involves more than reading a saved file: tools may need to reconnect, permissions may have changed, and an external operation may have completed during the interruption. Recovery logic reconciles those conditions with what the application last recorded.