Procedural Memory - joehubert/ai-agent-design-patterns GitHub Wiki

Home::Overview of Patterns

Classification

Memory Management Pattern

Intent

To store and provide access to reusable workflows, algorithms, and problem-solving approaches that agents can apply to similar problems, enabling efficient execution of familiar tasks without recreating solution strategies from scratch.

Also Known As

Task Memory, Solution Templates, Workflow Memory, Algorithmic Memory

Motivation

LLM-based agents often encounter similar problems repeatedly but may solve them differently each time, leading to inconsistency and inefficiency. Without a structured way to remember successful problem-solving approaches:

  • Agents waste computational resources rediscovering solutions to previously solved problems
  • Solutions to recurring problems lack consistency
  • Expertise developed through experience isn't preserved
  • Complex workflows need to be regenerated even when they follow standard patterns

Procedural Memory addresses these challenges by storing successful workflows and solution approaches in a retrievable format. When faced with a familiar problem, an agent can retrieve and adapt a previously successful approach rather than solving the problem from first principles.

For example, an agentic assistant that regularly helps users analyze financial data might store procedural memory for data cleaning techniques, standard analysis workflows, and visualization approaches. When a new financial analysis request arrives, it can retrieve these procedures and adapt them to the specific request rather than designing the entire analysis workflow from scratch.

Applicability

Use the Procedural Memory pattern when:

  • Agents frequently encounter similar problem types or task categories
  • Consistent approaches to recurring problems are desirable
  • Complex multi-step workflows need to be reused with modifications
  • You want to capture and formalize domain-specific solution strategies
  • Efficiency in handling repeated tasks is a priority
  • You need to preserve expertise gained through previous problem-solving

Structure

To do...

Components

The key elements participating in the pattern:

  • Procedure Store: A repository that contains formalized procedures, algorithms, and workflows indexed for efficient retrieval.

  • Procedure Encoder: Transforms successful solution approaches into storable procedure templates by extracting key steps, parameters, and decision points.

  • Retrieval Mechanism: Identifies when a new problem matches existing procedural knowledge and selects the most appropriate procedure(s).

  • Adaptation Engine: Modifies retrieved procedures to fit the specific requirements of the current problem.

  • Execution Environment: Runs the selected and adapted procedure in the context of the current task.

  • Evaluation & Learning Component: Assesses the effectiveness of applied procedures and incorporates improvements based on new experiences.

Interactions

How the components work together:

  1. When an agent encounters a problem, the Retrieval Mechanism analyzes the problem characteristics and queries the Procedure Store for relevant procedural knowledge.

  2. The Retrieval Mechanism selects the most appropriate procedure based on similarity to the current problem context, past performance, and applicability.

  3. The Adaptation Engine then modifies the retrieved procedure to address the specific requirements of the current problem, adjusting parameters, steps, or decision criteria as needed.

  4. The Execution Environment applies the adapted procedure to solve the current problem, tracking progress and intermediate results.

  5. After execution, the Evaluation & Learning Component assesses the effectiveness of the procedure and may update the stored procedure or create a new procedure variant if significant adaptations were required.

  6. For novel problems without matching procedures, the agent may develop a new solution approach that the Procedure Encoder can then formalize and add to the Procedure Store for future use.

Consequences

The results and trade-offs of using the pattern:

  • Benefits:

    • Increased efficiency through reuse of proven solution approaches
    • Greater consistency in handling similar problems
    • Preservation of domain expertise and successful strategies
    • Reduced computational load for recurring problem types
    • Progressive improvement of procedures through experience
  • Limitations:

    • Risk of applying inappropriate procedures to problems that appear similar but have critical differences
    • Potential for overreliance on existing procedures, reducing exploration of novel approaches
    • Overhead of maintaining, categorizing, and retrieving procedural knowledge
    • Challenge of balancing adherence to procedures with adaptation to specific scenarios
  • Performance implications:

    • Faster response to familiar problem types after initial investment in procedure development
    • Higher computational cost for procedure retrieval and adaptation vs. simple prompt-response patterns
    • Potential for reduced costs over time as procedural knowledge accumulates

Implementation

Guidelines for implementing the pattern:

  1. Procedure Representation: Choose an appropriate format for representing procedures - structured formats (JSON, XML) work well for algorithmic procedures, while natural language with placeholders may be better for contextual workflows.

  2. Retrieval Strategy: Implement semantic search capabilities to match current problems with stored procedures, using embeddings to capture the conceptual similarity between problems.

  3. Granularity Decision: Determine the appropriate level of detail for stored procedures - too granular leads to retrieval complexity, too abstract limits reusability.

  4. Adaptation Mechanisms: Develop clear methods for procedure adaptation, potentially using the LLM itself to generate contextual adaptations based on procedure templates.

  5. Versioning: Implement versioning for procedures to track evolution and allow rollback to previous versions if newer ones prove less effective.

  6. Metadata Enrichment: Tag procedures with metadata about their application contexts, success rates, and limitations to improve retrieval accuracy.

  7. Integration with Episodic Memory: Consider how procedural memory interfaces with episodic memory to provide context for procedure selection and adaptation.

  8. Evaluation Metrics: Define clear metrics for assessing procedure effectiveness to guide the learning and improvement process.

Code Examples

To do...

Variations

Common modifications or adaptations of the basic pattern:

  • Hierarchical Procedural Memory: Organizing procedures in hierarchical structures where high-level strategies can invoke lower-level tactics and operations.

  • Collaborative Procedure Development: Multiple agents contribute to and refine shared procedural knowledge based on their specialized expertise.

  • Context-Weighted Procedures: Procedures that dynamically adjust their approach based on contextual factors like user preferences, available resources, or time constraints.

  • Competitive Procedure Selection: Maintaining multiple alternative procedures for similar problems and selecting between them based on performance history.

  • Just-In-Time Procedure Compilation: Rather than storing complete procedures, storing components that are assembled into custom procedures at runtime.

  • Human-in-the-Loop Refinement: Involving human experts in reviewing and refining the most critical or frequently used procedures.

Real-World Examples

Systems or applications where this pattern has been successfully applied:

  • Example 1: AI coding assistants that store standard implementation patterns for common programming tasks, like data validation or API integration, adapting them to specific language and framework requirements.

  • Example 2: Healthcare diagnostic systems that maintain procedural memory for diagnostic workflows, adjusting them based on patient characteristics and available medical resources.

  • Example 3: Financial advisory agents that store regulatory compliance procedures and adapt them to different jurisdictions and client profiles.

  • Example 4: Customer support agents that maintain procedural memory for troubleshooting common product issues, personalizing the steps based on customer technical expertise and product configuration.

Related Patterns

Other patterns that:

  • Episodic Memory: Provides contextual information about past interactions that can inform procedure selection and adaptation.

  • Declarative Knowledge Bases: Supply factual information that procedures may reference or incorporate during execution.

  • Reflection: Helps evaluate and improve procedural approaches through introspection on their effectiveness.

  • Hierarchical Task Decomposition: Often uses procedural memory to guide the breakdown of complex tasks.

  • Planner: May leverage procedural memory to construct plans for complex tasks by composing stored procedures.

  • Semantic Caching: Can be used to cache the results of procedure execution for similar inputs.