Context, Memory, and Tools
MESHFLOW_MOCK=1 python3 hands_on/02_custom_agents.pyLesson 02: Context, Memory, And Tools
Lesson Goal
This lesson teaches the three building blocks that turn a plain LLM call into a useful application:
- Context: what the model can see right now.
- Memory: what the system can store and retrieve later.
- Tools: what the system can do outside text generation.
Beginners often mix these together. Keep them separate in your head. A model can only use information that is in context for the current call. Memory is not automatically visible. Tools are not automatically safe. The orchestration layer is responsible for deciding what to retrieve, what to include, what to execute, and what to log.
1. The Beginner Mental Model
Imagine you ask a model:
Write a project update for the billing migration.
The model does not magically know:
- Which company you work for.
- What the billing migration is.
- What happened last week.
- Which tasks are blocked.
- Whether the update is for executives or engineers.
- Whether it is allowed to look up documents or send messages.
The application must provide that information.
user request
+ system instructions
+ relevant conversation history
+ retrieved project notes
+ tool results
+ required output format
-> model call
That full package is context.
2. What Context Is
Context is the complete input the model receives during one call. It can include many pieces:
| Context Piece | Example | Why It Matters |
|---|---|---|
| System instructions | "You are a careful technical tutor." | Sets behavior and boundaries |
| Developer instructions | "Use short examples before abstractions." | Shapes implementation style |
| User request | "Explain tools and memory." | Defines the immediate goal |
| Conversation history | Previous turns in the chat | Helps maintain continuity |
| Retrieved documents | Relevant course notes | Grounds the answer |
| Tool definitions | Search, calculator, database query | Tells the model what it may request |
| Tool results | Search results, calculation output | Gives fresh facts or computed values |
| Workflow artifacts | research_notes, draft_answer | Carries state between steps |
| Output schema | JSON fields or markdown format | Makes the result easier to parse |
If a fact is not in context, the model may guess. Sometimes the guess sounds confident. That is why good AI systems focus so much on context assembly.
3. Context Window
The context window is the maximum amount of input the model can receive in one call. It is usually measured in tokens. A token is a small piece of text.
A larger context window helps, but it does not solve everything:
- Too much context can bury the important facts.
- Old or irrelevant context can confuse the model.
- Contradictory context can produce unstable answers.
- Private data should not be included unless it is needed.
- Long context can cost more and run slower.
Beginner rule: include the smallest set of information that lets the model do the job correctly.
4. What Memory Is
Memory is information stored outside the current model call so it can be used later. Memory is a system design choice, not a magical model property.
Common memory types:
| Memory Type | Stores | Example |
|---|---|---|
| Conversation memory | Recent messages | Last 10 chat turns |
| User memory | Stable user preferences | "Prefers concise answers" |
| Project memory | Project decisions and facts | "Use PostgreSQL for audit logs" |
| Episodic memory | Summaries of past sessions | "Last session debugged auth" |
| Semantic memory | Searchable knowledge | Docs indexed by embeddings |
| Workflow memory | Durable run state | Artifacts from previous nodes |
Memory becomes useful only after retrieval. A memory system usually follows this cycle:
store useful information
-> index or organize it
-> retrieve relevant pieces for a new task
-> filter for quality, privacy, and freshness
-> insert selected memory into context
5. Context vs Memory
This is one of the most important distinctions in the course.
| Question | Context | Memory |
|---|---|---|
| Where is it? | Inside one model call | Outside the model call |
| How long does it last? | Temporary | Durable if stored |
| Can the model see it automatically? | Yes, if included | No |
| Who manages it? | The application or orchestrator | The application or memory service |
| Main risk | Too much, missing, or wrong information | Stale, irrelevant, or unsafe retrieval |
Short version:
Context is what the model sees now.
Memory is what the system may remember later.
6. What Tools Are
A tool is a function or capability the AI system can call. Tools let the system do work that plain text generation cannot reliably do.
Examples:
- Search the web or a private knowledge base.
- Calculate numbers.
- Query a database.
- Read a file.
- Create a ticket.
- Send an email.
- Call an internal API.
- Run code.
- Check policy rules.
The model does not directly execute the tool. The usual flow is:
application sends user request + tool schemas to model
model asks to call a tool with structured arguments
application validates the arguments
application runs the tool
application adds the tool result to context
model uses the result to continue or answer
7. Tool Schemas
A tool schema describes what the tool is for and what inputs it accepts.
Bad schema:
tool: do_stuff(input)
Better schema:
tool: search_course_notes
description: Search the course notes for relevant passages.
arguments:
query: string, required, max 200 characters
lesson: optional string
Good schemas reduce confusion. They also make validation easier.
8. Tool Safety
Tools can cause real-world effects. That means tool use needs guardrails.
Classify tools by risk:
| Risk Level | Tool Example | Guardrail |
|---|---|---|
| Low | Word count, formatting | Log result |
| Medium | Search, read-only database query | Validate input, limit scope |
| High | Write database, send email, deploy code | Require approval gate |
| Critical | Delete data, transfer money | Strong policy, human approval, audit trail |
Beginner rule: if a tool changes the outside world, add a gate before execution or before finalizing the result.
9. How Context, Memory, And Tools Work Together
Suppose the user asks:
Create a beginner lesson on agent memory using our course style.
A good workflow might:
- Read the user request.
- Retrieve prior course style notes from memory.
- Search existing lesson files with a tool.
- Put the relevant notes and search results into context.
- Ask the model to draft the lesson.
- Use a checklist tool to inspect the draft.
- Store the final lesson summary back into memory.
The model is important, but the system around the model is what makes the work reliable.
10. Hands-On: Run The Example
Run:
python3 -m src.mini_meshflow run examples/02_tools_and_memory.json
Look for:
- Which node writes memory.
- Which node reads memory.
- Which node calls a tool.
- Which artifact carries the user goal.
- Which final output uses earlier artifacts.
Then draw the same workflow:
python3 -m src.mini_meshflow diagram examples/02_tools_and_memory.json
Read the diagram like a story:
remember goal -> read goal -> call tool -> generate final response
11. Practice: Label The System
For each item, decide whether it is context, memory, a tool, or an artifact.
| Item | Best Label |
|---|---|
| Current user question | Context |
| Saved user preference | Memory |
| Calculator function | Tool |
draft_answer from a previous node | Artifact |
| Search result inserted into the prompt | Context |
| Vector database of course notes | Memory |
| Human approval record | Artifact |
Artifacts often become context for later nodes. That is why explicit artifact names matter so much in MeshFlow-style workflows.
12. Common Beginner Mistakes
Mistake 1: Assuming the model remembers everything.
Correction: The model only sees what the application puts into the current context.
Mistake 2: Putting everything into context.
Correction: Retrieve and include only what is relevant, fresh, and allowed.
Mistake 3: Letting the model call tools without validation.
Correction: Treat tool input like any other untrusted input.
Mistake 4: Forgetting to log tool results.
Correction: Record tool name, input, output, error, duration, and cost where appropriate.
Mistake 5: Storing low-quality memories.
Correction: Store durable facts, decisions, and summaries. Avoid storing noisy drafts unless they are useful for audit.
13. Design Checklist
Before building any LLM application, answer:
- What does the model need to see for this task?
- Which facts should be retrieved from memory?
- Which memories are stale or unsafe to include?
- Which tools are available?
- Which tools can change the outside world?
- What tool inputs must be validated?
- What artifacts should be produced for later steps?
- What should be logged for debugging?
- What should require human approval?
14. Summary
Context, memory, and tools are separate responsibilities:
- Context is the assembled input for one model call.
- Memory stores information outside the call and retrieves it later.
- Tools let the system calculate, search, query, or act.
Most production AI quality problems come from weak context, stale memory, unsafe tools, or invisible tool results. MeshFlow-style workflows make these pieces explicit so learners can inspect and improve them.
Exercises
Exercises
Exercise 1: Run The Memory And Tool Workflow
python3 -m src.mini_meshflow run examples/02_tools_and_memory.json
Identify:
- Which node writes memory.
- Which node reads memory.
- Which node calls a tool.
- Which artifact stores the word count.
Exercise 2: Draw The Flow
Run:
python3 -m src.mini_meshflow diagram examples/02_tools_and_memory.json
Copy the graph into your notes and label where memory and tool use happen.
Exercise 3: Change The Tool Input
Change the remembered goal in ../../examples/02_tools_and_memory.json, then run the workflow again.