{"record":{"id":"64c2d31821ecd040","repo":"JuliusBrussee/caveman","slug":"cave-memory-too-large","errorCode":"cave_memory_too_large","errorMessage":"cave_memory_too_large","messagePattern":"cave_memory_too_large","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"mem/store.go","lineNumber":71,"sourceCode":"// across all its hits. Without it, recall loaded and returned every matching\n// memory whole — a single 2.5 MB memory came back as tokens_added=440,000 in one\n// result. Callers override it per-call through RecallOptions.TokenBudget.\nconst DefaultTokenBudget = 2000\n\n// UnlimitedTokenBudget disables Recall's aggregate token cap. It is explicit:\n// zero retains the safe DefaultTokenBudget for existing Go callers. Public\n// adapters map their documented token_budget=0 sentinel to this value.\nconst UnlimitedTokenBudget = -1\n\n// MaxMemoryBytes is the largest single memory Remember accepts. A memory is a\n// fact or note meant to be recalled and injected, not a file dump; anything\n// larger fails closed with ErrMemoryTooLarge rather than being stored and later\n// blowing the recall budget.\nconst MaxMemoryBytes = 256 * 1024\n\n// ErrMemoryTooLarge is returned when new memory text exceeds MaxMemoryBytes.\n// It carries the cave_ error idiom so callers surface cave_memory_too_large.\nvar ErrMemoryTooLarge = errors.New(\"cave_memory_too_large\")\n\n// tokenCounter is the shared offline BPE estimator (o200k_base). It is read-only\n// and safe for concurrent use, matching the engine's inferred token accounting.\nvar tokenCounter = tokens.Default()\n\n// Store is a cavemem instance: a SQLite memory table plus an engine (with its\n// own CCR store) used to compress recalls.\ntype Store struct {\n\tdb  *sql.DB\n\teng *engine.Engine\n\tccr *ccr.Store\n}\n\n// Options configures Open.\ntype Options struct {\n\t// Dir is the data directory; default ~/.caveman/mem (honoring CAVEMAN_HOME).\n\tDir string\n\t// InMemory uses an ephemeral database for both memories and CCR (tests).","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/mem/store.go#L53-L89","documentation":"mem.Store rejects any memory whose text exceeds MaxMemoryBytes (256 KiB), returning the sentinel ErrMemoryTooLarge which carries the cave_ error idiom. It fails closed so oversized memories are never stored and never blow the recall budget later. Callers surface it as the MCP tool error cave_memory_too_large or as a dedicated CLI exit code.","triggerScenarios":"store.Remember(text) (and supersede paths validated by validateMemorySize) called with text longer than 256*1024 bytes; the MCP remember tool in mem/cmd/cavemem/main.go:126 and the remember CLI command at main.go:254 map this error to tool error/exit code.","commonSituations":"Pasting a large document/log into a remember call; agent pipelines dumping whole files as memories; a supersede replacement bigger than the limit; unicode-heavy text where character count misleads about byte size.","solutions":["Trim or chunk the text below 256 KiB before calling Remember (store an excerpt or split into multiple memories).","Compress/summarize the content before storing.","Check len([]byte(text)) (or utf8.RuneCountInString for per-memory semantics) client-side and reject early with a better message.","If legitimately larger memories are required, raise the limit consciously at deployment knowing recall budget grows."],"exampleFix":"// before\nm, err := store.Remember(bigText)\n// after\nif len(bigText) > mem.MaxMemoryBytes {\n    bigText = summarize(bigText)\n}\nm, err := store.Remember(bigText)","handlingStrategy":"validation","validationCode":"if len([]byte(text)) > mem.MaxMemoryBytes {\n    return fmt.Errorf(\"memory is %d bytes, limit is %d\", len(text), mem.MaxMemoryBytes)\n}","typeGuard":null,"tryCatchPattern":"m, err := store.Remember(text)\nif errors.Is(err, mem.ErrMemoryTooLarge) {\n    text = summarize(text)\n    m, err = store.Remember(text)\n}","preventionTips":["Measure byte length (not runes) before storing","Chunk large documents into multiple memories","Validate supersede replacements against the same limit"],"tags":["validation","size-limit","memory-store"],"backgroundTag":"payload-too-large","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}