<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sho Tanaka (tsho)</title>
    <description>The latest articles on DEV Community by Sho Tanaka (tsho) (@tsho).</description>
    <link>https://dev.to/tsho</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3832915%2F56e76999-d063-4aaa-841b-60ef40490513.jpg</url>
      <title>DEV Community: Sho Tanaka (tsho)</title>
      <link>https://dev.to/tsho</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tsho"/>
    <language>en</language>
    <item>
      <title>pgvector vs LanceDB: A Reproducible Vector Search Benchmark (100k 1536-dim)</title>
      <dc:creator>Sho Tanaka (tsho)</dc:creator>
      <pubDate>Sat, 08 Aug 2026 04:56:27 +0000</pubDate>
      <link>https://dev.to/tsho/pgvector-vs-lancedb-a-reproducible-vector-search-benchmark-100k-x-1536-dim-odm</link>
      <guid>https://dev.to/tsho/pgvector-vs-lancedb-a-reproducible-vector-search-benchmark-100k-x-1536-dim-odm</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://implicit-none.com/en/pgvector-vs-lancedb-benchmark/" rel="noopener noreferrer"&gt;implicit-none.com&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Whenever you build a vector database — for a recommendation engine, RAG, or anything else — the same question comes up: &lt;strong&gt;should vector search ride along in the Postgres you already run (pgvector), or live in a dedicated embedding store (like LanceDB)?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As far as I could find, reproducible same-data, same-query measurements are rare. So this post benchmarks two open-source options — &lt;a href="https://github.com/pgvector/pgvector" rel="noopener noreferrer"&gt;pgvector&lt;/a&gt; 0.8.6 and &lt;a href="https://github.com/lancedb/lancedb" rel="noopener noreferrer"&gt;LanceDB&lt;/a&gt; 0.36.0 — on &lt;strong&gt;100k real OpenAI embeddings of DBpedia (1536-dim)&lt;/strong&gt; across four axes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Ingest + index build&lt;/strong&gt; (speed and disk)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The recall-latency curve&lt;/strong&gt; (comparisons only make sense at equal recall, so both systems' search parameters are swept into curves)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Concurrent load&lt;/strong&gt; (1 thread vs 8 threads)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Filtered search&lt;/strong&gt; (&lt;code&gt;WHERE&lt;/code&gt; + vector — unavoidable in real RAG)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The headline results:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;LanceDB ingests 20× faster and uses 1/3 the disk&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Single-threaded queries: LanceDB is ~2× faster at equal recall&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;But at 8 concurrent clients, pgvector flips it and wins by 1.8×&lt;/strong&gt; (server-side process parallelism vs embedded + GIL)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Filtered search is the big discovery&lt;/strong&gt;: pgvector's recall can collapse to 0.07 or jump to exact depending on what the Postgres planner silently decides; LanceDB's prefilter is boringly consistent&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The architectural difference, first
&lt;/h2&gt;

&lt;p&gt;These two aren't rivals on the same field — they're differently shaped tools, and that shapes how to read the numbers:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;pgvector&lt;/th&gt;
&lt;th&gt;LanceDB&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Form&lt;/td&gt;
&lt;td&gt;PostgreSQL extension (client/server)&lt;/td&gt;
&lt;td&gt;Embedded library (in-process)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Invocation&lt;/td&gt;
&lt;td&gt;SQL over TCP&lt;/td&gt;
&lt;td&gt;Python function calls&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Data format&lt;/td&gt;
&lt;td&gt;Postgres heap tables&lt;/td&gt;
&lt;td&gt;Lance (columnar, Arrow-native)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Transactions&lt;/td&gt;
&lt;td&gt;Full Postgres ACID&lt;/td&gt;
&lt;td&gt;Optimistic concurrency&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;pgvector's latencies include connection and protocol overhead; LanceDB's don't. That's not unfair — it's &lt;strong&gt;how you'd actually use each one&lt;/strong&gt;, and this benchmark takes that stance.&lt;/p&gt;

&lt;p&gt;One terminology note: LanceDB is the database layer built on top of the &lt;a href="https://github.com/lance-format/lance" rel="noopener noreferrer"&gt;Lance format&lt;/a&gt; (a columnar format + engine developed under its own org). The storage and vector-index implementations live in the lance core; what this article measures is the &lt;code&gt;lancedb&lt;/code&gt; package usage on top of it (using the format directly via &lt;code&gt;pylance&lt;/code&gt; is a topic for the upcoming Lance vs Parquet article).&lt;/p&gt;

&lt;h2&gt;
  
  
  Environment &amp;amp; setup
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Item&lt;/th&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Machine&lt;/td&gt;
&lt;td&gt;Apple M5 Pro / 48 GB RAM / macOS 26.4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;pgvector&lt;/td&gt;
&lt;td&gt;PostgreSQL 18.4 + pgvector &lt;strong&gt;0.8.6&lt;/strong&gt; (official Docker image, capped at 8 CPUs / 6 GB, shared_buffers 3GB)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;LanceDB&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;0.36.0&lt;/strong&gt; (Python 3.14, in-process)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Data&lt;/td&gt;
&lt;td&gt;
&lt;a href="https://huggingface.co/datasets/KShivendu/dbpedia-entities-openai-1M" rel="noopener noreferrer"&gt;DBpedia OpenAI embeddings&lt;/a&gt;, 100,000 × 1536-dim, L2-normalized&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Queries&lt;/td&gt;
&lt;td&gt;1,000 held-out vectors (never inserted), k=10, cosine&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ground truth&lt;/td&gt;
&lt;td&gt;Exact top-10 by full scan (recomputed per filter condition)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Indexes&lt;/td&gt;
&lt;td&gt;pgvector: HNSW (m=16, ef_construction=64) / LanceDB: IVF_HNSW_SQ (m=16, ef_construction=64)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Filter columns&lt;/td&gt;
&lt;td&gt;Boolean columns at 1% / 10% selectivity (deterministic by row index)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;All measurements after warmup, fixed seeds, every setting auto-saved into the result JSONs (&lt;a href="https://github.com/tsho/tsho-lab/tree/main/pgvector-vs-lancedb" rel="noopener noreferrer"&gt;code on GitHub&lt;/a&gt;).&lt;/p&gt;

&lt;h2&gt;
  
  
  Result 1: Ingest &amp;amp; index build — a LanceDB landslide
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fpl5z4ntk8qcik27wpme5.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fpl5z4ntk8qcik27wpme5.webp" alt="Ingest and index build" width="799" height="277"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Metric&lt;/th&gt;
&lt;th&gt;pgvector&lt;/th&gt;
&lt;th&gt;LanceDB&lt;/th&gt;
&lt;th&gt;Ratio&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Ingest (100k)&lt;/td&gt;
&lt;td&gt;31.3 s (3,190 vec/s)&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;1.4 s (71,474 vec/s)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;22.4×&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Index build&lt;/td&gt;
&lt;td&gt;60.3 s&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;5.2 s&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;11.6×&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Total disk&lt;/td&gt;
&lt;td&gt;2.48 GB&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0.78 GB&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;3.2×&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;pgvector's ingest uses the standard &lt;code&gt;COPY&lt;/code&gt; (text format), which pays the structural cost of textifying 1536-dim vectors through the SQL layer. LanceDB writes Arrow tables essentially as-is. The disk gap comes from Lance's columnar format plus the SQ (scalar quantization) index.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If your pipeline regenerates or appends embeddings daily, this 20× gap is wall-clock time you feel.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Result 2: The recall-latency curve — LanceDB 2× faster single-threaded
&lt;/h2&gt;

&lt;p&gt;The core comparison. Since equal-recall comparison is the only fair method for ANN benchmarks, both systems' parameters are swept into curves:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fr71l4bofb6nvabdnn17p.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fr71l4bofb6nvabdnn17p.webp" alt="Recall vs latency" width="800" height="532"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;recall@10 band&lt;/th&gt;
&lt;th&gt;pgvector p50&lt;/th&gt;
&lt;th&gt;LanceDB p50&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;~0.95&lt;/td&gt;
&lt;td&gt;2.50 ms (ef=40)&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;1.30 ms&lt;/strong&gt; (nprobes=8)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;~0.97–0.98&lt;/td&gt;
&lt;td&gt;2.86 ms (ef=80)&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;1.58 ms&lt;/strong&gt; (np=8, refine=2)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;~0.99&lt;/td&gt;
&lt;td&gt;3.42 ms (ef=160)&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;1.86 ms&lt;/strong&gt; (np=16, refine=4)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;LanceDB is roughly 2× faster across the whole range — which, per the architecture note, includes pgvector's loopback TCP + SQL overhead.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gotcha&lt;/strong&gt;: LanceDB's IVF_HNSW_SQ &lt;strong&gt;plateaus around recall 0.95&lt;/strong&gt; on its own (scalar-quantization error). If you need high recall, &lt;code&gt;refine_factor&lt;/code&gt; (re-ranking with full-precision vectors) is mandatory — every ≥0.97 row above uses it. pgvector's HNSW is unquantized, so cranking ef_search takes it toward recall 1.0 with no extra knobs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Result 3: Concurrency — pgvector flips it at 8 threads
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F0xv35f6oroe4mzvuu0vg.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F0xv35f6oroe4mzvuu0vg.webp" alt="Concurrency scaling" width="800" height="510"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;1-thread QPS&lt;/th&gt;
&lt;th&gt;8-thread QPS&lt;/th&gt;
&lt;th&gt;Scaling&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;pgvector (ef=80, recall 0.98)&lt;/td&gt;
&lt;td&gt;350&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;2,376&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;6.8×&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;LanceDB (np=8, rf=2, recall 0.97)&lt;/td&gt;
&lt;td&gt;611&lt;/td&gt;
&lt;td&gt;1,338&lt;/td&gt;
&lt;td&gt;2.2×&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The engine that was 2× faster alone loses by 1.8× under concurrency. The cause is structural:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;pgvector&lt;/strong&gt;: each connection gets its own Postgres backend process — 8 cores fully used (6.8× scaling)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LanceDB&lt;/strong&gt;: the search kernel is Rust, but it runs inside a Python process, so &lt;strong&gt;thread concurrency is constrained by the GIL&lt;/strong&gt; (2.2×)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you're serving high QPS behind a web server, this reversal matters. LanceDB could do better with multiprocessing or a free-threaded (3.13+) Python build — but these numbers are the reality of using it from stock Python.&lt;/p&gt;

&lt;h2&gt;
  
  
  Result 4: Filtered search — pgvector is "planner-dependent", LanceDB is steady
&lt;/h2&gt;

&lt;p&gt;Combining &lt;code&gt;WHERE category = ...&lt;/code&gt; with vector search is mandatory in real RAG, and this is where the two personalities diverge most:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Felk9otg7amfcqjj2t743.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Felk9otg7amfcqjj2t743.webp" alt="Filtered search" width="800" height="319"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;LanceDB (prefilter)&lt;/strong&gt;: recall 0.97–1.0 at p50 2.3–7.5 ms at both selectivities — &lt;strong&gt;consistently predictable&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;pgvector&lt;/strong&gt;: results are governed by &lt;strong&gt;which plan the Postgres planner picks&lt;/strong&gt;. Verified by recording EXPLAIN for every case:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;1% selectivity&lt;/strong&gt;: the planner skips HNSW entirely and runs an &lt;strong&gt;exact scan + sort&lt;/strong&gt; → recall 1.0 at ~5 ms. With only 1,000 candidate rows, that's the right call&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;10% selectivity&lt;/strong&gt;: &lt;strong&gt;the plan flips non-monotonically with ef_search&lt;/strong&gt; — HNSW at ef=10 and ef=80–320 (fast, recall 0.09–0.99), exact at ef=20–40 (recall 1.0 but 28 ms)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;hnsw.iterative_scan = relaxed_order&lt;/code&gt;&lt;/strong&gt; (new in 0.8) lifts the HNSW-path recall from 0.76 to 0.97 at ef=80. &lt;strong&gt;Non-negotiable if you do filtered search on pgvector&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The most important gotcha&lt;/strong&gt;: right after a bulk load (before &lt;code&gt;ANALYZE&lt;/code&gt;), the planner has no statistics and picks HNSW + post-filter even at 1% selectivity — &lt;strong&gt;recall collapsed to 0.071&lt;/strong&gt;. The identical query returns recall 1.0 after ANALYZE. &lt;strong&gt;Always ANALYZE after bulk-loading into pgvector&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In short: pgvector's filtered search lets the planner silently decide between fast and exact, and demands tuning literacy (ANALYZE, iterative_scan, reading plans). LanceDB has none of that complexity — but also none of the planner's cleverness (like falling back to exact at 1%).&lt;/p&gt;

&lt;h2&gt;
  
  
  Which to pick
&lt;/h2&gt;

&lt;p&gt;Two axes — performance and operations:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Situation&lt;/th&gt;
&lt;th&gt;Pick&lt;/th&gt;
&lt;th&gt;Why&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Postgres already deployed, high QPS&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;pgvector&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;1.8× at 8 threads; ACID, backups, and permissions come free&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ML pipeline with frequent embedding regeneration&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;LanceDB&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;20× ingest, Arrow/pandas-native, zero servers&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Filter + vector compound queries dominate&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;LanceDB&lt;/strong&gt; (or pgvector + tuning literacy)&lt;/td&gt;
&lt;td&gt;predictable prefilter; pgvector needs ANALYZE/iterative_scan&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Single-request latency first (agent memory etc.)&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;LanceDB&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;~2× faster at equal recall, in-process, no network&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Many millions of rows, ops team = DBAs&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;pgvector&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;reuse the whole Postgres operational stack&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Measured pgvector 0.8.6 vs LanceDB 0.36.0 on identical data (100k × 1536-dim), identical queries, recall-matched, across four axes&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LanceDB: 22.4× ingest, 11.6× index build, 1/3.2 disk&lt;/strong&gt; — decisive for write-heavy workloads&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LanceDB ~2× faster single-threaded at equal recall; pgvector wins 1.8× at 8 concurrent clients.&lt;/strong&gt; "One client or many" is the fork in the road&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Filtered search is pgvector's minefield&lt;/strong&gt;: recall 0.07 without ANALYZE, non-monotonic plan flips with ef_search — both measured, both fixed by &lt;code&gt;iterative_scan = relaxed_order&lt;/code&gt; + ANALYZE&lt;/li&gt;
&lt;li&gt;LanceDB plateaus at recall ~0.95 without &lt;code&gt;refine_factor&lt;/code&gt; (SQ quantization) — always add it for high-precision requirements&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Future work
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The 1M-scale rerun&lt;/strong&gt; (memory and index-build gaps should widen further)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lance vs Parquet&lt;/strong&gt;: format-level read/write comparison (part of this blog's data engineering series)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unquantized HNSW head-to-head&lt;/strong&gt;: LanceDB's flat-family indexes vs pgvector&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Benchmark code
&lt;/h2&gt;

&lt;p&gt;All scripts (Docker Compose, data prep, ground-truth generation, both benchmarks, chart generation) and the measured JSONs are at &lt;a href="https://github.com/tsho/tsho-lab/tree/main/pgvector-vs-lancedb" rel="noopener noreferrer"&gt;tsho/tsho-lab&lt;/a&gt;. Reproduce with &lt;code&gt;docker compose up&lt;/code&gt; plus three scripts.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/pgvector/pgvector" rel="noopener noreferrer"&gt;pgvector (GitHub)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/lancedb/lancedb" rel="noopener noreferrer"&gt;LanceDB (GitHub — Rust core with Python/TS bindings)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/lance-format/lance" rel="noopener noreferrer"&gt;Lance format (GitHub — the format underneath LanceDB, now governed under the lance-format org)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://lancedb.github.io/lancedb/" rel="noopener noreferrer"&gt;LanceDB Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/pgvector/pgvector/releases" rel="noopener noreferrer"&gt;pgvector 0.8.0 release — iterative index scans&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://huggingface.co/datasets/KShivendu/dbpedia-entities-openai-1M" rel="noopener noreferrer"&gt;DBpedia OpenAI 1M embeddings (Hugging Face)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>postgres</category>
      <category>database</category>
      <category>ai</category>
      <category>performance</category>
    </item>
    <item>
      <title>Why the First Turn in a Coding Agent Can Use So Many Input Tokens — and Why That Gets Better Over Time</title>
      <dc:creator>Sho Tanaka (tsho)</dc:creator>
      <pubDate>Wed, 29 Apr 2026 17:22:26 +0000</pubDate>
      <link>https://dev.to/snowflake/why-the-first-turn-in-a-coding-agent-can-use-so-many-input-tokens-and-why-that-gets-better-over-f8b</link>
      <guid>https://dev.to/snowflake/why-the-first-turn-in-a-coding-agent-can-use-so-many-input-tokens-and-why-that-gets-better-over-f8b</guid>
      <description>&lt;p&gt;Coding agents such as Cortex Code, Claude Code, Codex, and Cursor rely on large language models (LLMs) behind the scenes. A common question from users is: “Why does my first turn consume so many input tokens when I only typed a short prompt?” This post explains how prompt caching works in these systems, why the first turn often looks expensive, and why cache hit rates usually improve as a session continues.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Key point&lt;/strong&gt;: Coding agents like Cortex Code benefit from the same general prompt-caching principles described by Anthropic and OpenAI. Understanding those mechanics helps you interpret token usage more accurately.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  1. Why the First Turn Can Look Expensive
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1-1. Why users notice high input token usage on the first turn
&lt;/h3&gt;

&lt;p&gt;When you start a new session in a coding agent and type something simple like “fix the typo in line 3,” the API usage may show thousands of input tokens — far more than your short message. This is because the total prompt sent to the LLM usually includes much more than your message:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Component&lt;/th&gt;
&lt;th&gt;Typical size (example)&lt;/th&gt;
&lt;th&gt;Changes between turns?&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;System prompt&lt;/td&gt;
&lt;td&gt;2,000–10,000+ tokens&lt;/td&gt;
&lt;td&gt;Usually no&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Tool definitions (file read, write, search, bash, etc.)&lt;/td&gt;
&lt;td&gt;3,000–8,000+ tokens&lt;/td&gt;
&lt;td&gt;Usually no&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Additional instructions and rules&lt;/td&gt;
&lt;td&gt;1,000–5,000+ tokens&lt;/td&gt;
&lt;td&gt;Usually no&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Your message&lt;/td&gt;
&lt;td&gt;10–200 tokens&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Even a 5-word user prompt can result in a 10,000+ token API request because the system prompt, tool definitions, and other static instructions are prepended to every turn.&lt;/p&gt;

&lt;h3&gt;
  
  
  1-2. Why a short user prompt does not mean a small total prompt
&lt;/h3&gt;

&lt;p&gt;The model needs the full relevant context on every turn. In a standard stateless API pattern, that means the system prompt, tools, instructions, and conversation history are sent again with each request. On the first turn, there is typically no reusable cache entry yet, so the request must be processed from scratch. Anthropic’s Messages API follows this explicit multi-turn pattern, where developers construct each turn and manage conversation state themselves.&lt;/p&gt;

&lt;p&gt;This is not a bug or wasted spend. It is how the model preserves context across separate API calls. The good news is that much of this content is repeated across turns, which is exactly what prompt caching is designed to exploit. Anthropic documents prompt caching for repeated prefixes, and OpenAI likewise describes automatic reuse of previously computed prompt prefixes on supported models.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. What Gets Reused Across Turns
&lt;/h2&gt;

&lt;h3&gt;
  
  
  2-1. What makes up the reusable prefix: system prompt, tools, instructions, and history
&lt;/h3&gt;

&lt;p&gt;Prompt caching works by reusing a previously computed prompt prefix.&lt;/p&gt;

&lt;p&gt;A coding-agent request often looks roughly like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Tools] → [System prompt] → [Other static instructions] → [Message history] → [New user message]
 ←—————————————— reusable prefix candidate ——————————————→              ← new content →
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Anthropic, prompt caching applies to the prompt prefix across cacheable blocks such as &lt;code&gt;tools&lt;/code&gt;, &lt;code&gt;system&lt;/code&gt;, and &lt;code&gt;messages&lt;/code&gt;, depending on where the cache breakpoint is placed. Anthropic currently supports both automatic caching and explicit cache breakpoints. With automatic caching, the system manages the breakpoint for you and moves it forward as the conversation grows.&lt;/p&gt;

&lt;p&gt;In OpenAI, prompt caching works automatically on supported models for prompts longer than 1,024 tokens. The API reuses the longest previously computed prompt prefix, starting at 1,024 tokens and increasing in 128-token increments.&lt;/p&gt;

&lt;h3&gt;
  
  
  2-2. The difference between cacheable context, cache writes, and cache hits
&lt;/h3&gt;

&lt;p&gt;There are three practical categories to think about:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Category&lt;/th&gt;
&lt;th&gt;What it means&lt;/th&gt;
&lt;th&gt;Anthropic&lt;/th&gt;
&lt;th&gt;OpenAI&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Cache write / cache creation&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;The prefix is being cached for future reuse&lt;/td&gt;
&lt;td&gt;Billed separately from normal input; pricing depends on the cache setting&lt;/td&gt;
&lt;td&gt;Automatic behavior; no separate cache-write fee&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Cache read / cache hit&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;A previously cached prefix is reused&lt;/td&gt;
&lt;td&gt;Discounted relative to uncached input&lt;/td&gt;
&lt;td&gt;Discounted relative to uncached input&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Uncached input&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Tokens after the reusable prefix, or tokens not served from cache&lt;/td&gt;
&lt;td&gt;Standard input pricing&lt;/td&gt;
&lt;td&gt;Standard input pricing&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;On the first turn, there is usually no prior cache entry, so most or all of the request is effectively new. On later turns, repeated prefix content may be served from cache, depending on factors such as model support, prompt length, retention window, routing, and whether the prefix remains unchanged. Anthropic exposes &lt;code&gt;cache_creation_input_tokens&lt;/code&gt; and &lt;code&gt;cache_read_input_tokens&lt;/code&gt; in usage reporting, while OpenAI exposes cached prompt usage through &lt;code&gt;prompt_tokens_details.cached_tokens&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For Anthropic, the usage formula is documented as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;total_input_tokens = cache_read_input_tokens + cache_creation_input_tokens + input_tokens
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Anthropic also notes that minimum cacheable prompt lengths differ by model family, rather than being a single universal threshold.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Why Cache Hit Rates Improve Over Time
&lt;/h2&gt;

&lt;h3&gt;
  
  
  3-1. Why cache hit rates usually improve after the first few turns
&lt;/h3&gt;

&lt;p&gt;Here is what typically happens in a multi-turn coding-agent session:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Turn&lt;/th&gt;
&lt;th&gt;What happens&lt;/th&gt;
&lt;th&gt;Likely cache behavior&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Turn 1&lt;/td&gt;
&lt;td&gt;System + Tools + User(1)&lt;/td&gt;
&lt;td&gt;Everything is new&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Turn 2&lt;/td&gt;
&lt;td&gt;System + Tools + User(1) + Asst(1) + User(2)&lt;/td&gt;
&lt;td&gt;Earlier shared prefix may be reused; newer content is new&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Turn 3&lt;/td&gt;
&lt;td&gt;System + Tools + User(1) + Asst(1) + User(2) + Asst(2) + User(3)&lt;/td&gt;
&lt;td&gt;A longer shared prefix may be reused&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Turn N&lt;/td&gt;
&lt;td&gt;Full history&lt;/td&gt;
&lt;td&gt;A large fraction of the repeated prefix may be cached&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;In many healthy multi-turn sessions, the cache hit rate improves after the first turn because the repeated prefix gets larger and more stable. That is why judging costs based only on the first turn can be misleading. Anthropic explicitly describes multi-turn caching that moves forward with the conversation, and OpenAI describes reuse of the longest previously computed prefix.&lt;/p&gt;

&lt;p&gt;That said, the exact hit rate will vary. It depends on the model, the prompt length, whether the repeated prefix is identical, how long the session has been idle, and provider-specific routing behavior. So percentages like “60–80% on turn 2” or “80–95% on turn 4+” should be treated as common patterns, not guarantees.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. How to Improve Cache Reuse in Practice
&lt;/h2&gt;

&lt;h3&gt;
  
  
  4-1. How to improve cache hit rates without changing developer behavior too much
&lt;/h3&gt;

&lt;p&gt;Based on how caching works, here are some practical tips:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Keep sessions alive when possible&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Starting a brand-new session often means rebuilding the reusable prefix from scratch. Longer, continuous sessions generally create more opportunities for cache reuse.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Keep the repeated prefix stable&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Prompt caching depends on prefix reuse. If your system prompt, static instructions, or tool definitions change frequently, cache reuse will usually drop.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Avoid unnecessary prompt reordering&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Put the most stable content first and the most dynamic content later. OpenAI’s guidance explicitly recommends placing static content at the beginning and variable content at the end to improve cache effectiveness.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use provider features that preserve cacheability&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Anthropic documents that, in some tool-search workflows, additional tools can be introduced without breaking the cached prefix because the prefix itself remains unchanged.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Let the platform handle cache placement when supported&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
In many cases, prompt caching is automatic. Anthropic supports automatic caching via a top-level &lt;code&gt;cache_control&lt;/code&gt; setting, and OpenAI applies prompt caching automatically on supported models without additional integration changes.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Switching models mid-session usually reduces cache reuse
&lt;/h4&gt;

&lt;p&gt;Prompt caches are provider- and model-specific. If you switch to a different model in the middle of a session, previously reusable prefixes may no longer apply to the new model, and the next request may need to be processed largely from scratch for that model. As a rule of thumb, if you want to maximize cache reuse, it is usually better to stick with one model throughout a session. This follows from the provider documentation being model-specific and from extended retention options being available only for certain OpenAI models.&lt;/p&gt;

&lt;h3&gt;
  
  
  4-2. Common misunderstandings about “high token usage on the first prompt”
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Misunderstanding&lt;/th&gt;
&lt;th&gt;Reality&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;“The first turn used 15K tokens — that’s wasteful”&lt;/td&gt;
&lt;td&gt;Those tokens often include the system prompt, tools, and static instructions that can later be reused through caching&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;“A bigger system prompt is always a bad idea”&lt;/td&gt;
&lt;td&gt;A larger stable prefix may have an upfront cost, but repeated reuse can make it much cheaper over a session&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;“Cache writes are always an extra penalty”&lt;/td&gt;
&lt;td&gt;Anthropic and OpenAI expose caching differently; you should interpret usage using each provider’s own pricing and usage fields&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;“My cache hit rate is low”&lt;/td&gt;
&lt;td&gt;The first turn often has little or no reuse. It is more useful to evaluate cache behavior across the full session&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  5. How to Calculate and Monitor Cache Hit Rates
&lt;/h2&gt;

&lt;h3&gt;
  
  
  5-1. How cache hit rate is calculated
&lt;/h3&gt;

&lt;p&gt;A simple way to think about cache hit rate is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cache_hit_rate = cache_read_input_tokens / (cache_read_input_tokens + cache_creation_input_tokens + input_tokens)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This formula maps cleanly onto Anthropic’s usage fields. For OpenAI, you typically estimate cache reuse using &lt;code&gt;prompt_tokens_details.cached_tokens&lt;/code&gt; relative to total prompt tokens. Anthropic and OpenAI expose different fields, so the exact calculation is provider-specific.&lt;/p&gt;

&lt;p&gt;In a healthy multi-turn coding session, you will often see this pattern:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Turn 1&lt;/strong&gt;: little or no cache reuse&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Turn 2–3&lt;/strong&gt;: noticeable reuse if the prefix is stable&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Later turns&lt;/strong&gt;: higher reuse if the conversation remains active and the repeated prefix continues to match&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  5-2. How to monitor cache hit rates in practice
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Anthropic API&lt;/strong&gt; returns usage fields like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"cache_creation_input_tokens"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;12500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"cache_read_input_tokens"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"input_tokens"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"output_tokens"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On an early turn, &lt;code&gt;cache_read_input_tokens&lt;/code&gt; may be 0 while &lt;code&gt;cache_creation_input_tokens&lt;/code&gt; is high. On later turns, &lt;code&gt;cache_read_input_tokens&lt;/code&gt; may grow as more of the repeated prefix is reused.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;OpenAI API&lt;/strong&gt; returns fields like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"prompt_tokens"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;2006&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"prompt_tokens_details"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"cached_tokens"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1920&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"completion_tokens"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;cached_tokens&lt;/code&gt; field indicates how many prompt tokens were served from cache. OpenAI documents automatic prompt caching on supported models for prompts longer than 1,024 tokens.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cache retention&lt;/strong&gt; also matters.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Anthropic&lt;/strong&gt;: default 5-minute cache lifetime, with a 1-hour option available. Anthropic also notes that cache behavior is managed either automatically or via explicit cache breakpoints.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;OpenAI&lt;/strong&gt;: in-memory cached prefixes are typically retained for a short inactive window, and extended prompt cache retention of up to 24 hours is available for supported models.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If your session sits idle long enough for the retention window to expire, the next request may behave more like a fresh cache write or a cache miss. In interactive coding sessions, though, users often stay well within these windows.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Final Takeaway
&lt;/h2&gt;

&lt;p&gt;If your coding agent shows high input token usage on the first turn, that does not necessarily mean the system is inefficient. In many cases, you are seeing the cost of sending and processing the full reusable context: tools, system instructions, static guidance, and conversation history. Prompt caching exists precisely to make those repeated prefixes cheaper and faster on later turns. Anthropic and OpenAI both document prefix-based caching that rewards stable repeated context over multi-turn sessions.&lt;/p&gt;

&lt;p&gt;So when evaluating token usage in a coding agent, do not judge the session by the first turn alone. Look at the full conversation. The first turn often establishes the reusable prefix; later turns are where prompt caching usually starts to pay off.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://platform.claude.com/docs/en/build-with-claude/prompt-caching" rel="noopener noreferrer"&gt;Anthropic Claude API docs — Prompt caching&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-use-with-prompt-caching" rel="noopener noreferrer"&gt;Anthropic Claude API docs — Tool use with prompt caching&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://claude.com/blog/prompt-caching" rel="noopener noreferrer"&gt;Anthropic blog — Prompt caching with Claude&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developers.openai.com/api/docs/guides/prompt-caching" rel="noopener noreferrer"&gt;OpenAI docs — Prompt caching&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://openai.com/index/api-prompt-caching/" rel="noopener noreferrer"&gt;OpenAI blog — Prompt Caching in the API&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/anthropics/claude-cookbooks/blob/main/misc/prompt_caching.ipynb" rel="noopener noreferrer"&gt;Anthropic cookbook — prompt_caching.ipynb&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/openai/openai-cookbook/blob/main/examples/Prompt_Caching101.ipynb" rel="noopener noreferrer"&gt;OpenAI cookbook — Prompt_Caching101.ipynb&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ai</category>
      <category>snowflake</category>
      <category>claude</category>
      <category>openai</category>
    </item>
  </channel>
</rss>
