# How to Manage Custom Memory Prompts for Generation Layers in TencentDB Memory

> Learn to manage custom Memory Prompts for TencentDB generation layers using the MemoryPromptClient SDK. Understand precedence chains for Agent, Team, Instance, and defaults.

- Repository: [Tencent Cloud/TencentDB-Agent-Memory](https://github.com/TencentCloud/TencentDB-Agent-Memory)
- Tags: how-to-guide
- Published: 2026-09-02

---

**Custom Memory Prompts in TencentDB Memory are managed through the `MemoryPromptClient` SDK and injected into generation layers (L1, L2, L3) via a precedence chain that resolves Agent → Team → Instance → Built‑in defaults.**

The TencentDB‑Agent‑Memory platform allows developers to override system behavior by defining custom Memory Prompts that target specific generation layers. These prompts are stored in the `memory_prompts` collection and bound to Agents, Teams, or Instances, then injected into the LLM system prompt at runtime. According to the source code, the platform implements optimistic concurrency control for updates and immutable protocol structures to ensure stability across the `MemoryProxy` and `MemoryCore` services.

## Understanding Memory Prompts and Generation Layers

TencentDB Memory organizes prompt injection into three distinct generation layers:

- **L1 (User‑level)** – Controls individual user context and preferences
- **L2 (Scene‑level)** – Manages situational context for specific workflows  
- **L3 (Persona‑level)** – Defines character and behavioral attributes

Each layer maintains a built‑in default prompt that provides the required JSON structure and output schema. When you create a custom Memory Prompt, you override only the **focus** or **summarisation** content while the surrounding protocol remains immutable.

### The Precedence Chain (Agent → Team → Instance)

When resolving which prompt to inject, the system walks the following precedence chain:

```

Agent → Team → Instance → Built‑in (layer defaults)

```

The `context‑injector.ts` handler evaluates this chain at runtime. If an Agent has a custom prompt bound to layer L2, that content replaces the L2 built‑in prompt. If no Agent prompt exists, the system checks for a Team‑level prompt, then an Instance‑level prompt, finally falling back to the built‑in default.

## Creating and Updating Custom Memory Prompts

The `MemoryPromptClient` class in [`sdk/memory-core/typescript/src/v3/memory-prompt-client.ts`](https://github.com/TencentCloud/TencentDB-Agent-Memory/blob/main/sdk/memory-core/typescript/src/v3/memory-prompt-client.ts) exposes methods for creating and modifying prompts. All payloads are validated against Zod schemas defined in [`MemoryCore/src/gateway/memory-prompt-schemas.ts`](https://github.com/TencentCloud/TencentDB-Agent-Memory/blob/main/MemoryCore/src/gateway/memory-prompt-schemas.ts) before persistence in the `memory_prompts` and `memory_prompt_settings` collections.

### Creating a New Prompt

Use the `create()` method to define a prompt for a specific layer and target:

```typescript
import { MemoryPromptClient } from "memory-core";

const client = new MemoryPromptClient({ baseUrl: "https://api.example.com" });

await client.create({
  name: "team-summary-prompt",
  layer: "l2",                     // L2 = scene-level
  prompt: "Summarise the latest project updates in 2 sentences.",
  team_id: "team-1234",            // bind to this team
});

```

The request inserts a record into `memory_prompts` with a unique `memory_prompt_id` and sets `memory_prompt_source` to `"team"` based on the provided `team_id`.

### Updating with Optimistic Concurrency

The `apply()` method implements versioning to prevent lost updates. Each prompt carries a `memory_prompt_version` field that must match the current database version:

```typescript
await client.apply({
  memory_prompt_id: "prompt-abcdef",
  prompt: "Summarise the latest project updates, focusing on risks.",
  version: 3,                      // must match current version
});

```

If the version is stale, the server returns a conflict error. This mechanism is enforced by the handlers in [`MemoryCore/src/gateway/memory-prompt-handlers.ts`](https://github.com/TencentCloud/TencentDB-Agent-Memory/blob/main/MemoryCore/src/gateway/memory-prompt-handlers.ts) before the `MemoryPromptStore` commits the transaction.

## Retrieving and Binding Prompts at Runtime

### Querying the Memory Prompt Store

The `GET /v3/memory-prompt/get` endpoint retrieves prompts by ID or target lookup. The handlers in [`memory-prompt-handlers.ts`](https://github.com/TencentCloud/TencentDB-Agent-Memory/blob/main/memory-prompt-handlers.ts) perform the target resolution when `memory_prompt_id` is omitted, searching the `memory_prompts` collection by `agent_id`, `team_id`, or `instance_id`.

```typescript
// Resolution follows precedence: agent → team → instance → builtin
const prompt = await store.getMemoryPrompts([
  session.agent?.promptId,
  session.team?.promptId,
]);

```

### Runtime Injection via Context Injector

The actual injection into the LLM system prompt occurs in [`MemoryProxy/src/session/context-injector.ts`](https://github.com/TencentCloud/TencentDB-Agent-Memory/blob/main/MemoryProxy/src/session/context-injector.ts). When a session is established, the injector fetches the resolved prompt content and appends it to the system prompt lines:

```typescript
if (prompt?.length) {
  // Append to the system prompt before LLM call
  systemPromptLines.push("prompt:");
  systemPromptLines.push(prompt[0].prompt);
}

```

The [`pipeline-manager.ts`](https://github.com/TencentCloud/TencentDB-Agent-Memory/blob/main/pipeline-manager.ts) in `MemoryCore/src/utils/` orchestrates which generation layer (L1/L2/L3) receives the injection, gated by the `prompt_mode` flag defined in [`memory-generation-log-types.ts`](https://github.com/TencentCloud/TencentDB-Agent-Memory/blob/main/memory-generation-log-types.ts).

## Deleting and Auditing Prompts

Remove prompts using the `delete()` method, which archives records to `memory_prompt_setting_logs` for audit purposes:

```typescript
await client.delete({ memory_prompt_ids: ["prompt-abcdef"] });

```

The deletion endpoint `POST /v3/memory-prompt/delete` is handled by [`memory-prompt-handlers.ts`](https://github.com/TencentCloud/TencentDB-Agent-Memory/blob/main/memory-prompt-handlers.ts) and ensures that removal is logged before the record is purged from `memory_prompts`.

## Summary

- **Generation Layers**: L1 (user), L2 (scene), and L3 (persona) provide structured contexts where only content—not JSON schema—can be overridden
- **Precedence Resolution**: The system evaluates Agent → Team → Instance → Built‑in when selecting prompts for injection  
- **SDK Operations**: Use `MemoryPromptClient.create()`, `apply()`, and `delete()` to manage the prompt lifecycle with optimistic concurrency control via `memory_prompt_version`
- **Runtime Injection**: The [`context-injector.ts`](https://github.com/TencentCloud/TencentDB-Agent-Memory/blob/main/context-injector.ts) module resolves and appends prompts to system prompts during session initialization
- **Source Files**: Core logic resides in [`memory-prompt-client.ts`](https://github.com/TencentCloud/TencentDB-Agent-Memory/blob/main/memory-prompt-client.ts), [`memory-prompt-schemas.ts`](https://github.com/TencentCloud/TencentDB-Agent-Memory/blob/main/memory-prompt-schemas.ts), [`memory-prompt-handlers.ts`](https://github.com/TencentCloud/TencentDB-Agent-Memory/blob/main/memory-prompt-handlers.ts), and [`context-injector.ts`](https://github.com/TencentCloud/TencentDB-Agent-Memory/blob/main/context-injector.ts)

## Frequently Asked Questions

### What are the three generation layers (L1, L2, L3) in TencentDB Memory?

L1 represents user‑level context for individual preferences, L2 manages scene‑level context for specific operational workflows, and L3 defines persona‑level attributes that control character and behavioral output. Each layer maintains immutable protocol structures while allowing content overrides via custom Memory Prompts.

### How does the precedence chain resolve conflicts between agent, team, and instance prompts?

The system evaluates the chain `Agent → Team → Instance → Built‑in` sequentially. The first custom prompt found in this hierarchy replaces the built‑in default for that layer. If an Agent has no custom prompt bound, the system checks Team level, then Instance level, finally defaulting to the layer's built‑in prompt defined in the generation configuration.

### Can I modify the JSON structure of a Memory Prompt or only the content?

Only the content (focus or summarisation text) may be modified. The surrounding protocol—including required JSON fields and output schema—is immutable to ensure compatibility with the generation layer's parsing logic in [`pipeline-manager.ts`](https://github.com/TencentCloud/TencentDB-Agent-Memory/blob/main/pipeline-manager.ts) and the `prompt_mode` validation in [`memory-generation-log-types.ts`](https://github.com/TencentCloud/TencentDB-Agent-Memory/blob/main/memory-generation-log-types.ts).

### What happens when I delete a Memory Prompt in TencentDB Agent Memory?

The `POST /v3/memory-prompt/delete` endpoint removes the prompt from the `memory_prompts` collection and writes a deletion audit entry to `memory_prompt_setting_logs`. Once deleted, the precedence chain will skip this prompt during resolution and fall back to the next available level (e.g., from Agent to Team, or Team to Instance).