Large CLAUDE.md Files on Ollama: Why a 35KB Preprompt That Works on Opus Breaks Local Models — and the 3-Tier Fix

claude-codeollamaclinelocal-llmsetup-guidecost-analysis

TL;DR: A 35KB CLAUDE.md is roughly 9,000 tokens — invisible on Claude Opus 5’s 1M window with $0.50/M cached reads, but 14% of a 65K local window before your code loads. Local models under ~30B also follow long instruction files noticeably worse. Restructure the file first; buy VRAM second.

After this guide you’ll be able to:

  • Calculate exactly what your CLAUDE.md costs in tokens, dollars, and local context share before you migrate
  • Configure Claude Code against Ollama so a large preprompt isn’t silently truncated
  • Pick the right path by preprompt size: any local model, a 262K-context model on a 24GB card, or staying on the API

Honest take: If your CLAUDE.md is over 35KB, the file is the problem, not the backend. Split it with path-scoped rules before you spend $1,000+ on a GPU — and if you can’t shrink it, a cached Sonnet 5 reads it for $0.0018 per turn, which is cheaper than the electricity to reprocess it locally.

A postmortem that hit the Hacker News front page on September 15, 2026 (106 points) put hard numbers on a migration a lot of developers are attempting right now: Patrick McCanna moved a 35KB production preprompt from Anthropic’s Claude Opus API to self-hosted Ollama running behind OpenCode, and documented where it fell apart. The prompt that ran fine for months on the cloud API consumed 14% of the local 65K context window on its own, and the agent “ran out of fuel” within minutes — thrashing on repeated tool calls and re-reading files it had already read.

None of those failures are exotic. Each one is a documented, reproducible property of how Ollama, Claude Code, and local models handle large system-level prompts. Everything below was verified September 15, 2026 against the Ollama FAQ and Claude Code integration docs, Anthropic’s memory and pricing documentation, and the original writeup — with the recurring caveat that several pages were cross-verified through search summaries because direct fetches are egress-blocked in this environment.

How many tokens does a 35KB CLAUDE.md actually cost?

About 8,000–9,500 tokens, depending on the tokenizer. Anthropic’s own rule of thumb is 1 token ≈ 4 characters of English, so 35,840 bytes lands near 9,000 tokens — consistent with McCanna’s measurement that the file ate 14% of a 65,536-token window (14% of 65,536 = 9,175).

Where that hurts depends entirely on the backend:

Claude Opus 5 (API)Claude Sonnet 5 (API)Local, 65K windowLocal, default window
Context share0.9% of 1M0.9% of 1M14%Doesn’t fit (4,096 tokens)
Cost per turn to re-read$0.0045 cached$0.0018 cached$0 + reprocess time
50-turn session, preprompt only~$0.23~$0.09$0

The API prices are from Anthropic’s pricing page as of September 15, 2026: Opus 5 is $5/M input with $0.50/M cache reads; Sonnet 5 is $2/M with $0.20/M cache reads (the September 1 increase to $3/$15 was canceled). Prompt caching is what makes a 35KB preprompt nearly free on the cloud API — every agent turn re-reads it at one-tenth of input price.

That last column is the first gotcha. Ollama’s compatibility layer does not implement Anthropic-style prompt caching, so a local backend reprocesses the full preprompt on every turn — you pay in seconds of prefill instead of dollars, and the bill grows with every tool call in the session.

Why does a big preprompt work on Opus but break on Ollama?

Three failures stack, and each one alone can look like “the local model is dumb.”

Ollama’s default context window is smaller than your preprompt. The Ollama FAQ documents a 4,096-token default, and recent releases scale the default with available VRAM: under 24 GiB you get 4,096 tokens, 24–48 GiB gets 32,768, and only 48 GiB+ unlocks 262,144. A 9,000-token CLAUDE.md doesn’t fit the bottom tier at all — and Ollama doesn’t error. It silently truncates from the top of the prompt, which is exactly where system-level instructions live. We documented the full mechanics and per-tool fixes in the num_ctx deep dive; everything there applies here unchanged.

No caching means the window fills fast and stays full. On the API, a long session accumulates history against a 1M window. Locally, your 9,000-token preprompt + Claude Code’s system prompt and tool schemas + file contents + tool-call history all compete inside 32K–65K. McCanna’s logs show what saturation looks like in practice: the agent second-guesses instructions, re-reads files it already processed, and burns its remaining window on redundant tool calls. His mitigations — fewer tool calls per step, session state logged to disk for handoffs — are workarounds for a window that’s simply too small for the prompt style.

Instruction adherence degrades on long files — even before truncation. This isn’t only a local-model problem. Anthropic’s own Claude Code documentation tells you to target under 200 lines per CLAUDE.md because “longer files consume more context and reduce adherence.” A 35KB file is 500+ lines. A 27B-class local model holds persona and multi-step rules across that span measurably worse than Opus 5 does; SitePoint’s migration guide for this exact scenario recommends canary-fact probes (plant a checkable fact mid-file, ask for it back, target ≥80% retrieval) to measure the decay instead of guessing.

The compounding is what kills the migration: the model that most needs a tight prompt is also the one getting it truncated and re-reading it cold every turn.

How do you point Claude Code at Ollama without losing the CLAUDE.md?

Three environment variables plus one server setting. Ollama’s Claude Code integration docs (verified today) specify:

export ANTHROPIC_AUTH_TOKEN=ollama       # required by the client, ignored by Ollama
export ANTHROPIC_API_KEY=""
export ANTHROPIC_BASE_URL=http://localhost:11434

OLLAMA_CONTEXT_LENGTH=65536 ollama serve  # 64K+ is Ollama's own recommendation for coding tools
claude --model qwen3.8:27b

Then verify the window actually took — this is the check most broken setups skip:

$ ollama ps
NAME            ID              SIZE     PROCESSOR    CONTEXT    UNTIL
qwen3.8:27b     3fa1c2d9e8b7    21 GB    100% GPU     65536      4 minutes from now

Two things to confirm in that output: CONTEXT shows your number, not 4096, and PROCESSOR reads 100% GPU. A CPU split means the weights plus the KV cache for that window exceed your VRAM, and throughput collapses. Note the precedence trap: a num_ctx baked into a Modelfile overrides OLLAMA_CONTEXT_LENGTH, so if a custom model was built with a small window, the env var silently loses.

Inside Claude Code, run /context and check your CLAUDE.md appears under Memory files — that’s the direct confirmation the file loaded rather than being skipped. One documented hard limit: Claude Code loads a CLAUDE.md of up to 4 MiB in full and skips larger files entirely, so a 35KB file always loads on the client side. The truncation risk is entirely on the Ollama side of the wire. Claude Code also resolves its background haiku alias separately from --model; set ANTHROPIC_DEFAULT_HAIKU_MODEL to a local model or background calls fail — the full setup guide covers that trap.

Cline users hit the same wall with different knobs: Cline’s own system prompt alone overflows a 4K window before your rules are even appended, and its Ollama provider has a per-model context-window field that must match the server’s. The tool-use loop fix covers Cline’s version of the thrash pattern.

How should you shrink a 35KB CLAUDE.md instead of porting it?

Use Claude Code’s own splitting mechanisms — they cut what loads per session, which helps local backends far more than any server flag.

  • Path-scoped rules. Move file-type-specific instructions into .claude/rules/*.md with paths: frontmatter (e.g. paths: ["src/api/**/*.ts"]). These load only when the agent touches matching files, instead of spending window in every session. This is the single highest-leverage change for a local backend.
  • Keep the root file under 200 lines. Anthropic’s documented target. Run /doctor — since v2.1.206 it proposes trims, cutting content the model can derive from the codebase (directory layouts, dependency lists) and keeping pitfalls and conventions.
  • Know what @imports don’t do. Splitting into @path/to/file.md imports organizes the text but does not reduce context — imported files still load in full at launch. Splitting only pays off with path-scoped rules.
  • Free wins: block-level HTML comments (<!-- note -->) are stripped before injection, so maintainer notes cost zero tokens; structure the survivors as bookends (critical rules at the top and restated at the bottom), the pattern SitePoint’s guide validates for long system prompts on local models, since the middle of a long prompt is what degrades first.

A 35KB monolith usually compresses to a 5–8KB core plus rules that load on demand. That version works on a 32K local window — the monolith never will.

Which local models can hold a large preprompt on real hardware?

The model needs two things: a native context window well past 64K, and enough VRAM headroom to hold the KV cache for the window you configure. As of September 2026 the strongest fit on a single consumer card is Qwen3.8-27B — Apache 2.0, 262K native context, and an 18GB Q4_K_M Ollama build. The catch is the cache: the weights fit a 24GB card, but an f16 KV cache at a 64K window pushes total memory to roughly 24–27GB, past an RTX 3090 or 4090. Quantize the KV cache to q8_0 or cap the window at ~48K to stay on-GPU; the out-of-memory fix walks through the settings.

The hardware floor for this workload is a used 24GB RTX 3090, tracking $1,000–1,400 on the used market as of September 2026 (GPUDojo/ResalePrices eBay data) — and rising, not falling. A 12–16GB card can run a smaller model at 16–32K, which handles a trimmed 5–8KB CLAUDE.md but not a 35KB one with room left to work. For picking a model by VRAM tier, runaihome’s local models by VRAM guide is the companion piece; aifoss.dev’s Ollama review covers the server itself.

When should you stay on the Claude API instead?

Decide by preprompt size — this is the 3-tier path, with what each tier actually requires:

Your CLAUDE.mdThe pathWhat it takesWhen NOT to do this
≤8KB (~2K tokens)Any capable local coding model, 32K window12–16GB VRAM you may already ownYou bill hourly and prefill latency costs more than API cents
8–35KBQwen3.8-27B-class at 48–64K, quantized KV cache24GB card, ~$1,000–1,400 usedSharing a GPU with anything else; no time for canary-testing adherence
>35KB, can’t restructureStay on Sonnet 5 or Opus 5 with prompt caching$0.0018–$0.0045 per turn to re-readHard privacy requirement — then restructure is mandatory, not optional

The economics of the third row are worth stating plainly, because the migration impulse is usually about cost. At Sonnet 5’s cached-read rate, re-reading a 9,000-token preprompt 7,500 times a month (50-turn sessions, 5 a day) costs about $13.50 — and that’s the preprompt share of the bill, not typical total spend, since pinning Sonnet 5 in Claude Code keeps whole sessions around $0.65. Against a $1,000+ GPU whose break-even we’ve measured at 16–53 months depending on what it replaces, “my preprompt is big” is not by itself a reason to go local. Privacy is; API rate limits can be; the preprompt isn’t.

The boundary case cuts the other way too: if you’ve trimmed to a lean core plus path-scoped rules, a 24GB card genuinely delivers a $0-marginal-cost agent that holds its instructions. The postmortem’s real lesson is the order of operations — restructure the prompt for the constraint, then migrate. Porting a cloud-shaped prompt to local hardware unchanged fails on contact.

FAQ

Does Claude Code have a maximum CLAUDE.md size? Yes — 4 MiB. Files up to that size load in full; larger files are skipped entirely, not truncated. The practical guidance is much lower: Anthropic recommends under 200 lines per file because adherence drops as the file grows. A 35KB file loads fine; it just works worse everywhere, and drastically worse on local models.

Why does my local agent re-read the same files in a loop? Context saturation or silent truncation. The window filled (or Ollama truncated from the top), the record of what the agent already did fell out, and it repeats itself. Run ollama ps and check the CONTEXT column — if it reads 4096, that’s the whole bug. Set OLLAMA_CONTEXT_LENGTH=65536 and confirm the model still shows 100% GPU.

Is a 35KB CLAUDE.md too big even for the cloud API? Not for cost — cached, it’s under half a cent per turn on Opus 5. But Anthropic’s own docs say long instruction files reduce adherence on every model, and it occupies context on every request. Trimming to a core file plus path-scoped rules improves the cloud experience and is the prerequisite for local. There’s no configuration where the 35KB monolith is the right shape.

Sources

Last updated September 15, 2026. Pricing, context defaults, and model availability change frequently; verify current state before making purchasing decisions.

Was this article helpful?

Know which coding tool is worth paying for

Hands-on comparisons of AI coding assistants and what each one costs to run — including the local-model path. Sent only when something changes. Unsubscribe anytime.