# Maximum Byte Size for User Prompts and Post‑Compaction Summaries in ai‑memory: The 16 KiB Limit

> Discover the 16 KiB byte size limit for user prompts and post-compaction summaries in akitaonrails/ai-memory. Learn why this limit ensures safe observation storage.

- Repository: [Fabio Akita/ai-memory](https://github.com/akitaonrails/ai-memory)
- Tags: deep-dive
- Published: 2026-08-22

---

**The ai‑memory crate enforces a hard limit of 16,384 bytes (16 KiB) on both user‑prompt excerpts and post‑compaction summary excerpts to ensure observation payloads remain within safe storage boundaries.**

The ai‑memory repository (akitaonrails/ai-memory) manages observation data through strict sanitization rules that cap content size. Understanding the maximum byte size for user prompts and post‑compaction summaries is essential for developers integrating observation hooks or building custom memory backends.

## The Universal 16 KiB Ceiling

All observation bodies in ai‑memory share a single hard limit defined in the core sanitizer module. This ceiling ensures that serialized observations remain portable and do not overrun buffer allocations during storage or transmission.

The limit is defined as:

- **Constant**: `OBSERVATION_BODY_MAX_BYTES`
- **Value**: `16 * 1024` (16,384 bytes)
- **Location**: [`crates/ai-memory-core/src/sanitize.rs`](https://github.com/akitaonrails/ai-memory/blob/main/crates/ai-memory-core/src/sanitize.rs) at line 46

## Implementation Across the Crate

The 16 KiB limit propagates from the core sanitizer to the hooks crate through explicit constant aliases.

### Core Definition in ai-memory-core

In [`crates/ai-memory-core/src/sanitize.rs`](https://github.com/akitaonrails/ai-memory/blob/main/crates/ai-memory-core/src/sanitize.rs), the sanitizer establishes the foundational limit:

```rust
pub const OBSERVATION_BODY_MAX_BYTES: usize = 16 * 1024;

```

This constant serves as the single source of truth for all observation body size validations.

### Hook-Level Aliases in ai-memory-hooks

The hooks crate imports this limit and exposes it under semantic names for specific excerpt types. In [`crates/ai-memory-hooks/src/payload.rs`](https://github.com/akitaonrails/ai-memory/blob/main/crates/ai-memory-hooks/src/payload.rs) (lines 10–15), you will find:

```rust
pub const USER_PROMPT_EXCERPT_MAX_BYTES: usize = ai_memory_core::OBSERVATION_BODY_MAX_BYTES;
pub const POST_COMPACTION_EXCERPT_MAX_BYTES: usize = ai_memory_core::OBSERVATION_BODY_MAX_BYTES;

```

Both `USER_PROMPT_EXCERPT_MAX_BYTES` and `POST_COMPACTION_EXCERPT_MAX_BYTES` alias the core 16 KiB value, ensuring consistency across user prompt handling and post‑compaction summarization.

## Truncating Content to Fit the Limit

When ingesting data that exceeds 16 KiB, ai‑memory truncates content at byte boundaries to comply with the limit. The core sanitizer provides utility functions for safe UTF-8 truncation.

```rust
use ai_memory_core::{OBSERVATION_BODY_MAX_BYTES, truncate_utf8_bytes};
use ai_memory_hooks::{USER_PROMPT_EXCERPT_MAX_BYTES, POST_COMPACTION_EXCERPT_MAX_BYTES};

fn main() {
    // Verify that both excerpt types share the identical 16 KiB ceiling
    assert_eq!(USER_PROMPT_EXCERPT_MAX_BYTES, OBSERVATION_BODY_MAX_BYTES);
    assert_eq!(POST_COMPACTION_EXCERPT_MAX_BYTES, OBSERVATION_BODY_MAX_BYTES);

    // Example: Truncating an oversized user prompt
    let oversized_prompt = "x".repeat(20_000); // 20 KB input
    let safe_excerpt = truncate_utf8_bytes(&oversized_prompt, USER_PROMPT_EXCERPT_MAX_BYTES);
    
    assert!(safe_excerpt.len() <= 16_384);
}

```

This pattern ensures that no observation excerpt—whether captured from a user prompt or generated during compaction—violates the storage boundary.

## Summary

- ai‑memory enforces a **16 KiB (16,384 bytes)** maximum size on all observation bodies.
- The limit is defined centrally in [`crates/ai-memory-core/src/sanitize.rs`](https://github.com/akitaonrails/ai-memory/blob/main/crates/ai-memory-core/src/sanitize.rs) as `OBSERVATION_BODY_MAX_BYTES`.
- **User‑prompt excerpts** use `USER_PROMPT_EXCERPT_MAX_BYTES` and **post‑compaction summaries** use `POST_COMPACTION_EXCERPT_MAX_BYTES`, both aliasing the same 16 KiB value.
- Content exceeding the limit is truncated using byte‑safe utilities to maintain valid UTF-8 encoding.

## Frequently Asked Questions

### What is the exact maximum byte size for user prompts in ai‑memory?

The exact limit is **16,384 bytes** (16 KiB). This is defined by the `USER_PROMPT_EXCERPT_MAX_BYTES` constant in [`crates/ai-memory-hooks/src/payload.rs`](https://github.com/akitaonrails/ai-memory/blob/main/crates/ai-memory-hooks/src/payload.rs), which aliases `OBSERVATION_BODY_MAX_BYTES` from the core sanitizer.

### Why does ai‑memory use 16 KiB instead of a larger limit?

The 16 KiB ceiling prevents observation payloads from overrunning storage boundaries and buffer allocations. According to the source code in [`crates/ai-memory-core/src/sanitize.rs`](https://github.com/akitaonrails/ai-memory/blob/main/crates/ai-memory-core/src/sanitize.rs), this limit ensures safe serialization while still accommodating substantial context windows.

### How does ai‑memory handle prompts that exceed the byte limit?

When a prompt exceeds 16 KiB, the system truncates it using byte‑safe utilities like `truncate_utf8_bytes`. This function ensures the resulting excerpt does not exceed `OBSERVATION_BODY_MAX_BYTES` while preserving valid UTF-8 character boundaries.

### Are post‑compaction summaries treated differently than user prompts?

No. Both excerpt types are subject to the identical 16 KiB limit. The constants `POST_COMPACTION_EXCERPT_MAX_BYTES` and `USER_PROMPT_EXCERPT_MAX_BYTES` both resolve to `OBSERVATION_BODY_MAX_BYTES`, guaranteeing uniform size constraints across all observation excerpts.