<?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: Ivaylo Nikolov</title>
    <description>The latest articles on DEV Community by Ivaylo Nikolov (@divoto).</description>
    <link>https://dev.to/divoto</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%2F4236616%2F3f9939f0-66e2-4980-832e-e171c8b25a2b.jpg</url>
      <title>DEV Community: Ivaylo Nikolov</title>
      <link>https://dev.to/divoto</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/divoto"/>
    <language>en</language>
    <item>
      <title>Your Laravel jobs are running twice: retry_after vs timeout</title>
      <dc:creator>Ivaylo Nikolov</dc:creator>
      <pubDate>Mon, 30 Nov 2026 11:23:24 +0000</pubDate>
      <link>https://dev.to/divoto/your-laravel-jobs-are-running-twice-retryafter-vs-timeout-38oc</link>
      <guid>https://dev.to/divoto/your-laravel-jobs-are-running-twice-retryafter-vs-timeout-38oc</guid>
      <description>&lt;p&gt;NewSumm reads news from hundreds of outlets and groups articles about the same event into one story. All of that happens in queued jobs: fetching feeds, grouping articles, writing summaries. Some of those jobs take several minutes.&lt;/p&gt;

&lt;p&gt;For a while, our failed jobs table kept filling up. &lt;strong&gt;726 failures&lt;/strong&gt;, nearly all of them &lt;code&gt;TimeoutExceededException&lt;/code&gt;, spread across fetching, grouping and summarising. Nothing was obviously broken. The jobs worked when we ran them by hand. They just kept failing in production, and the summaries kept costing more than they should.&lt;/p&gt;

&lt;p&gt;The cause was two numbers in two different files.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two timers that sound like the same thing
&lt;/h2&gt;

&lt;p&gt;Laravel queues have two settings that are easy to mix up.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The job timeout&lt;/strong&gt; is how long a single job is allowed to run before the worker stops it. You set it on the job class, or with &lt;code&gt;--timeout&lt;/code&gt; on &lt;code&gt;queue:work&lt;/code&gt; (the default is 60 seconds):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ClusterArticlesJob&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;ShouldQueue&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$timeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;600&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;code&gt;retry_after&lt;/code&gt;&lt;/strong&gt; is set per connection in &lt;code&gt;config/queue.php&lt;/code&gt;. It is how long the queue waits for a job it has handed out before deciding the worker must have died, and handing the job to someone else:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="s1"&gt;'database'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s1"&gt;'driver'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'database'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'queue'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'default'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'retry_after'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;90&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;90&lt;/code&gt; is the value a new Laravel app ships with. It is fine while every job finishes in well under a minute and a half.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Warning:&lt;/strong&gt; &lt;code&gt;retry_after&lt;/code&gt; does not stop the first worker. It only gives the same job to another worker while the first one is still busy with it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What happens when retry_after is shorter
&lt;/h2&gt;

&lt;p&gt;Our grouping job could run for up to 600 seconds. With &lt;code&gt;retry_after&lt;/code&gt; at 90, this is what one job looked like:&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%2Fnhmrrts41gs3mhad52kf.png" 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%2Fnhmrrts41gs3mhad52kf.png" alt="Timeline: worker 1 runs the job; at 90 seconds the queue hands the same job to worker 2, at 180 seconds to worker 3" width="800" height="316"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Worker 1 picks up the job and starts working.&lt;/li&gt;
&lt;li&gt;At 90 seconds the job is still running, which is normal. The queue assumes worker 1 has died and makes the job available again.&lt;/li&gt;
&lt;li&gt;Worker 2 picks it up and starts the same work from the beginning. So does worker 3, 90 seconds later.&lt;/li&gt;
&lt;li&gt;Each pick-up counts as an attempt. Eventually the copies either hit their timeout or run out of attempts, and the job lands in &lt;code&gt;failed_jobs&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Meanwhile every copy was doing real work. Stories were grouped twice, and summaries were written two or three times over, each one a paid API call. The failures were the visible part. The duplicate work was the expensive part.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why nobody spots it
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The two numbers live far apart.&lt;/strong&gt; One is a property on a job class in &lt;code&gt;app/Jobs&lt;/code&gt;, the other a line in &lt;code&gt;config/queue.php&lt;/code&gt;. Nothing connects them, so no code review will notice.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It works locally.&lt;/strong&gt; In development you often use the &lt;code&gt;sync&lt;/code&gt; driver, or only one worker, so there is nobody to hand the job to.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The error is misleading.&lt;/strong&gt; &lt;code&gt;TimeoutExceededException&lt;/code&gt; sends you looking for slow code. The job wasn't too slow; it was being run several times at once.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unique jobs don't help.&lt;/strong&gt; &lt;code&gt;ShouldBeUnique&lt;/code&gt; stops a second copy being &lt;em&gt;dispatched&lt;/em&gt;. Here nothing new was dispatched. The queue handed out the same job again.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The rule
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; Every job's timeout must be shorter than the &lt;code&gt;retry_after&lt;/code&gt; of the connection it runs on, with a margin. Laravel's docs say the same about &lt;code&gt;--timeout&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We raised &lt;code&gt;retry_after&lt;/code&gt; to 1200 seconds on every connection that has one, and made it configurable per environment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="s1"&gt;'retry_after'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;env&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'DB_QUEUE_RETRY_AFTER'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is a trade-off. If a worker really does die (an out-of-memory kill, a server restart), its job now waits up to 20 minutes before it is retried. For us that is fine: the scheduled jobs run again on their own soon after. If your app has both quick jobs that must recover fast and slow jobs that must not be duplicated, put the slow ones on their own connection with its own &lt;code&gt;retry_after&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;(SQS doesn't use &lt;code&gt;retry_after&lt;/code&gt;; the queue's visibility timeout plays the same role, and the same rule applies.)&lt;/p&gt;

&lt;h2&gt;
  
  
  Making sure it can't come back
&lt;/h2&gt;

&lt;p&gt;The fix was one line. The real risk was someone adding a slow job next year and bringing the bug straight back, because nothing connects the two numbers. So we wrote a test that does:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'keeps retry_after above every job timeout'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$job&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$timeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nv"&gt;$job&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nf"&gt;collect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;config&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'queue.connections'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$connection&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;isset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$connection&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'retry_after'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$connection&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$connection&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'retry_after'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;toBeGreaterThan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$timeout&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"{$job} timeout must be below the '{$name}' retry_after"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nc"&gt;ClusterArticlesJob&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;GenerateEmbeddingsJob&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;SummarisePendingClustersJob&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now raising a job's timeout past &lt;code&gt;retry_after&lt;/code&gt; fails the build with a message that says exactly what to change.&lt;/p&gt;

&lt;h2&gt;
  
  
  One more trap in the same place
&lt;/h2&gt;

&lt;p&gt;While we were in there, we found a second one. A worker started with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;php artisan queue:work --queue=default,news
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;does not share its time between the two queues. The list is a &lt;strong&gt;priority order&lt;/strong&gt;: the worker empties &lt;code&gt;default&lt;/code&gt; completely before it takes a single job from &lt;code&gt;news&lt;/code&gt;. If &lt;code&gt;default&lt;/code&gt; is refilled every few minutes and holds long jobs, &lt;code&gt;news&lt;/code&gt; can wait forever. Nothing fails and nothing is logged; the jobs just sit there.&lt;/p&gt;

&lt;p&gt;If two queues matter equally, give each one its own worker.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;[ ] Find your longest job timeout (&lt;code&gt;$timeout&lt;/code&gt; on the class, or the worker's &lt;code&gt;--timeout&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;[ ] Make sure every connection's &lt;code&gt;retry_after&lt;/code&gt; is comfortably above it.&lt;/li&gt;
&lt;li&gt;[ ] Add a test that checks this, so the next slow job can't quietly break it.&lt;/li&gt;
&lt;li&gt;[ ] Check what your &lt;code&gt;--queue=&lt;/code&gt; lists actually mean: they are priorities, not a fair share.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;NewSumm groups news from hundreds of outlets into single stories, with a short summary and a link to every source. You can &lt;a href="https://newsumm.com" rel="noopener noreferrer"&gt;see it in action on today's top stories&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://newsumm.com/blog/laravel-queue-retry-after-vs-timeout" rel="noopener noreferrer"&gt;newsumm.com&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>queues</category>
      <category>debugging</category>
    </item>
    <item>
      <title>Building a news pipeline with the Laravel AI SDK: agents, structured output and the pitfalls nobody warns you about</title>
      <dc:creator>Ivaylo Nikolov</dc:creator>
      <pubDate>Tue, 17 Nov 2026 11:56:24 +0000</pubDate>
      <link>https://dev.to/divoto/building-a-news-pipeline-with-the-laravel-ai-sdk-agents-structured-output-and-the-pitfalls-nobody-49la</link>
      <guid>https://dev.to/divoto/building-a-news-pipeline-with-the-laravel-ai-sdk-agents-structured-output-and-the-pitfalls-nobody-49la</guid>
      <description>&lt;p&gt;NewSumm reads news from hundreds of outlets, groups articles about the same event into one story, and writes a short summary of what the outlets agree on and where they differ. On a normal day that is tens of thousands of articles.&lt;/p&gt;

&lt;p&gt;The whole pipeline runs on the &lt;a href="https://github.com/laravel/ai" rel="noopener noreferrer"&gt;Laravel AI SDK&lt;/a&gt; (&lt;code&gt;laravel/ai&lt;/code&gt;): embeddings for every article, and agents that summarise stories, describe each outlet's angle and extract the people and places a story is about. This post is about the Laravel side: how the code is organised, what the SDK gives you for free, and the three failures that cost us real time because no test could see them.&lt;/p&gt;

&lt;p&gt;If you want the maths behind the grouping (embeddings, running centroids, how the clusters are distributed), that is in &lt;a href="https://dev.to/blog/clustering-the-news-with-cheap-models"&gt;Clustering the news with cheap models&lt;/a&gt;. Here we stay in PHP.&lt;/p&gt;

&lt;p&gt;The code below is simplified from our real classes. The prompts, models and thresholds are left out on purpose; the structure is what transfers.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The shape of the pipeline
&lt;/h2&gt;

&lt;p&gt;Every step is a queued job:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Fetch&lt;/strong&gt; feeds and store new articles.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Embed&lt;/strong&gt; each article: one vector from its title and description.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cluster&lt;/strong&gt; articles into stories by vector similarity, in Postgres with pgvector.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Summarise&lt;/strong&gt; each story that more than one article has joined.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Only step 4 calls a language model, and only for multi-article stories. In a two-day sample of 51,842 articles, 4,748 stories had two or more articles: &lt;strong&gt;9.2%&lt;/strong&gt; of the article count. Most of the cost decision is made before any agent runs: send the model &lt;em&gt;stories&lt;/em&gt;, not &lt;em&gt;articles&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Embeddings: batch them, and guard the input
&lt;/h2&gt;

&lt;p&gt;The SDK's embeddings API is one line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Laravel&lt;/span&gt;\&lt;span class="nc"&gt;Ai&lt;/span&gt;\&lt;span class="nc"&gt;Embeddings&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Embeddings&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$texts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;generate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$vectors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$response&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;embeddings&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// one array of floats per input, same order&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We embed in chunks of 50 so one HTTP request covers 50 articles. The call itself was never the problem. The input was.&lt;/p&gt;

&lt;p&gt;Feed data is messy, and a single bad string makes the provider return a &lt;strong&gt;400 for the whole batch&lt;/strong&gt;. We hit three:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Too long.&lt;/strong&gt; Some feeds put the full article in the description. We cap the text well under the provider's token limit, and use a lower cap than the usual "four characters per token" rule suggests, because dense non-Latin scripts use far more tokens per character.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Empty.&lt;/strong&gt; No title, no description. Skip it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Looks empty, isn't.&lt;/strong&gt; Titles made only of a non-breaking space or a zero-width space. &lt;code&gt;trim()&lt;/code&gt; and &lt;code&gt;filled()&lt;/code&gt; both let these through, so the provider sees an empty string and rejects the batch.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;isBlank&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$text&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nf"&gt;preg_match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'/^[\p{C}\p{Z}]+$/u'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The third one hurt most. A rejected batch left its articles marked unprocessed, so they were retried on every run, and the job (then oldest-first) never got past them. Fresh articles stopped being embedded, so nothing clustered, so nothing was summarised. The site simply stopped showing new stories. Two fixes: guard the input, and process &lt;strong&gt;newest first&lt;/strong&gt;, so one poisoned article can never hold up today's news.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Tip&lt;/p&gt;
&lt;p&gt;
When a batch call can fail on a single item, mark items you can't process as &lt;em&gt;done, with no result&lt;/em&gt; rather than leaving them in the queue. Retrying a bad input forever is a slow outage.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  3. Agents are just classes
&lt;/h2&gt;

&lt;p&gt;An agent in the SDK is a class that implements &lt;code&gt;Agent&lt;/code&gt; and uses the &lt;code&gt;Promptable&lt;/code&gt; trait. Instructions live in a method, so they're versioned, reviewed and tested like any other code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt;\&lt;span class="nc"&gt;Ai&lt;/span&gt;\&lt;span class="nc"&gt;Agents&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Laravel&lt;/span&gt;\&lt;span class="nc"&gt;Ai&lt;/span&gt;\&lt;span class="nc"&gt;Attributes&lt;/span&gt;\&lt;span class="nc"&gt;UseCheapestModel&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Laravel&lt;/span&gt;\&lt;span class="nc"&gt;Ai&lt;/span&gt;\&lt;span class="nc"&gt;Contracts&lt;/span&gt;\&lt;span class="nc"&gt;Agent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Laravel&lt;/span&gt;\&lt;span class="nc"&gt;Ai&lt;/span&gt;\&lt;span class="nc"&gt;Promptable&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Stringable&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;#[UseCheapestModel]&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;StorySummaryAgent&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Agent&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Promptable&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;__construct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nv"&gt;$compareOutlets&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;instructions&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Stringable&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s1"&gt;'You are a news editor. Summarise what these articles agree on...'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two things here earned their place.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;#[UseCheapestModel]&lt;/code&gt;&lt;/strong&gt; tells the SDK to use the provider's cheapest text model. Our tasks are narrow (summarise, compare, fill a schema), and small models do them well. The attribute keeps model names out of our code: switch provider, and every agent follows.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Constructor arguments&lt;/strong&gt; flow through &lt;code&gt;make()&lt;/code&gt;, so one agent can vary its behaviour per call without a second class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;StorySummaryAgent&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;make&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;compareOutlets&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$story&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;hasEnoughSources&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;articlesPrompt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$articles&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When &lt;code&gt;compareOutlets&lt;/code&gt; is on, the agent's instructions and schema both gain a "where coverage differs" section. One class, one test file, two behaviours.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Structured output turns a writer into an extractor
&lt;/h2&gt;

&lt;p&gt;Free text is hard to store. We want a headline, a summary, key points, a category from a fixed list, one angle per outlet, and the people and places involved. Implement &lt;code&gt;HasStructuredOutput&lt;/code&gt; and describe the shape:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate&lt;/span&gt;\&lt;span class="nc"&gt;Contracts&lt;/span&gt;\&lt;span class="nc"&gt;JsonSchema&lt;/span&gt;\&lt;span class="nc"&gt;JsonSchema&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Laravel&lt;/span&gt;\&lt;span class="nc"&gt;Ai&lt;/span&gt;\&lt;span class="nc"&gt;Contracts&lt;/span&gt;\&lt;span class="nc"&gt;HasStructuredOutput&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;StorySummaryAgent&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Agent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;HasStructuredOutput&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;JsonSchema&lt;/span&gt; &lt;span class="nv"&gt;$schema&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="s1"&gt;'headline'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$schema&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;required&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'summary'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$schema&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;required&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'key_points'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$schema&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;items&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$schema&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;required&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'category'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$schema&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="k"&gt;enum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Category&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;values&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;required&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;compareOutlets&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
                &lt;span class="s1"&gt;'coverage_differences'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$schema&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;required&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The response reads like an array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$story&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s1"&gt;'headline'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$response&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'headline'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'summary'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$response&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'summary'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'category'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$response&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'category'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An &lt;code&gt;enum&lt;/code&gt; on the category means the model picks from our list instead of inventing "Tech &amp;amp; Science" one day and "Technology" the next. We still validate what comes back (a schema constrains shape, not truth), but the classes of garbage shrink a lot.&lt;/p&gt;

&lt;h3&gt;
  
  
  Share rules and schema between agents
&lt;/h3&gt;

&lt;p&gt;Entity extraction came later than summaries. We didn't want to re-summarise thousands of published stories just to get their people and places, so extraction has its own agent for backfilling, and the &lt;em&gt;same&lt;/em&gt; rules and fields are spread into the summary agent so new stories get both in one call:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EntityExtractionAgent&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Agent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;HasStructuredOutput&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Promptable&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nc"&gt;Rules&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="s1"&gt;'PROMPT'&lt;/span&gt;
        &lt;span class="nc"&gt;Record&lt;/span&gt; &lt;span class="n"&gt;who&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;story&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;about&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;where&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="n"&gt;happened&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
        &lt;span class="no"&gt;PROMPT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;JsonSchema&lt;/span&gt; &lt;span class="nv"&gt;$schema&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kt"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$schema&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;JsonSchema&lt;/span&gt; &lt;span class="nv"&gt;$schema&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'entities'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="cm"&gt;/* people + places */&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// In StorySummaryAgent::schema():&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;EntityExtractionAgent&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$schema&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One source of truth for the rules, two agents using it, and backfills never touch a published summary.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. The strict-schema trap
&lt;/h2&gt;

&lt;p&gt;This one put our daily recap out for a day, and the whole test suite stayed green.&lt;/p&gt;

&lt;p&gt;Our provider's structured-output mode is &lt;strong&gt;strict&lt;/strong&gt;: every object in the schema must say &lt;code&gt;additionalProperties: false&lt;/code&gt; and list all its properties as required. The SDK sends requests in strict mode and adds &lt;code&gt;additionalProperties: false&lt;/code&gt; to the &lt;strong&gt;root&lt;/strong&gt; object for you. It does not add it to &lt;strong&gt;nested&lt;/strong&gt; objects.&lt;/p&gt;

&lt;p&gt;So this works:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="s1"&gt;'key_points'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$schema&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;items&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$schema&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and this is rejected with a 400 on every single call:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="s1"&gt;'beats'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$schema&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;items&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$schema&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s1"&gt;'title'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$schema&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;required&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'text'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$schema&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;required&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The fix is one method call on each nested object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="s1"&gt;'beats'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$schema&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;items&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$schema&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s1"&gt;'title'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$schema&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;required&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'text'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$schema&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;required&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;withoutAdditionalProperties&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three things made it worse than it needed to be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Fakes don't validate schemas.&lt;/strong&gt; &lt;code&gt;Agent::fake()&lt;/code&gt; never talks to the provider, so it can't apply the provider's rules. Every test passed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The job swallowed the error.&lt;/strong&gt; It caught the exception, logged a warning, skipped the category and published an &lt;em&gt;empty&lt;/em&gt; recap. Nothing reached &lt;code&gt;failed_jobs&lt;/code&gt;; it looked like a quiet news day.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The log hid the reason.&lt;/strong&gt; The exception message was truncated before the part of the response body that named the bad field.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now a test compiles every agent's real schema, exactly as the SDK will send it, and walks it recursively:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate&lt;/span&gt;\&lt;span class="nc"&gt;JsonSchema&lt;/span&gt;\&lt;span class="nc"&gt;JsonSchemaTypeFactory&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Laravel&lt;/span&gt;\&lt;span class="nc"&gt;Ai&lt;/span&gt;\&lt;span class="nc"&gt;ObjectSchema&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;assertStrictModeCompatible&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$schema&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'root'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$schema&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'additionalProperties'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;toBeFalse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Object at {$path} is missing additionalProperties: false"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$schema&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'required'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;toEqualCanonicalizing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;array_keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$schema&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'properties'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Object at {$path} must mark every property required"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$schema&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'properties'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$name&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$property&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$property&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'type'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'object'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;assertStrictModeCompatible&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$property&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"{$path}.{$name}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$property&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'type'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'array'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$property&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'items'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'type'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'object'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;assertStrictModeCompatible&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$property&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'items'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"{$path}.{$name}[]"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'compiles a schema strict mode accepts'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$agent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$schema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ObjectSchema&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nv"&gt;$agent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;JsonSchemaTypeFactory&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;toSchema&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nf"&gt;assertStrictModeCompatible&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$schema&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nc"&gt;StorySummaryAgent&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;EntityExtractionAgent&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;DailyRecapAgent&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No API call, runs in milliseconds, and it fails the build the moment someone adds a nested object without the fix.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Warning&lt;/p&gt;
&lt;p&gt;
If a job calls a model in a loop and catches errors per item, alert when &lt;em&gt;every&lt;/em&gt; item fails. All items failing within seconds is a rejected request, not a bad generation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  6. Agents in queues: mind &lt;code&gt;retry_after&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Summaries run in queued jobs, and a model call can take a while. If a job runs longer than the queue connection's &lt;code&gt;retry_after&lt;/code&gt;, Laravel hands it to a second worker while the first is still running, and you pay for the same summary twice. Ours shipped with the default &lt;code&gt;retry_after&lt;/code&gt; of 90 seconds against job timeouts of several minutes. That produced 726 failed jobs and a lot of duplicate model calls before we spotted it.&lt;/p&gt;

&lt;p&gt;Rule of thumb: &lt;code&gt;retry_after&lt;/code&gt; must be longer than the longest job timeout on that connection. We wrote that one up in full: &lt;a href="https://dev.to/blog/laravel-queue-retry-after-vs-timeout"&gt;Your Laravel jobs are running twice: retry_after vs timeout&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Knowing what it costs
&lt;/h2&gt;

&lt;p&gt;Provider dashboards tell you what you spent. They don't tell you &lt;em&gt;which agent&lt;/em&gt; spent it. The SDK fires an event after every call, with the token usage attached, so a listener can record each one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Laravel&lt;/span&gt;\&lt;span class="nc"&gt;Ai&lt;/span&gt;\&lt;span class="nc"&gt;Events&lt;/span&gt;\&lt;span class="nc"&gt;AgentPrompted&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Laravel&lt;/span&gt;\&lt;span class="nc"&gt;Ai&lt;/span&gt;\&lt;span class="nc"&gt;Events&lt;/span&gt;\&lt;span class="nc"&gt;EmbeddingsGenerated&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RecordAiUsage&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleAgentPrompted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;AgentPrompted&lt;/span&gt; &lt;span class="nv"&gt;$event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$usage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$event&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;usage&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="s1"&gt;'agent'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;class_basename&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$event&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;prompt&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;agent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'model'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$event&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;prompt&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'input_tokens'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$usage&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;promptTokens&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'cached_input_tokens'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$usage&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;cacheReadInputTokens&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'output_tokens'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$usage&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;completionTokens&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleEmbeddingsGenerated&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;EmbeddingsGenerated&lt;/span&gt; &lt;span class="nv"&gt;$event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="s1"&gt;'agent'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'embeddings'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'model'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$event&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'input_tokens'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$event&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;tokens&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$attributes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;AiUsage&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$attributes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Throwable&lt;/span&gt; &lt;span class="nv"&gt;$e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;warning&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Could not record AI usage: {$e-&amp;gt;getMessage()}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Laravel's event discovery picks up the &lt;code&gt;handle*&lt;/code&gt; methods by their type hints, so there is nothing to register. Note the &lt;code&gt;try&lt;/code&gt;/&lt;code&gt;catch&lt;/code&gt;: bookkeeping must never fail the call it records. A summary you have already paid for is worth more than its usage row.&lt;/p&gt;

&lt;p&gt;On top of that table sits an Artisan command, &lt;code&gt;php artisan ai:usage&lt;/code&gt;, that groups tokens by agent (or by hour, with &lt;code&gt;--hourly&lt;/code&gt;) and multiplies them by per-model prices from config.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Testing with fakes, and their limits
&lt;/h2&gt;

&lt;p&gt;For everything that isn't the provider contract, the SDK's fakes are excellent:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'stores the overview the agent writes'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;EntityOverviewAgent&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;fake&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'overview'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"First paragraph.\n\nSecond paragraph."&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// ... run the job ...&lt;/span&gt;

    &lt;span class="nc"&gt;EntityOverviewAgent&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;assertPrompted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$prompt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$prompt&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Kyiv'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'skips articles with blank titles'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Embeddings&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;fake&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// ... embed a batch containing "\u{00A0}" as a title ...&lt;/span&gt;

    &lt;span class="nc"&gt;Embeddings&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;assertGenerated&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$prompt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$prompt&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;inputs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;49&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fakes answer "does my code do the right thing with a response?". They can't answer "will the provider accept my request?". For that you need either a contract test like the schema walker above, or one small real call in a scheduled smoke check. We have the first and wish we'd had it sooner.&lt;/p&gt;

&lt;h2&gt;
  
  
  9. What we'd tell someone starting today
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Decide what the model reads before choosing a model.&lt;/strong&gt; Sending stories instead of articles cut our calls by an order of magnitude. No model choice comes close.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Make agents classes, with their instructions in code.&lt;/strong&gt; They get reviewed, diffed and tested like everything else.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use structured output for anything you store&lt;/strong&gt;, with enums wherever the answer comes from a list.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Test the schema you send, not just the response you fake.&lt;/strong&gt; Nested objects need &lt;code&gt;withoutAdditionalProperties()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Guard embedding input&lt;/strong&gt; against long, empty and invisible-whitespace text, and never let one bad item block a batch forever.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Record tokens per agent from day one.&lt;/strong&gt; It's one listener, and it tells you where the money actually goes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Never swallow a model error silently.&lt;/strong&gt; Log the response body, and alert when every item fails.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The SDK makes the happy path short. Most of the work was in the unhappy paths, and those are the parts worth writing down.&lt;/p&gt;

</description>
      <category>agents</category>
      <category>ai</category>
      <category>laravel</category>
      <category>php</category>
    </item>
    <item>
      <title>Clustering the news with cheap models: a measured look at embeddings, online centroids and entity extraction</title>
      <dc:creator>Ivaylo Nikolov</dc:creator>
      <pubDate>Tue, 10 Nov 2026 08:37:14 +0000</pubDate>
      <link>https://dev.to/divoto/clustering-the-news-with-cheap-models-a-measured-look-at-embeddings-online-centroids-and-entity-4hbf</link>
      <guid>https://dev.to/divoto/clustering-the-news-with-cheap-models-a-measured-look-at-embeddings-online-centroids-and-entity-4hbf</guid>
      <description>&lt;p&gt;A news aggregator has a deceptively simple job: notice when forty outlets are writing about the same event, and show it once. Underneath, that job is an online clustering problem on a stream of short, noisy, multilingual texts, with a hard budget on cost per item.&lt;/p&gt;

&lt;p&gt;The usual instinct is to throw a large language model at it. That does not scale. A large model reading every incoming article is the most expensive possible design, and most of what it reads is noise: most articles are about something nobody else is covering.&lt;/p&gt;

&lt;p&gt;This post describes the design we arrived at for NewSumm and, more usefully, measures it. Every figure below comes from our own pipeline's output for a two-day window (3–4 November 2026): &lt;strong&gt;51,842 articles&lt;/strong&gt;, which became &lt;strong&gt;34,929 stories&lt;/strong&gt;. Where we analyse extracted people and places, the window runs to 9 November.&lt;/p&gt;

&lt;p&gt;The short version: almost all of the work is done by geometry, not by language models. Embeddings cost a few dollars per million articles. Language models only see the narrow end of a long funnel, so their cost scales with the number of &lt;em&gt;stories&lt;/em&gt;, not the number of &lt;em&gt;articles&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; This is a description of the method and its measured behaviour, not a recipe. We deliberately leave out our thresholds, model choices, prompts and tuning, which are specific to our data and change as it does.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  1. The problem, stated precisely
&lt;/h2&gt;

&lt;p&gt;Let articles arrive as a stream x₁, x₂, x₃, … Each must be assigned, on arrival, to an existing story or to a new one. The constraints:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Online.&lt;/strong&gt; We cannot wait for the day to finish and run a batch algorithm. A story should appear minutes after its first articles.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unknown k.&lt;/strong&gt; The number of stories is not fixed. On a typical day thousands are born and most never grow.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Short, uneven input.&lt;/strong&gt; We only have what feeds provide: a headline and usually a sentence or two. In our sample the median input is 225 characters, the mean 471.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Duplicates that aren't independent.&lt;/strong&gt; The same agency copy is reprinted by many outlets. Ten articles can be one report.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cost per item must be tiny.&lt;/strong&gt; Whatever touches every article must cost a fraction of a cent.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is the &lt;em&gt;topic detection and tracking&lt;/em&gt; (TDT) problem studied since the late 1990s [1], with modern tools.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Representation: one cheap vector per article
&lt;/h2&gt;

&lt;p&gt;Each article is mapped once to a dense embedding vector &lt;strong&gt;x&lt;/strong&gt; ∈ ℝᵈ from a small, inexpensive embedding model, using only its headline and description. Similarity is cosine similarity:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cos(x, y) = (x · y) / (‖x‖ ‖y‖)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With unit-normalised vectors this is just the dot product, and cosine distance is 1 − cos(x, y).&lt;/p&gt;

&lt;p&gt;The economics are the first surprise. At a mean of roughly 120 tokens per article and current list prices for small embedding models (a few cents per million tokens), embedding &lt;strong&gt;a million articles costs in the order of a few dollars&lt;/strong&gt;. Embedding is the only model call made for every article. Everything downstream is arithmetic and an index lookup.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does the geometry separate stories?
&lt;/h3&gt;

&lt;p&gt;Before trusting a single threshold, look at the distributions. For 2,500 randomly sampled articles from multi-article stories, we measured three quantities:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Unrelated pairs:&lt;/strong&gt; the similarity between two randomly chosen articles from different stories.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Nearest other story:&lt;/strong&gt; the similarity between the article and the &lt;em&gt;closest story it does not belong to&lt;/em&gt;. These are the hard negatives: the decision the algorithm actually has to make.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Own story:&lt;/strong&gt; the similarity between the article and the centroid of the story it ended up in.&lt;/li&gt;
&lt;/ol&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%2Fnnjf64tmo9s1togjl3ww.png" 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%2Fnnjf64tmo9s1togjl3ww.png" alt="Density of cosine similarity for three groups: unrelated article pairs peak near 0.1; an article versus its nearest other story peaks near 0.73; an article versus its own story peaks near 0.9" width="800" height="442"&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;Quantity&lt;/th&gt;
&lt;th&gt;Median&lt;/th&gt;
&lt;th&gt;Tail&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Unrelated pair&lt;/td&gt;
&lt;td&gt;0.10&lt;/td&gt;
&lt;td&gt;99th percentile 0.33&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Article vs nearest other story&lt;/td&gt;
&lt;td&gt;0.73&lt;/td&gt;
&lt;td&gt;95th percentile 0.86&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Article vs its own story&lt;/td&gt;
&lt;td&gt;0.88&lt;/td&gt;
&lt;td&gt;5th percentile 0.77&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Three things stand out.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Unrelated news is nowhere near a decision boundary.&lt;/strong&gt; Random pairs sit around 0.1 and almost never pass 0.35. Any threshold sensible enough to group stories will never confuse a football result with a central bank decision.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The real difficulty is the nearest neighbour, not the average.&lt;/strong&gt; The nearest &lt;em&gt;other&lt;/em&gt; story sits at 0.73, uncomfortably close to the own-story distribution. These neighbours are mostly genuinely related: the same war on a different day, the same company in a different deal, the same league on a different match. A cosine threshold is a decision about &lt;em&gt;granularity&lt;/em&gt; (what counts as "the same event") as much as about relevance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The distributions overlap, so a single global threshold cannot be perfect.&lt;/strong&gt; About 3.4% of nearest-other similarities are at or above the median own-story similarity. No threshold separates them cleanly. This is where the algorithm needs structure beyond the threshold: time, story maturity and a second pass (section 4).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; Plot these three distributions on your own data before choosing a threshold. The random-pair curve tells you nothing; the nearest-other curve is the one your threshold has to beat.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  3. Online clustering with running centroids
&lt;/h2&gt;

&lt;p&gt;The core algorithm is a single-pass, threshold-based scheme (a "leader" or sequential clustering algorithm), refined for news:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for each new article x:
    candidates ← approximate nearest story centroids to x
    keep candidates that are still "live" (recent enough)
    if the best candidate passes the similarity rule:
        add x to it, update its centroid
    else:
        start a new story with centroid x
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Centroids as running means
&lt;/h3&gt;

&lt;p&gt;A story k with nₖ members has centroid μₖ, the mean of its member embeddings. Recomputing that mean from scratch on every insertion costs O(nₖ·d), which makes building a story of n articles O(n²·d). The running-mean update is exact and costs O(d):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;μₖ⁽ⁿ⁾ = μₖ⁽ⁿ⁻¹⁾ + (x − μₖ⁽ⁿ⁻¹⁾) / n
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When two stories A and B merge, the combined centroid is the size-weighted mean, which again equals the from-scratch recomputation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;μ_AB = (n_A μ_A + n_B μ_B) / (n_A + n_B)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A useful property follows for unit-normalised embeddings. The squared norm of a centroid is the mean pairwise similarity of its members (self-pairs included):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;‖μ‖² = (1/n²) Σᵢ Σⱼ xᵢ · xⱼ
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the length of the centroid is a free measure of &lt;strong&gt;cohesion&lt;/strong&gt;. A tight story has a centroid close to the unit sphere; a story that has drifted across loosely related events has a visibly shorter one. This is also why an article's similarity to its &lt;em&gt;own&lt;/em&gt; centroid reads higher than its similarity to any single fellow member. The centroid averages away each article's idiosyncrasies.&lt;/p&gt;

&lt;h3&gt;
  
  
  Making the lookup sub-linear
&lt;/h3&gt;

&lt;p&gt;Comparing each article to every live story is O(m·d) for m stories, and m reaches tens of thousands within a few days. Instead, centroids live in an approximate nearest-neighbour index, a hierarchical navigable small-world graph (HNSW) [2], which answers top-k queries in roughly logarithmic time.&lt;/p&gt;

&lt;p&gt;One practical lesson cost us real downtime. &lt;strong&gt;Ask the index a pure nearest-neighbour question, then apply your business rules in application code.&lt;/strong&gt; Mixing filters such as "updated in the last day" into the same query invites the database planner to abandon the index for a scan whenever its statistics shift. We saw per-article lookup time jump from milliseconds to seconds overnight. Pulling a generous candidate pool from the index and filtering it afterwards is both faster and predictable.&lt;/p&gt;

&lt;h3&gt;
  
  
  Time is a feature
&lt;/h3&gt;

&lt;p&gt;A story is not just a point in embedding space. It has a lifespan. The rule that decides membership treats a story differently depending on when it was last updated and how established it is. An active story accepts close matches. A quiet but established story, whose centroid is averaged over many articles and therefore stable, can still accept a continuing development. And a story eventually stops growing, so that next month's coverage of a long-running subject starts a new story instead of joining an ever-widening blob.&lt;/p&gt;

&lt;p&gt;How fast do stories actually fill up? For the 16,273 articles in stories of three or more:&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%2Fu0ml88mc27y4pxhd0jsb.png" 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%2Fu0ml88mc27y4pxhd0jsb.png" alt="Cumulative share of a story's articles by hours since it was first seen: 36% within the first hour, half by 6.6 hours, 74% by 24 hours, 90% by 48 hours" width="800" height="442"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Half of a story's articles arrive within &lt;strong&gt;6.6 hours&lt;/strong&gt; of the first (the 36% at hour zero is mostly the opening burst, including the founding article itself), and 90% within &lt;strong&gt;48 hours&lt;/strong&gt;. The curve has a fast head (the initial burst as outlets pick up the same news) and a long tail of follow-ups, analysis and regional angles. That tail is precisely the population that sits near the decision boundary in section 2: later articles drift semantically from the original report.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Second passes: merging and chaptering
&lt;/h2&gt;

&lt;p&gt;A single online pass has a known weakness: &lt;strong&gt;order dependence&lt;/strong&gt;. If the first two articles about an event arrive close together, before either is part of a story, they can found two separate stories that then grow in parallel. Their centroids converge as they fill up.&lt;/p&gt;

&lt;p&gt;The cure is a periodic merge pass. Any recently changed story is compared with other live stories, centroid to centroid. Pairs above a merge rule are folded together using the weighted-mean formula above. Only recently changed stories need checking, because a pair can only become mergeable when one side changes. That keeps the pass proportional to recent activity, not to the size of the archive.&lt;/p&gt;

&lt;p&gt;A related pass links &lt;strong&gt;chapters&lt;/strong&gt;. When a story has aged out, continuing coverage starts a new story. A new story that is close enough to an aged-out one is linked to it as its continuation, so readers can follow a long-running subject without one story growing forever.&lt;/p&gt;

&lt;p&gt;The merge pass does real work. Of the stories first seen in our two-day window, 529 were later folded into another story as duplicates.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. The shape of the output: a heavy tail
&lt;/h2&gt;

&lt;p&gt;What does the resulting set of stories look like?&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%2Ft5l20qi464i5ebgbopsv.png" 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%2Ft5l20qi464i5ebgbopsv.png" alt="Log-log plot of the share of stories with at least k articles: a near-straight line from k = 2 to several hundred, with a fitted power-law exponent of about 2.4" width="800" height="442"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;86% of stories are a single article&lt;/strong&gt; (30,181 of 34,929). They hold 58% of all articles.&lt;/li&gt;
&lt;li&gt;13.6% of stories have two or more articles, and these hold 42% of the input.&lt;/li&gt;
&lt;li&gt;Fewer than 1% reach ten articles. The largest story in the window has 409.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The tail, from two articles upward, is close to a straight line on a log–log plot of the complementary cumulative distribution. A maximum-likelihood fit for a discrete power law [3] gives:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;α̂ = 1 + n · [ Σᵢ ln( kᵢ / (k_min − ½) ) ]⁻¹  ≈  2.42   (k_min = 2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are not claiming the distribution &lt;em&gt;is&lt;/em&gt; a power law: the tail bends below the fit past a hundred articles, and a two-day window truncates the largest stories. But the qualitative point is robust and matters for system design: &lt;strong&gt;attention in the news is extremely concentrated&lt;/strong&gt;. A small number of events absorb most of the coverage, and most items are about something nobody else is covering.&lt;/p&gt;

&lt;p&gt;That shape is what makes the economics work.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Counting independent sources: syndication
&lt;/h2&gt;

&lt;p&gt;"Covered by 12 outlets" is the most natural signal of importance, and it is easy to get wrong. A wire story reprinted verbatim by many publishers is one report, not many. Several feeds of the same publisher are one outlet.&lt;/p&gt;

&lt;p&gt;We count independent sources as the number of distinct publishers, capped by the number of distinct &lt;em&gt;normalised headlines&lt;/em&gt;. Normalisation folds case, punctuation and the HTML-entity mess that real feeds produce (the same apostrophe arrives as &lt;code&gt;'&lt;/code&gt;, &lt;code&gt;&amp;amp;#39;&lt;/code&gt; and even double-encoded &lt;code&gt;&amp;amp;amp;#39;&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sources(C) = min( |publishers(C)|, |normalised headlines(C)| )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then a story with ten publishers and three distinct headlines counts as three independent reports.&lt;/p&gt;

&lt;p&gt;The effect is large. In our window, 3,252 stories had articles from at least two publishers. For &lt;strong&gt;652 of them (20%)&lt;/strong&gt;, the independent count was lower than the publisher count. &lt;strong&gt;442 (13.6%) collapsed to a single independent report&lt;/strong&gt;: one piece of agency copy, reprinted. Without this correction, a reader sees a "multi-source" badge that means nothing, and any downstream signal built on source counts, such as ranking, alerting or bias balance, inherits the error.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Where the language model comes in
&lt;/h2&gt;

&lt;p&gt;Only now does a language model enter, and only for stories that more than one article has joined. The funnel shows why that matters:&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%2F4o8g30bvs60wsaf1i27z.png" 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%2F4o8g30bvs60wsaf1i27z.png" alt="Funnel: 51,842 articles ingested, 34,929 stories formed, 4,748 stories with two or more articles, 2,810 with two or more independent outlets" width="799" height="347"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For the two-day window, the whole input is 51,842 articles. A per-article language-model design would make 51,842 calls. The stories that merit reading number 4,748 (&lt;strong&gt;9.2%&lt;/strong&gt; of the article count) and 2,810 after removing syndicated copies. Because a story is read as one unit, with its articles together, model cost scales with the number of &lt;em&gt;multi-article stories&lt;/em&gt;. Thanks to the heavy tail, that is an order of magnitude smaller than the input. (A growing story is read again as it changes, so the exact ratio depends on how often you refresh; the order of magnitude does not.)&lt;/p&gt;

&lt;p&gt;The second lever is the size of the model. The tasks we ask of it are narrow and well-specified: summarise what several articles agree on, describe where their coverage differs, and fill a fixed schema. A small, inexpensive model with &lt;strong&gt;structured output&lt;/strong&gt; (a strict JSON schema the response must match) handles them well. Structure turns a generative model into a classifier and extractor whose output can be validated like any other input.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Warning:&lt;/strong&gt; Strict schemas fail closed, and that is a feature. A schema the provider rejects fails every call, loudly. Test the real schema against the real API, because mocked model calls in unit tests will never catch it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  8. Extracting people and places
&lt;/h2&gt;

&lt;p&gt;The same pass that summarises a story also extracts &lt;strong&gt;who it is about and where it happened&lt;/strong&gt;, at almost no marginal cost: the model is already reading the articles. Two design choices did most of the work.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ask for roles, not just names.&lt;/strong&gt; Each person comes with a canonical full name, a short role (which later helps tell namesakes apart), and whether they are the &lt;em&gt;subject&lt;/em&gt; of the story or merely &lt;em&gt;mentioned&lt;/em&gt;. Each place comes with its precision (city, region or country), its country code, and whether it is where the event &lt;em&gt;happened&lt;/em&gt; or a place merely reacting. "Country X condemns strike in City Y" has one event place and one reacting place. Collapsing them is the most common error in naive extraction.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Constrain the vocabulary.&lt;/strong&gt; Places must be real cities, regions or countries. Venues, streets and districts resolve to their city. Continents and multi-country regions are excluded. A country acting as a state ("Russia said") is a place, not an organisation. Each rule removes a class of ambiguity that would otherwise surface later as a duplicate or a mis-pinned location.&lt;/p&gt;

&lt;h3&gt;
  
  
  Entity resolution
&lt;/h3&gt;

&lt;p&gt;Extraction produces strings. Turning strings into &lt;em&gt;things&lt;/em&gt; is the harder half. The pipeline has three stages:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Candidate generation.&lt;/strong&gt; Places are matched against a geographic gazetteer. People are matched against an open knowledge graph (Wikidata [4]) by label and aliases, including spelling variants.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Type-constrained filtering.&lt;/strong&gt; A candidate is judged by the &lt;em&gt;classes it belongs to&lt;/em&gt; in the knowledge graph, following the "instance of / subclass of" hierarchy, not by whether its description happens to contain the right words. A person must be a human, never a fictional character, band or TV episode that shares the name. This one change removed a whole family of false matches that description-based heuristics let through.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Abstention.&lt;/strong&gt; When candidates remain ambiguous, the entity stays unresolved rather than being guessed. A wrong link is worse than no link: it merges two people's coverage silently.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  What comes out: another heavy tail
&lt;/h3&gt;

&lt;p&gt;Across 3,508 stories first seen 3–9 November, extraction produced 6,276 distinct people and places with 10,291 story links, about 2.9 per story:&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%2Fu2qddjr2bth4i9osdkg3.png" 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%2Fu2qddjr2bth4i9osdkg3.png" alt="Rank-frequency plot of entities on log-log axes: a near-straight line from about 220 stories for the top entity down to single mentions, with a fitted Zipf exponent of about 0.67" width="800" height="442"&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;Distinct entities&lt;/th&gt;
&lt;th&gt;Story links&lt;/th&gt;
&lt;th&gt;Links per entity&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;People&lt;/td&gt;
&lt;td&gt;4,238&lt;/td&gt;
&lt;td&gt;6,166&lt;/td&gt;
&lt;td&gt;1.45&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Places&lt;/td&gt;
&lt;td&gt;2,038&lt;/td&gt;
&lt;td&gt;4,125&lt;/td&gt;
&lt;td&gt;2.02&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;81% of entities appear in exactly one story.&lt;/strong&gt; News constantly introduces new names: witnesses, local officials, athletes in a single match.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The top 1% of entities account for 16.6% of all mentions.&lt;/strong&gt; A handful of heads of state, capitals and conflict zones recur constantly.&lt;/li&gt;
&lt;li&gt;The rank–frequency curve is close to Zipf-like, f(r) ∝ r⁻ˢ, with s ≈ 0.67 over the top thousand ranks. That is flatter than the classic s ≈ 1 of word frequencies [5]. Over a week, news spreads its attention across more distinct people and places than language spreads across words.&lt;/li&gt;
&lt;li&gt;Places concentrate more than people: on average a place recurs in 2.0 stories, a person in 1.45.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The engineering consequences are practical. Resolution effort should be spent where the mass is: the head of the distribution, which recurs, is worth resolving carefully, ideally with a human-reviewed override path. The singleton tail cannot be curated by hand and must be handled by conservative automatic rules that abstain rather than guess.&lt;/p&gt;

&lt;h2&gt;
  
  
  9. Measuring quality honestly
&lt;/h2&gt;

&lt;p&gt;None of this is worth much without measurement, and clustering is notoriously hard to evaluate because there is rarely a gold standard. What has worked for us:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Replay, don't guess.&lt;/strong&gt; Changes to the similarity rules are evaluated by replaying a stored window of real articles through old and new rules and comparing the outputs: stories formed, merges made, and a hand-checked sample of the pairs that differ. Our first tuning round was validated this way before it shipped.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Event logs per story.&lt;/strong&gt; Every creation, article addition and merge is recorded. When a story looks wrong, the log shows exactly which decision produced it, and when.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Precision and recall for extraction, from hand review.&lt;/strong&gt; Reviewers mark each extracted entity as correct or wrong, and add the entities that were missed:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;precision = correct / (correct + wrong)
recall    = correct / (correct + missed)
F₁        = 2 · precision · recall / (precision + recall)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These are tracked by extraction date, so the effect of any rule change shows up from the day it went in.&lt;/p&gt;

&lt;h2&gt;
  
  
  10. What we would tell someone starting today
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Let geometry do the bulk of the work.&lt;/strong&gt; One embedding per article and an approximate nearest-neighbour index handle the full stream for almost nothing. Reserve language models for the aggregated, narrow end.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Study the nearest-neighbour distribution, not the average.&lt;/strong&gt; The hard decisions are between related events, and a single threshold will not separate them. Add time, maturity and a merge pass.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use running centroids.&lt;/strong&gt; They are exact, O(d) per update, and their length is a free cohesion signal.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Correct for syndication before you count sources.&lt;/strong&gt; In our data, one in five multi-publisher stories overstated its independent coverage.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Extract with roles and constraints, resolve with types, and abstain when unsure.&lt;/strong&gt; The output follows a heavy tail; spend human attention on the head.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep the index query pure.&lt;/strong&gt; Nearest neighbours first, business rules after.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The result is a pipeline where the expensive model never sees a lone article. It only reads stories, and only the one in seven that more than one article has joined.&lt;/p&gt;

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

&lt;ol&gt;
&lt;li&gt;J. Allan, J. Carbonell, G. Doddington, J. Yamron, Y. Yang. &lt;em&gt;Topic Detection and Tracking Pilot Study: Final Report.&lt;/em&gt; Proceedings of the DARPA Broadcast News Transcription and Understanding Workshop, 1998.&lt;/li&gt;
&lt;li&gt;Y. A. Malkov, D. A. Yashunin. &lt;em&gt;Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs.&lt;/em&gt; IEEE Transactions on Pattern Analysis and Machine Intelligence 42(4), 2020. &lt;a href="https://arxiv.org/abs/1603.09320" rel="noopener noreferrer"&gt;arXiv:1603.09320&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;A. Clauset, C. R. Shalizi, M. E. J. Newman. &lt;em&gt;Power-law distributions in empirical data.&lt;/em&gt; SIAM Review 51(4), 2009. &lt;a href="https://arxiv.org/abs/0706.1062" rel="noopener noreferrer"&gt;arXiv:0706.1062&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;D. Vrandečić, M. Krötzsch. &lt;em&gt;Wikidata: a free collaborative knowledgebase.&lt;/em&gt; Communications of the ACM 57(10), 2014.&lt;/li&gt;
&lt;li&gt;G. K. Zipf. &lt;em&gt;Human Behavior and the Principle of Least Effort.&lt;/em&gt; Addison-Wesley, 1949.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;Data: NewSumm pipeline output for stories first seen 3–4 November 2026 (clustering, sources, funnel and arrival figures) and 3–9 November 2026 (entities). Similarity figures are from a random sample of 2,500 articles in stories of four or more, and 2,498 random cross-story pairs. The own-story centroid includes the article itself, which lifts that curve slightly, most for small stories.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on the &lt;a href="https://newsumm.com/blog/clustering-the-news-with-cheap-models" rel="noopener noreferrer"&gt;NewSumm blog&lt;/a&gt;. NewSumm groups news from hundreds of outlets into single stories: &lt;a href="https://newsumm.com" rel="noopener noreferrer"&gt;newsumm.com&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>nlp</category>
      <category>postgres</category>
      <category>datascience</category>
    </item>
  </channel>
</rss>
