# MemoryStrategy

Factory class for creating memory strategies If you need long-term memory for context recall across sessions, you can setup memory extraction strategies to extract the relevant memory from the raw events.

Use built-in strategies for quick setup, use built-in strategies with override to specify models and prompt templates.

- Source: [`aws-bedrockagentcore/lib/memory/memory-strategy.ts`](https://github.com/aws/aws-cdk/blob/v2.278.0/packages/aws-cdk-lib/aws-bedrockagentcore/lib/memory/memory-strategy.ts#L122)
- Type last changed: v2.255.0 · 19 May 2026

## Example

TypeScript:

```ts
// Create a custom semantic memory strategy
const customSemanticStrategy = agentcore.MemoryStrategy.usingSemantic({
  strategyName: "customSemanticStrategy",
  description: "Custom semantic memory strategy",
  namespaces: ["/custom/strategies/{memoryStrategyId}/actors/{actorId}"],
  customConsolidation: {
    model: bedrock.FoundationModel.fromFoundationModelId(this, 'ConsolidationModel',
      bedrock.FoundationModelIdentifier.ANTHROPIC_CLAUDE_3_5_SONNET_20241022_V2_0,
    ),
    appendToPrompt: "Custom consolidation prompt for semantic memory",
  },
  customExtraction: {
    model: bedrock.FoundationModel.fromFoundationModelId(this, 'ExtractionModel',
      bedrock.FoundationModelIdentifier.ANTHROPIC_CLAUDE_3_5_SONNET_20241022_V2_0,
    ),
    appendToPrompt: "Custom extraction prompt for semantic memory",
  },
});

// Create memory with custom strategy
const memory = new agentcore.Memory(this, "MyMemory", {
  memoryName: "my-custom-memory",
  description: "Memory with custom strategy",
  expirationDuration: cdk.Duration.days(90),
  memoryStrategies: [customSemanticStrategy],
});
```

Python:

```python
# Create a custom semantic memory strategy
custom_semantic_strategy = agentcore.MemoryStrategy.using_semantic(
    strategy_name="customSemanticStrategy",
    description="Custom semantic memory strategy",
    namespaces=["/custom/strategies/{memoryStrategyId}/actors/{actorId}"],
    custom_consolidation=agentcore.OverrideConfig(
        model=bedrock.FoundationModel.from_foundation_model_id(self, "ConsolidationModel", bedrock.FoundationModelIdentifier.ANTHROPIC_CLAUDE_3_5_SONNET_20241022_V2_0),
        append_to_prompt="Custom consolidation prompt for semantic memory"
    ),
    custom_extraction=agentcore.OverrideConfig(
        model=bedrock.FoundationModel.from_foundation_model_id(self, "ExtractionModel", bedrock.FoundationModelIdentifier.ANTHROPIC_CLAUDE_3_5_SONNET_20241022_V2_0),
        append_to_prompt="Custom extraction prompt for semantic memory"
    )
)

# Create memory with custom strategy
memory = agentcore.Memory(self, "MyMemory",
    memory_name="my-custom-memory",
    description="Memory with custom strategy",
    expiration_duration=cdk.Duration.days(90),
    memory_strategies=[custom_semantic_strategy]
)
```

Java:

```java
// Create a custom semantic memory strategy
ManagedMemoryStrategy customSemanticStrategy = MemoryStrategy.usingSemantic(ManagedStrategyProps.builder()
        .strategyName("customSemanticStrategy")
        .description("Custom semantic memory strategy")
        .namespaces(List.of("/custom/strategies/{memoryStrategyId}/actors/{actorId}"))
        .customConsolidation(OverrideConfig.builder()
                .model(FoundationModel.fromFoundationModelId(this, "ConsolidationModel", FoundationModelIdentifier.ANTHROPIC_CLAUDE_3_5_SONNET_20241022_V2_0))
                .appendToPrompt("Custom consolidation prompt for semantic memory")
                .build())
        .customExtraction(OverrideConfig.builder()
                .model(FoundationModel.fromFoundationModelId(this, "ExtractionModel", FoundationModelIdentifier.ANTHROPIC_CLAUDE_3_5_SONNET_20241022_V2_0))
                .appendToPrompt("Custom extraction prompt for semantic memory")
                .build())
        .build());

// Create memory with custom strategy
Memory memory = Memory.Builder.create(this, "MyMemory")
        .memoryName("my-custom-memory")
        .description("Memory with custom strategy")
        .expirationDuration(Duration.days(90))
        .memoryStrategies(List.of(customSemanticStrategy))
        .build();
```

C# / .NET:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
// Create a custom semantic memory strategy
var customSemanticStrategy = MemoryStrategy.UsingSemantic(new ManagedStrategyProps {
    StrategyName = "customSemanticStrategy",
    Description = "Custom semantic memory strategy",
    Namespaces = new [] { "/custom/strategies/{memoryStrategyId}/actors/{actorId}" },
    CustomConsolidation = new OverrideConfig {
        Model = FoundationModel.FromFoundationModelId(this, "ConsolidationModel", FoundationModelIdentifier.ANTHROPIC_CLAUDE_3_5_SONNET_20241022_V2_0),
        AppendToPrompt = "Custom consolidation prompt for semantic memory"
    },
    CustomExtraction = new OverrideConfig {
        Model = FoundationModel.FromFoundationModelId(this, "ExtractionModel", FoundationModelIdentifier.ANTHROPIC_CLAUDE_3_5_SONNET_20241022_V2_0),
        AppendToPrompt = "Custom extraction prompt for semantic memory"
    }
});

// Create memory with custom strategy
var memory = new Memory(this, "MyMemory", new MemoryProps {
    MemoryName = "my-custom-memory",
    Description = "Memory with custom strategy",
    ExpirationDuration = Duration.Days(90),
    MemoryStrategies = new [] { customSemanticStrategy }
});
```

Golang:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
// Create a custom semantic memory strategy
customSemanticStrategy := agentcore.MemoryStrategy_UsingSemantic(&ManagedStrategyProps{
	StrategyName: jsii.String("customSemanticStrategy"),
	Description: jsii.String("Custom semantic memory strategy"),
	Namespaces: []*string{
		jsii.String("/custom/strategies/{memoryStrategyId}/actors/{actorId}"),
	},
	CustomConsolidation: &OverrideConfig{
		Model: bedrock.FoundationModel_FromFoundationModelId(this, jsii.String("ConsolidationModel"), bedrock.FoundationModelIdentifier_ANTHROPIC_CLAUDE_3_5_SONNET_20241022_V2_0()),
		AppendToPrompt: jsii.String("Custom consolidation prompt for semantic memory"),
	},
	CustomExtraction: &OverrideConfig{
		Model: bedrock.FoundationModel_*FromFoundationModelId(this, jsii.String("ExtractionModel"), bedrock.FoundationModelIdentifier_ANTHROPIC_CLAUDE_3_5_SONNET_20241022_V2_0()),
		AppendToPrompt: jsii.String("Custom extraction prompt for semantic memory"),
	},
})

// Create memory with custom strategy
memory := agentcore.NewMemory(this, jsii.String("MyMemory"), &MemoryProps{
	MemoryName: jsii.String("my-custom-memory"),
	Description: jsii.String("Memory with custom strategy"),
	ExpirationDuration: cdk.Duration_Days(jsii.Number(90)),
	MemoryStrategies: []IMemoryStrategy{
		customSemanticStrategy,
	},
})
```

## Initializer

TypeScript:

```ts
new MemoryStrategy()
```

Python:

```python
MemoryStrategy()
```

Java:

```java
new MemoryStrategy()
```

C# / .NET:

```csharp
new MemoryStrategy()
```

Golang:

```go
NewMemoryStrategy()
```

## Methods

- `usingBuiltInEpisodic(): ManagedMemoryStrategy` (static) — Default strategies for organizing and extracting memory data, each optimized for specific use cases.

  Captures meaningful slices of user and system interactions, preserve them into compact records after summarizing.
  Extracted memory example: User first asked about pricing on Monday, then requested feature comparison on Tuesday, finally made purchase decision on Wednesday.

  **Returns:** A ManagedMemoryStrategy.

- `usingBuiltInSemantic(): ManagedMemoryStrategy` (static) — Default strategies for organizing and extracting memory data, each optimized for specific use cases.

  Distills general facts, concepts, and underlying meanings from raw conversational data, presenting the information in a context-independent format.
  Extracted memory example: In-context learning = task-solving via examples, no training needed.

  **Returns:** A ManagedMemoryStrategy.

- `usingBuiltInSummarization(): ManagedMemoryStrategy` (static) — Default strategies for organizing and extracting memory data, each optimized for specific use cases.

  This strategy compresses conversations into concise overviews, preserving essential context and key insights for quick recall.
  Extracted memory example: Users confused by cloud setup during onboarding.

  **Returns:** A ManagedMemoryStrategy.

- `usingBuiltInUserPreference(): ManagedMemoryStrategy` (static) — Default strategies for organizing and extracting memory data, each optimized for specific use cases.

  Captures individual preferences, interaction patterns, and personalized settings to enhance future experiences.
  Extracted memory example: User needs clear guidance on cloud storage account connection during onboarding.

  **Returns:** A ManagedMemoryStrategy.

- `usingEpisodic(config: ManagedStrategyProps): ManagedMemoryStrategy` (static) — Creates an episodic memory strategy with custom configuration.

  Captures meaningful slices of user and system interactions, preserve them into compact records after summarizing.
  Extracted memory example: User first asked about pricing on Monday, then requested feature comparison on Tuesday, finally made purchase decision on Wednesday.

  **Parameters**

  - `config` — - The configuration for the episodic memory strategy.

  **Returns:** A ManagedMemoryStrategy.

- `usingSelfManaged(config: SelfManagedStrategyProps): SelfManagedMemoryStrategy` (static) — Creates a self-managed memory strategy.

  A self-managed strategy gives you complete control over your memory extraction and consolidation pipelines.

  **Parameters**

  - `config` — - The configuration for the self-managed memory strategy.

  **Returns:** A SelfManagedMemoryStrategy.

- `usingSemantic(config: ManagedStrategyProps): ManagedMemoryStrategy` (static) — Creates a semantic memory strategy with custom configuration.

  Distills general facts, concepts, and underlying meanings from raw conversational data, presenting the information in a context-independent format.
  Extracted memory example: In-context learning = task-solving via examples, no training needed.

  **Parameters**

  - `config` — - The configuration for the semantic memory strategy.

  **Returns:** A ManagedMemoryStrategy.

- `usingSummarization(config: ManagedStrategyProps): ManagedMemoryStrategy` (static) — Creates a summarization memory strategy with custom configuration.

  This strategy compresses conversations into concise overviews, preserving essential context and key insights for quick recall.
  Extracted memory example: Users confused by cloud setup during onboarding.

  **Parameters**

  - `config` — - The configuration for the summarization memory strategy.

  **Returns:** A ManagedMemoryStrategy.

- `usingUserPreference(config: ManagedStrategyProps): ManagedMemoryStrategy` (static) — Creates a user preference memory strategy with custom configuration.

  Captures individual preferences, interaction patterns, and personalized settings to enhance future experiences.
  Extracted memory example: User needs clear guidance on cloud storage account connection during onboarding.

  **Parameters**

  - `config` — - The configuration for the user preference memory strategy.

  **Returns:** A ManagedMemoryStrategy.
