muscle-mem is a behavior cache for AI agents.
It is a Python SDK that records your agent's tool-calling patterns as it solves tasks, and will deterministically replay those learned trajectories whenever the task is encountered again, falling back to agent mode if edge cases are detected.
The goal of muscle-mem is to get LLMs out of the hotpath for repetitive tasks, increasing speed, reducing variability, and eliminating token costs for the many cases that could have just been a script.
It's unexplored territory, so all feedback is welcome!
- Read Muscle Mem - Removing LLM calls from Agents for more context
- Join Muscle Mem discord for feedback
- May 7, 2025 - First working demo
- May 8, 2025 - Open sourced
muscle-mem is not another agent framework.
You implement your agent however you want, and then plug it into muscle-mem's engine.
When given a task, the engine will:
- determine if the environment has been seen before (cache-hit), or if it's new (cache-miss) using Checks
- perform the task, either
- using the retrieved trajectory on cache-hit,
- or passing the task to your agent on cache-miss.
- collect tool call events to add to cache as a new trajectory
To add safe tool reuse to your agent, the critical question is cache validation. Ask yourself:
For each tool we give to our agent, what features in the environment can be used to indicate whether or not it's safe to perform that action?
If you can answer this, your agent can have Muscle Memory.
pip install muscle-mem
The engine wraps your agent and serves as the primary executor of tasks.
It manages its own cache of previous trajectories, and determines when to invoke your agent.
The @engine.tool decorator instruments action-taking tools, so their invocations are recorded to the engine.
The Check is the fundamental building block for cache validation. They determine if it’s safe to execute a given action.
Each Check encapsulates:
- A capture callback to extract relevant features from the current environment
- A compare callback to determine if current environment matches cached environment
You can attach Checks to each tool @engine.tool to enforce cache validation.
This can be done before the tool call as a precheck (also used for query time validation), or after a tool call as a postcheck.
Below is a contrived example, which captures use of the hello tool, and uses timestamps and a one second expiration as the Check mechanic for cache validation.
Below is the combined script for all of the above code snippets.
For a more real example, see a computer-use agent implementation:
https://github.com/pig-dot-dev/muscle-mem/blob/main/tests/cua.py
I invite all feedback as this system develops!
Please consider:
- Joining the Muscle Mem discord
- Testing the muscle-mem repo, and giving it a star