<script data-pm-proxy="intercept"></script><?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"><channel><title><![CDATA[Ida Builds in AI]]></title><description><![CDATA[I build and write within the tech and AI space.]]></description><link>https://howtouseai.substack.com</link><image><url>https://substackcdn.com/image/fetch/$s_!FCe_!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F44989030-8b30-4ee8-a93d-c9101f3439d8_460x460.png</url><title>Ida Builds in AI</title><link>https://howtouseai.substack.com</link></image><generator>Substack</generator><lastBuildDate>Tue, 01 Sep 2026 22:49:12 GMT</lastBuildDate><atom:link href="/__u/howtouseai.substack.com/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[Ida Silfverskiold]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[howtouseai@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[howtouseai@substack.com]]></itunes:email><itunes:name><![CDATA[Ida Silfverskiold]]></itunes:name></itunes:owner><itunes:author><![CDATA[Ida Silfverskiold]]></itunes:author><googleplay:owner><![CDATA[howtouseai@substack.com]]></googleplay:owner><googleplay:email><![CDATA[howtouseai@substack.com]]></googleplay:email><googleplay:author><![CDATA[Ida Silfverskiold]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[I Spent the Summer Testing 14 OCR Engines]]></title><description><![CDATA[To figure out where each excelled and where it failed]]></description><link>https://howtouseai.substack.com/p/i-spent-the-summer-testing-14-ocr</link><guid isPermaLink="false">https://howtouseai.substack.com/p/i-spent-the-summer-testing-14-ocr</guid><dc:creator><![CDATA[Ida Silfverskiold]]></dc:creator><pubDate>Tue, 01 Sep 2026 09:51:16 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!o1fJ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F335dad91-41dd-402e-bb97-7195e03dc7dc_1400x915.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!o1fJ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F335dad91-41dd-402e-bb97-7195e03dc7dc_1400x915.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!o1fJ!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F335dad91-41dd-402e-bb97-7195e03dc7dc_1400x915.png 424w, /__u/substackcdn.com/image/fetch/$s_!o1fJ!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F335dad91-41dd-402e-bb97-7195e03dc7dc_1400x915.png 848w, /__u/substackcdn.com/image/fetch/$s_!o1fJ!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F335dad91-41dd-402e-bb97-7195e03dc7dc_1400x915.png 1272w, /__u/substackcdn.com/image/fetch/$s_!o1fJ!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F335dad91-41dd-402e-bb97-7195e03dc7dc_1400x915.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!o1fJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F335dad91-41dd-402e-bb97-7195e03dc7dc_1400x915.png" width="1400" height="915" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/335dad91-41dd-402e-bb97-7195e03dc7dc_1400x915.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:915,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!o1fJ!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F335dad91-41dd-402e-bb97-7195e03dc7dc_1400x915.png 424w, /__u/substackcdn.com/image/fetch/$s_!o1fJ!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F335dad91-41dd-402e-bb97-7195e03dc7dc_1400x915.png 848w, /__u/substackcdn.com/image/fetch/$s_!o1fJ!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F335dad91-41dd-402e-bb97-7195e03dc7dc_1400x915.png 1272w, /__u/substackcdn.com/image/fetch/$s_!o1fJ!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F335dad91-41dd-402e-bb97-7195e03dc7dc_1400x915.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Just for fun &#8212; there is an actual graph further down | Images by author</figcaption></figure></div><p><em>This is an article dated June 2026 but has been re-published here.</em></p><p>There is a huge market for Intelligent Document Processing (IDP), and it is projected to grow to somewhere between $20 billion and $90 billion by the early 2030s, depending on which <a href="https://www.fortunebusinessinsights.com/intelligent-document-processing-market-108590">analyst</a> you ask.</p><p>Probably driven by companies paying $15&#8211;25 per invoice in manual handling costs.</p><p>Because I stay close to the tech world, I have watched a wave of small, specialized OCR vision models ship over the past year, mostly from Chinese organizations.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!vJek!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7726a22e-2153-4f08-9eee-ac83b26ac378_1400x486.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!vJek!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7726a22e-2153-4f08-9eee-ac83b26ac378_1400x486.png 424w, /__u/substackcdn.com/image/fetch/$s_!vJek!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7726a22e-2153-4f08-9eee-ac83b26ac378_1400x486.png 848w, /__u/substackcdn.com/image/fetch/$s_!vJek!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7726a22e-2153-4f08-9eee-ac83b26ac378_1400x486.png 1272w, /__u/substackcdn.com/image/fetch/$s_!vJek!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7726a22e-2153-4f08-9eee-ac83b26ac378_1400x486.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!vJek!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7726a22e-2153-4f08-9eee-ac83b26ac378_1400x486.png" width="1400" height="486" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7726a22e-2153-4f08-9eee-ac83b26ac378_1400x486.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:486,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!vJek!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7726a22e-2153-4f08-9eee-ac83b26ac378_1400x486.png 424w, /__u/substackcdn.com/image/fetch/$s_!vJek!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7726a22e-2153-4f08-9eee-ac83b26ac378_1400x486.png 848w, /__u/substackcdn.com/image/fetch/$s_!vJek!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7726a22e-2153-4f08-9eee-ac83b26ac378_1400x486.png 1272w, /__u/substackcdn.com/image/fetch/$s_!vJek!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7726a22e-2153-4f08-9eee-ac83b26ac378_1400x486.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>These are not the only new options, though. Document-parsing tools such as LlamaParse and Docling have also come onto the scene.</p><p>These new document-parsing models and pipelines now share the space with old-timers such as Tesseract and AWS Textract Structured. Depending on what they are being used for, the price tag for companies can currently range from $0 to around $65 per 1,000 processed pages.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!wCUU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd239fc74-2aa8-4463-adeb-f94647b39668_1400x563.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!wCUU!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd239fc74-2aa8-4463-adeb-f94647b39668_1400x563.png 424w, /__u/substackcdn.com/image/fetch/$s_!wCUU!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd239fc74-2aa8-4463-adeb-f94647b39668_1400x563.png 848w, /__u/substackcdn.com/image/fetch/$s_!wCUU!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd239fc74-2aa8-4463-adeb-f94647b39668_1400x563.png 1272w, /__u/substackcdn.com/image/fetch/$s_!wCUU!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd239fc74-2aa8-4463-adeb-f94647b39668_1400x563.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!wCUU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd239fc74-2aa8-4463-adeb-f94647b39668_1400x563.png" width="1400" height="563" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d239fc74-2aa8-4463-adeb-f94647b39668_1400x563.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:563,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!wCUU!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd239fc74-2aa8-4463-adeb-f94647b39668_1400x563.png 424w, /__u/substackcdn.com/image/fetch/$s_!wCUU!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd239fc74-2aa8-4463-adeb-f94647b39668_1400x563.png 848w, /__u/substackcdn.com/image/fetch/$s_!wCUU!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd239fc74-2aa8-4463-adeb-f94647b39668_1400x563.png 1272w, /__u/substackcdn.com/image/fetch/$s_!wCUU!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd239fc74-2aa8-4463-adeb-f94647b39668_1400x563.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>So, I felt it would be useful to properly test 14 of these engines, old and new, on a range of messy documents, including handwritten notes, tables, legacy financial documents, scanned invoices, receipts, charts, old newspapers, and tax forms, to see how they compete.</p><p>The idea was to test how they handled two things: text recovery and the ability to preserve useful table structure.</p><p>The <strong>main questions</strong> I wanted answered were: <strong>do you really need to pay $65 per 1,000 structured pages</strong>, or can you cut that down to a fraction? And <strong>do the new specialized OCR models outperform the general ones?</strong></p><p><em>This article is for anyone working in AI engineering, but also for business people who need to understand how to approach this space and optimize their own setup.</em></p><h2>TL;DR</h2><p>OCR is not really a search for the best engine. It is a routing and evaluation problem, and the answer depends on your documents.</p><p>Cheap, old tools can handle clean documents. General vision models are better when documents become unpredictable. Expensive structured extraction can be necessary, but there are now cheaper options that perform just as well.</p><p>Specialized models trained specifically for OCR do well enough for the price, but can fail sharply outside their distribution, which is impossible to see from benchmarks alone.</p><p>The key thing to carry with you is that using one engine for every document can force you to pay far more than necessary, while new technology does not always outperform older, proven tools.</p><p><em>Let&#8217;s cover how we got to this conclusion though.</em></p><h2>Let&#8217;s run through the mechanics first</h2><p>For anyone new to OCR, or anyone who needs a refresher, I&#8217;ll give a brief introduction first.</p><p>Then, let&#8217;s cover the test setup so you understand the engines, the types of documents I chose, and the metrics I used to decide how well each engine performed.</p><h3>Explain the OCR space to me</h3><p>OCR, or Optical Character Recognition, is how a machine turns a picture into machine-readable text. It is simple in principle and, for easier documents, mostly solved. It becomes harder when things get more human.</p><p>To give you a quick overview, older OCR systems found text on a page, sliced it into characters, and matched each one against a library of known shapes. Tesseract has done this since the 1980s.</p><p>Modern OCR, however, including newer versions of Tesseract, usually uses a neural network that looks at the whole page at once and outputs the document as text. So, if your document is a clean PDF or a high-quality scan in a standard font, OCR is mostly a solved problem.</p><p>It stops being solved the moment things get messier: photographed receipts, handwritten notes, weird graphs and charts, dense financial tables, or scanned tax forms and loan applications.</p><p>Companies need this done well for obvious reasons, as every downstream system acts on the output. The better OCR gets, the more paperwork becomes something a system can reason over instead of something a human has to read by hand.</p><p>There is also the fact that if we feed AI systems badly parsed documents, everything that comes after will be difficult to trust.</p><p>This raises the question I wanted to test: can small open-source models actually do the work that expensive APIs charge for, or should we look to general vision models to handle OCR too?</p><h3>The docs, the engines, and the metrics</h3><p>This experiment needs to cover a few things before we move on to the results: specifically what engines we used, what docs we tested with, and how we decided who won.</p><p>For the engines, I wanted a lineup that covered all the choices I talked about, this meant: old and new, open and closed, local and cloud, specialized and general.</p><p><strong>Tesseract</strong> became the classical choice. It runs locally and is very fast. Then I added two document-parsing pipelines: <strong>Docling</strong> and <strong>Marker</strong>. Docling is slower but runs on CPU, Marker is open-weight but needing a GPU to run fast, which shows up later in the price.</p><p>Then for the new wave of specialized open OCR models: <strong>GLM-OCR, PaddleOCR-VL, DeepSeek-OCR</strong>, and <strong>MinerU 2.5</strong> (a borderline case, really a pipeline with a VLM inside). I picked them off OpenDataLab&#8217;s OmniDocBench <a href="https://opendatalab.com/omnidocbench">leaderboard</a>, where they ranked first, second, fourth, and fifth.</p><p><em>Note for <strong>PaddleOCR-VL</strong>, we ran the two-part system pipeline.</em></p><p>I hosted them on Modal and served the applicable ones with vLLM, batching to speed things up. I counted the scale-up time when measuring latency later.</p><p>I also added one closed purpose-built model, <strong>Mistral OCR</strong>, which I&#8217;d heard good things about.</p><p>On the open side, I used <strong>Qwen3-VL</strong> (8B, from Alibaba), also hosted on Modal with the rest of the smaller models. <em>I should flag that I gave it a plain transcription prompt rather than the optimized serving setup it was designed for, so I may not have given it a fair shot.</em></p><p>On the closed side, for the general models, I picked <strong>Gemini Flash 3.1 Lite</strong> (currently first on the IDP Leaderboard, the western counterpart built on OmniDocBench v1.5) and <strong>Claude Sonnet 4.6</strong>, at sixth.</p><p>For the cloud document services: <strong>LlamaParse</strong> and <strong>AWS Textract</strong>, in both its text and structured forms. Structured Textract can do far more than I asked of it. I only tested its text accuracy across the board and its table extraction against eight of the other engines.</p><p>Let&#8217;s turn to the <strong>documents</strong>. I picked seventeen document types that were either easy, medium, or hard. Ninety-three files in all.</p><p>Easy was the stuff OCR mostly solved years ago: clean invoices and <a href="https://huggingface.co/datasets/rth/sroie-2019-v2">receipts</a>. Medium came largely from the OmniAI OCR Benchmark <a href="https://huggingface.co/datasets/getomni-ai/ocr-benchmark">dataset</a>: bank statements, medical notes, photographed receipts, shipping documents, tax forms.</p><p>Hard was chosen when things turned more difficult: charts, forms, handwritten notes, weirdly scanned financial tables, legal papers, newspapers, and old legacy reports.</p><blockquote><p>Some docs were really quite difficult, such as the legacy scanned docs you see below, and this was just because I was curious if some could actually do it well.</p></blockquote><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!lnNB!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe4d6dbea-5481-465a-8ef2-149dcee0e6ae_1400x1050.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!lnNB!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe4d6dbea-5481-465a-8ef2-149dcee0e6ae_1400x1050.png 424w, /__u/substackcdn.com/image/fetch/$s_!lnNB!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe4d6dbea-5481-465a-8ef2-149dcee0e6ae_1400x1050.png 848w, /__u/substackcdn.com/image/fetch/$s_!lnNB!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe4d6dbea-5481-465a-8ef2-149dcee0e6ae_1400x1050.png 1272w, /__u/substackcdn.com/image/fetch/$s_!lnNB!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe4d6dbea-5481-465a-8ef2-149dcee0e6ae_1400x1050.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!lnNB!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe4d6dbea-5481-465a-8ef2-149dcee0e6ae_1400x1050.png" width="1400" height="1050" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e4d6dbea-5481-465a-8ef2-149dcee0e6ae_1400x1050.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1050,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!lnNB!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe4d6dbea-5481-465a-8ef2-149dcee0e6ae_1400x1050.png 424w, /__u/substackcdn.com/image/fetch/$s_!lnNB!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe4d6dbea-5481-465a-8ef2-149dcee0e6ae_1400x1050.png 848w, /__u/substackcdn.com/image/fetch/$s_!lnNB!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe4d6dbea-5481-465a-8ef2-149dcee0e6ae_1400x1050.png 1272w, /__u/substackcdn.com/image/fetch/$s_!lnNB!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe4d6dbea-5481-465a-8ef2-149dcee0e6ae_1400x1050.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Messy legacy reports we ran through an LLM judge sourced from the <a href="https://huggingface.co/datasets/pixparse/idl-wds">Industry Documents library</a> under fair-use license &#8212; every engine did badly according to the judge (except Gemini Flash) maybe some bias carried through there.</figcaption></figure></div><p>Some of these images came with gold ground truth and some didn&#8217;t, and the ground truth I did have wasn&#8217;t always consistent, some files labeled correctly, some not, which is why we should briefly cover the metrics too.</p><p>Since every engine emits different markup, the usual scoring didn&#8217;t quite fit. One might pick <strong>Precision</strong> and <strong>Recall</strong> for a case like this.</p><p><em><strong>Precision</strong> looks at how many of the OCR output&#8217;s words actually match in the ground truth (GT) while <strong>Recall </strong>measures how many times each GT word was captured.</em></p><p>Precision would punish engines that emit markdown structure the GT doesn&#8217;t contain, furthermore the GT sometimes skipped labels entirely which would punish the engine unfairly. Recall would measure the words but punish the frequency.</p><p>So, I added on a third metric called <strong>Coverage</strong>.</p><p>I just wanted to measure how much of the ground truth shows up somewhere in the engine&#8217;s output. It isn&#8217;t perfect, but it tells me whether an engine caught most of what mattered, without penalizing it for gaps that were the ground truth&#8217;s fault rather than the engine&#8217;s.</p><p>For the documents with no gold ground truth at all, I fell back on an LLM judge, with Gemini 3 Pro as the base model as the judge, and anyone who&#8217;s used an LLM judge knows this is fickle business.</p><h2>What this experiment showed</h2><p>Let&#8217;s briefly run through the results, look at which engines work best where, and then cover the other oddities from this test that are worth learning from.</p><h3>Let&#8217;s first map out the results</h3><p>We mapped every document against the <strong>Coverage</strong> metric to build a scatter chart, and tracked latency on a separate chart. The thing a generalized chart can&#8217;t tell you though is that the engines failed in different ways.</p><p>The bubble graph showed that most engines fall somewhere in the middle top, with two outliers on both sides of it.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!V-Eb!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0df1ae7-077f-4c06-a225-764feab71811_1400x883.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!V-Eb!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0df1ae7-077f-4c06-a225-764feab71811_1400x883.png 424w, /__u/substackcdn.com/image/fetch/$s_!V-Eb!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0df1ae7-077f-4c06-a225-764feab71811_1400x883.png 848w, /__u/substackcdn.com/image/fetch/$s_!V-Eb!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0df1ae7-077f-4c06-a225-764feab71811_1400x883.png 1272w, /__u/substackcdn.com/image/fetch/$s_!V-Eb!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0df1ae7-077f-4c06-a225-764feab71811_1400x883.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!V-Eb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0df1ae7-077f-4c06-a225-764feab71811_1400x883.png" width="1400" height="883" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a0df1ae7-077f-4c06-a225-764feab71811_1400x883.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:883,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!V-Eb!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0df1ae7-077f-4c06-a225-764feab71811_1400x883.png 424w, /__u/substackcdn.com/image/fetch/$s_!V-Eb!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0df1ae7-077f-4c06-a225-764feab71811_1400x883.png 848w, /__u/substackcdn.com/image/fetch/$s_!V-Eb!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0df1ae7-077f-4c06-a225-764feab71811_1400x883.png 1272w, /__u/substackcdn.com/image/fetch/$s_!V-Eb!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0df1ae7-077f-4c06-a225-764feab71811_1400x883.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">All images have been created from the result of the experiment</figcaption></figure></div><p>Gemini Flash and Textract Text did very well across the board with some edge cases. Some of the open source models like GLM and Qwen3 VL did just below the APIs but what stood out is the Coverage for GLM while being fast (it&#8217;s a 0.9B model after all).</p><p>Sonnet performed the highest but also with a steeper price tag.</p><p>When we also mapped latency, some of the models turned out to be very slow, but again most wound up somewhere in the middle or to the left (if small and open source).</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!ZlW8!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc857cd4d-1301-4752-b694-b73725ad7d2b_1400x883.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!ZlW8!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc857cd4d-1301-4752-b694-b73725ad7d2b_1400x883.png 424w, /__u/substackcdn.com/image/fetch/$s_!ZlW8!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc857cd4d-1301-4752-b694-b73725ad7d2b_1400x883.png 848w, /__u/substackcdn.com/image/fetch/$s_!ZlW8!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc857cd4d-1301-4752-b694-b73725ad7d2b_1400x883.png 1272w, /__u/substackcdn.com/image/fetch/$s_!ZlW8!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc857cd4d-1301-4752-b694-b73725ad7d2b_1400x883.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!ZlW8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc857cd4d-1301-4752-b694-b73725ad7d2b_1400x883.png" width="1400" height="883" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c857cd4d-1301-4752-b694-b73725ad7d2b_1400x883.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:883,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!ZlW8!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc857cd4d-1301-4752-b694-b73725ad7d2b_1400x883.png 424w, /__u/substackcdn.com/image/fetch/$s_!ZlW8!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc857cd4d-1301-4752-b694-b73725ad7d2b_1400x883.png 848w, /__u/substackcdn.com/image/fetch/$s_!ZlW8!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc857cd4d-1301-4752-b694-b73725ad7d2b_1400x883.png 1272w, /__u/substackcdn.com/image/fetch/$s_!ZlW8!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc857cd4d-1301-4752-b694-b73725ad7d2b_1400x883.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Tesseract, Claude Sonnet 4.6, and Docling stand out. Tesseract was incredibly fast compared to all other engines. It should be your go-to for easier documents. The specialized models with 0.9B &#8212; 3B params were quite fast when optimized for the correct GPU running on Modal.</p><p>These graphs generalise across all the documents, but I did separate the results based on the type and difficulty level.</p><p>To start with the easy docs. On invoices, every engine did well, Tesseract especially. Receipts knocked everyone down a little.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!qek1!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fc7d784-3698-43c0-aa61-612f31c45b43_1400x582.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!qek1!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fc7d784-3698-43c0-aa61-612f31c45b43_1400x582.png 424w, /__u/substackcdn.com/image/fetch/$s_!qek1!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fc7d784-3698-43c0-aa61-612f31c45b43_1400x582.png 848w, /__u/substackcdn.com/image/fetch/$s_!qek1!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fc7d784-3698-43c0-aa61-612f31c45b43_1400x582.png 1272w, /__u/substackcdn.com/image/fetch/$s_!qek1!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fc7d784-3698-43c0-aa61-612f31c45b43_1400x582.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!qek1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fc7d784-3698-43c0-aa61-612f31c45b43_1400x582.png" width="1400" height="582" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4fc7d784-3698-43c0-aa61-612f31c45b43_1400x582.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:582,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!qek1!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fc7d784-3698-43c0-aa61-612f31c45b43_1400x582.png 424w, /__u/substackcdn.com/image/fetch/$s_!qek1!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fc7d784-3698-43c0-aa61-612f31c45b43_1400x582.png 848w, /__u/substackcdn.com/image/fetch/$s_!qek1!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fc7d784-3698-43c0-aa61-612f31c45b43_1400x582.png 1272w, /__u/substackcdn.com/image/fetch/$s_!qek1!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fc7d784-3698-43c0-aa61-612f31c45b43_1400x582.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The one outlier was Docling, which struggled across a lot of the categories, even the easy ones.</p><p>When I looked into the Docling failures I found things like <code>Ifjointreturn</code> instead of &#8220;joint return,&#8221; and worse, strings like <code>City,wrostffielfouaveaoreignadresalcomletacesb</code>. DeepSeek also missed key details here like invoice number and date, which is why its number sits low.</p><p>Textract was the best engine on a lot of the medium types, along with Claude Sonnet 4.6 and Mistral OCR.</p><p>On the harder types, Gemini Flash started rising, beating Textract on forms and handwritten notes, matching it elsewhere. It did remarkably well everywhere. Tesseract and Docling failed hard on handwritten, and forms were tough for them too.</p><p>For the docs with no ground truth (newspapers, legal, reports, some scanned legacy documents) we used an LLM judge. These are genuinely hard, so it&#8217;s no surprise almost everyone failed on the reports and newspapers.</p><p>Except <strong>Gemini Flash that did reasonably well</strong> everywhere. Mistral OCR also did well for newspapers. Gemini Flash won everywhere with the judge, though we used Gemini Pro as the judge so take that with a grain of salt (but I did double check myself).</p><p>Before rounding off: I also ran 8 engines against Textract Structured to see how they did on financial tables, extracting an HTML table.</p><p>I used Textract Structured&#8217;s output as the ground truth for TEDS (Tree Edit Distance Similarity) and scored Claude Sonnet 4.6, LlamaParse, Mistral OCR, Gemini Flash, Marker, MinerU, DeepSeek-OCR, and Docling against it.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!JqHX!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33a12755-6ca2-4fdc-9d9d-c4a216716e9d_1400x428.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!JqHX!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33a12755-6ca2-4fdc-9d9d-c4a216716e9d_1400x428.png 424w, /__u/substackcdn.com/image/fetch/$s_!JqHX!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33a12755-6ca2-4fdc-9d9d-c4a216716e9d_1400x428.png 848w, /__u/substackcdn.com/image/fetch/$s_!JqHX!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33a12755-6ca2-4fdc-9d9d-c4a216716e9d_1400x428.png 1272w, /__u/substackcdn.com/image/fetch/$s_!JqHX!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33a12755-6ca2-4fdc-9d9d-c4a216716e9d_1400x428.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!JqHX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33a12755-6ca2-4fdc-9d9d-c4a216716e9d_1400x428.png" width="1400" height="428" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/33a12755-6ca2-4fdc-9d9d-c4a216716e9d_1400x428.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:428,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!JqHX!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33a12755-6ca2-4fdc-9d9d-c4a216716e9d_1400x428.png 424w, /__u/substackcdn.com/image/fetch/$s_!JqHX!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33a12755-6ca2-4fdc-9d9d-c4a216716e9d_1400x428.png 848w, /__u/substackcdn.com/image/fetch/$s_!JqHX!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33a12755-6ca2-4fdc-9d9d-c4a216716e9d_1400x428.png 1272w, /__u/substackcdn.com/image/fetch/$s_!JqHX!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33a12755-6ca2-4fdc-9d9d-c4a216716e9d_1400x428.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Mistral OCR, and LlamaParse, and Marker did very well while being much cheaper. I also ran it through an LLM judge, and the winners were the same three (even before Textract Structured) though I&#8217;d want to build that test better before I fully trust it.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!GHne!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fef610fd3-d338-4ea6-8fb8-106ad340874b_1400x413.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!GHne!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fef610fd3-d338-4ea6-8fb8-106ad340874b_1400x413.png 424w, /__u/substackcdn.com/image/fetch/$s_!GHne!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fef610fd3-d338-4ea6-8fb8-106ad340874b_1400x413.png 848w, /__u/substackcdn.com/image/fetch/$s_!GHne!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fef610fd3-d338-4ea6-8fb8-106ad340874b_1400x413.png 1272w, /__u/substackcdn.com/image/fetch/$s_!GHne!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fef610fd3-d338-4ea6-8fb8-106ad340874b_1400x413.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!GHne!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fef610fd3-d338-4ea6-8fb8-106ad340874b_1400x413.png" width="1400" height="413" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ef610fd3-d338-4ea6-8fb8-106ad340874b_1400x413.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:413,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!GHne!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fef610fd3-d338-4ea6-8fb8-106ad340874b_1400x413.png 424w, /__u/substackcdn.com/image/fetch/$s_!GHne!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fef610fd3-d338-4ea6-8fb8-106ad340874b_1400x413.png 848w, /__u/substackcdn.com/image/fetch/$s_!GHne!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fef610fd3-d338-4ea6-8fb8-106ad340874b_1400x413.png 1272w, /__u/substackcdn.com/image/fetch/$s_!GHne!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fef610fd3-d338-4ea6-8fb8-106ad340874b_1400x413.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Now, let&#8217;s talk about what it costs to scale this up, and what would make sense where.</p><h3>Let&#8217;s spell it out: what does best where</h3><p>Let&#8217;s run through what it costs to scale up with these engines, and then based on these docs what you would chose where.</p><p>First, the cost of using these engines vary wildly, as you saw before. Sometimes it helps to see the cost not just for one document, but thousands up to a million.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!wZDD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e62b65e-1757-484f-a0d9-cf507615599e_2000x471.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!wZDD!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e62b65e-1757-484f-a0d9-cf507615599e_2000x471.png 424w, /__u/substackcdn.com/image/fetch/$s_!wZDD!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e62b65e-1757-484f-a0d9-cf507615599e_2000x471.png 848w, /__u/substackcdn.com/image/fetch/$s_!wZDD!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e62b65e-1757-484f-a0d9-cf507615599e_2000x471.png 1272w, /__u/substackcdn.com/image/fetch/$s_!wZDD!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e62b65e-1757-484f-a0d9-cf507615599e_2000x471.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!wZDD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e62b65e-1757-484f-a0d9-cf507615599e_2000x471.png" width="1456" height="343" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7e62b65e-1757-484f-a0d9-cf507615599e_2000x471.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:343,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!wZDD!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e62b65e-1757-484f-a0d9-cf507615599e_2000x471.png 424w, /__u/substackcdn.com/image/fetch/$s_!wZDD!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e62b65e-1757-484f-a0d9-cf507615599e_2000x471.png 848w, /__u/substackcdn.com/image/fetch/$s_!wZDD!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e62b65e-1757-484f-a0d9-cf507615599e_2000x471.png 1272w, /__u/substackcdn.com/image/fetch/$s_!wZDD!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e62b65e-1757-484f-a0d9-cf507615599e_2000x471.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>We&#8217;re self-hosting on Modal, so these costs come from actual usage there.</p><p>We&#8217;ve calculated that the GLM OCR 0.9B model does well enough on an A10G while running qwen3_vl, mineru, deepseek_ocr, marker, and paddleocr_vl on an A100.</p><p><em>You can obviously run locally, but my computer wouldn&#8217;t allow it and I didn&#8217;t want to try it and this gives us numbers to work with.</em></p><p>For the docs we have here, Gemini Flash 3.1-Lite is a clear winner but so is GLM OCR for that price and latency. Mistral OCR did well on structured tables while staying cheap. GLM OCR did very well when we look at latency too (considering we&#8217;re self hosting).</p><p>Claude Sonnet 4.6 did well too, but it&#8217;s slow and expensive comparatively.</p><p>Docling is so very slow on my laptop. I&#8217;m sure there are ways to speed it up, but it also failed in ways that make it inherently unstable (still a small test though).</p><p>Textract is a stable choice, but structured buys you almost no additional text accuracy so if you&#8217;re paying that steep markup for structured output, make sure you actually use it. <em>I&#8217;m guessing it&#8217;s a pretty good business model for them.</em></p><p>So, in general for this very small test: for clean, high-volume print, just use Tesseract. For general heterogeneous production, go Gemini Flash or GLM OCR. For a cost-floor with table structure, test Mistral OCR. For high-stakes docs, route to Sonnet or a larger model.</p><p>Since everyone did well in different ways you&#8217;ll have to contact me for specifics.</p><p>Let me just quickly talk about some things that stood out after doing this experiment.</p><h3>What this whole experiment taught me</h3><p>A handful of things surfaced from this that are worth pulling out on their own.</p><p>First, if you want to understand how a model or engine will do on your docs, the only way is to <strong>test on those docs</strong>, <strong>you can&#8217;t rely on benchmarks</strong> to tell you. This was the number one insight this showed.</p><p>OCR usefulness depends on your own document mix, layouts, languages, scans, tables, handwriting, and failure tolerance.</p><p><strong>Do not pay for structure if you don&#8217;t need it.</strong> I wonder how many are using certain APIs or models for a reason they can&#8217;t justify. Map the cost to understand what you are losing by not using the correct engine for the documents.</p><p>Lastly, the failure modes told me more than the averages.</p><p>DeepSeek OCR has chart blindness and empty outputs on some docs. While Docling has character errors, word-merging, and column misalignment all stacking together. Tesseract did fine on clean docs (as mentioned) but failed on photo/handwriting altogether outputting garbage.</p><h2>Caveats to consider</h2><p>Before we round up, let me cover how this test is ultimately imperfect by naming the issues in the GT, the metrics used, and the sample size.</p><p>I covered this in one of the section above, but the ground truth differs between documents depending on the dataset where they were found. In general tokenization artifacts can make correct OCR look worse than it is.</p><p>Most engines have different formats, some return plain text, some markdown, some HTML/rich markdown and it&#8217;s hard to generalize across all.</p><p>We are using <strong>Coverage</strong>, and then also some other metrics, but these aren&#8217;t perfect. Coverage won&#8217;t charge the engine if it outputs too much text or the structure of it is off. Though I did find that for the engines that failed, they did so at the start or mid-way through rather than at the end.</p><p>This means it&#8217;s useful for ranking but not a perfect way to score.</p><p>LLM judges are not neutral truth: I&#8217;ve covered this in the past, but they are biased and very prompt sensitive.</p><p>Then I just need to say that this test is interesting but not that big, the sample size is way too small to use this as a factual study. But, I don&#8217;t fully trust these metrics nor the judge so it was the only way for me to be able to double check the results on my own without this turning into a year long project.</p><p>So, this test is useful for direction and getting a sense of what works, but for getting a sense of your use case, you need to run it through with your specific docs.</p><p>Lastly, latency and reproducibility is unstable. Serverless cold starts make timing noisy, and API models can silently change over time.</p><p>Like always with these articles, it takes quite a bit to do an experiment like this, but I do it because I&#8217;m genuinely curious.</p><p>What it looks like though is that OCR seems to be a routing problem, and perhaps an evaluation problem. Classify your docs and run them through several engines, then try to build a decent router and validator in your pipeline to escalate failures and then log the costs.</p><p>But we can say there are now new options for harder documents that can be just as fast and cheaper than before.</p><p>&#10084;</p><p><em>All datasets used in this benchmark are publicly available and sourced from HuggingFace. Licenses include MIT, CC-BY-4.0, and fair-use frameworks (UCSF Industry Documents Library) covering research, scholarship, and education. No source documents are reproduced &#8212; datasets were used solely as evaluation inputs to measure OCR engine performance.</em></p>]]></content:encoded></item><item><title><![CDATA[Testing a Naive RAG Pipeline vs an ‘Advanced’ One]]></title><description><![CDATA[To understand which wins where and why]]></description><link>https://howtouseai.substack.com/p/testing-a-naive-rag-pipeline-vs-an</link><guid isPermaLink="false">https://howtouseai.substack.com/p/testing-a-naive-rag-pipeline-vs-an</guid><dc:creator><![CDATA[Ida Silfverskiold]]></dc:creator><pubDate>Tue, 01 Sep 2026 09:46:12 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!4pcn!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e765dad-9d12-478b-903b-fa640a336261_1400x734.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!4pcn!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e765dad-9d12-478b-903b-fa640a336261_1400x734.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!4pcn!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e765dad-9d12-478b-903b-fa640a336261_1400x734.png 424w, /__u/substackcdn.com/image/fetch/$s_!4pcn!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e765dad-9d12-478b-903b-fa640a336261_1400x734.png 848w, /__u/substackcdn.com/image/fetch/$s_!4pcn!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e765dad-9d12-478b-903b-fa640a336261_1400x734.png 1272w, /__u/substackcdn.com/image/fetch/$s_!4pcn!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e765dad-9d12-478b-903b-fa640a336261_1400x734.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!4pcn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e765dad-9d12-478b-903b-fa640a336261_1400x734.png" width="1400" height="734" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4e765dad-9d12-478b-903b-fa640a336261_1400x734.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:734,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!4pcn!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e765dad-9d12-478b-903b-fa640a336261_1400x734.png 424w, /__u/substackcdn.com/image/fetch/$s_!4pcn!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e765dad-9d12-478b-903b-fa640a336261_1400x734.png 848w, /__u/substackcdn.com/image/fetch/$s_!4pcn!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e765dad-9d12-478b-903b-fa640a336261_1400x734.png 1272w, /__u/substackcdn.com/image/fetch/$s_!4pcn!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4e765dad-9d12-478b-903b-fa640a336261_1400x734.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><em>This is an article dated February 2026 but has been re-published here.</em></p><p>Last year I wrote an article about overengineering a RAG system, adding fancy things like query optimization, detailed chunking with neighbors and keys, along with expanding the context.</p><p>The <strong>argument against</strong> this kind of work is that for some of these add-ons, you still end up <strong>paying 40&#8211;50% more in latency and cost</strong>.</p><p>So after some back and forth, I decided to test two pipelines, one with query optimization and neighbor expansion, and one without. Let&#8217;s call it a naive vs. a complex pipeline (even if both use good engineering practices).</p><p>After running a few evals on a <strong>synthetically created dataset </strong>to test both of these pipelines, I <strong>couldn&#8217;t see any differences in results</strong>. They performed exactly the same.</p><p>The issue was that the dataset was just too synthetic.</p><p><strong>So, we ran it on a more real dataset with questions grabbed from humans. </strong>This turned out to show something entirely different. The &#8216;complex&#8217; pipeline won by a large enough margin on most tests.</p><p>But although it won on the metrics we had set out to test, such as groundedness, faithfulness, answer quality, we started witnessing new issues like over-synthetication that we hadn&#8217;t predicted before.</p><p>Thus, we understood that both had different failure modes.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!JrLD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe73c7c6a-00e5-4e5f-83cd-a620639a9660_1400x524.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!JrLD!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe73c7c6a-00e5-4e5f-83cd-a620639a9660_1400x524.png 424w, /__u/substackcdn.com/image/fetch/$s_!JrLD!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe73c7c6a-00e5-4e5f-83cd-a620639a9660_1400x524.png 848w, /__u/substackcdn.com/image/fetch/$s_!JrLD!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe73c7c6a-00e5-4e5f-83cd-a620639a9660_1400x524.png 1272w, /__u/substackcdn.com/image/fetch/$s_!JrLD!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe73c7c6a-00e5-4e5f-83cd-a620639a9660_1400x524.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!JrLD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe73c7c6a-00e5-4e5f-83cd-a620639a9660_1400x524.png" width="1400" height="524" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e73c7c6a-00e5-4e5f-83cd-a620639a9660_1400x524.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:524,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!JrLD!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe73c7c6a-00e5-4e5f-83cd-a620639a9660_1400x524.png 424w, /__u/substackcdn.com/image/fetch/$s_!JrLD!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe73c7c6a-00e5-4e5f-83cd-a620639a9660_1400x524.png 848w, /__u/substackcdn.com/image/fetch/$s_!JrLD!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe73c7c6a-00e5-4e5f-83cd-a620639a9660_1400x524.png 1272w, /__u/substackcdn.com/image/fetch/$s_!JrLD!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe73c7c6a-00e5-4e5f-83cd-a620639a9660_1400x524.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This article will take you through the experiment, the results, and what we can learn from it. Great if you&#8217;re just starting to learn how to do evals or if you&#8217;re just curious about what we found here.</p><p>We&#8217;ll go through the setup, the experiment design, three different evaluation runs with different datasets, how to understand the results, and the cost/benefit tradeoff.</p><p><em>Please note that this experiment is using reference-free metrics and LLM judges, which you always have to be cautious about. You can see the entire breakdown in <a href="https://docs.google.com/spreadsheets/d/1wyq4FlG_gLEHF-QdZm_tC3gqTxfSHgXXDrCFJLcRNxA/edit?usp=sharing">this</a> excel document.</em></p><p>If you feel confused at any time, there are two articles, <a href="https://medium.com/data-science-collective/how-to-build-an-over-engineered-retrieval-system-923bd7466931">here</a> and <a href="https://medium.com/data-science-collective/running-evals-on-a-fancy-rag-pipeline-6d573f53d288">here</a>, that came before this one, though this one should stand on its own.</p><h2>The intro</h2><p>People continue to add complexity to their RAG pipelines, and there is a reason for it. The overall design is flawed, so we keep patching on fixes to make something that is more robust.</p><p>Most people have introduced hybrid search, BM25 and semantic, along with re-rankers. This has become standard practice. But there are more complex features you can add.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!WOaX!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F67b47bd0-2235-4897-b690-8a96e0eaf05e_1400x772.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!WOaX!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F67b47bd0-2235-4897-b690-8a96e0eaf05e_1400x772.png 424w, /__u/substackcdn.com/image/fetch/$s_!WOaX!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F67b47bd0-2235-4897-b690-8a96e0eaf05e_1400x772.png 848w, /__u/substackcdn.com/image/fetch/$s_!WOaX!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F67b47bd0-2235-4897-b690-8a96e0eaf05e_1400x772.png 1272w, /__u/substackcdn.com/image/fetch/$s_!WOaX!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F67b47bd0-2235-4897-b690-8a96e0eaf05e_1400x772.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!WOaX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F67b47bd0-2235-4897-b690-8a96e0eaf05e_1400x772.png" width="1400" height="772" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/67b47bd0-2235-4897-b690-8a96e0eaf05e_1400x772.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:772,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!WOaX!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F67b47bd0-2235-4897-b690-8a96e0eaf05e_1400x772.png 424w, /__u/substackcdn.com/image/fetch/$s_!WOaX!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F67b47bd0-2235-4897-b690-8a96e0eaf05e_1400x772.png 848w, /__u/substackcdn.com/image/fetch/$s_!WOaX!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F67b47bd0-2235-4897-b690-8a96e0eaf05e_1400x772.png 1272w, /__u/substackcdn.com/image/fetch/$s_!WOaX!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F67b47bd0-2235-4897-b690-8a96e0eaf05e_1400x772.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The pipeline we&#8217;re testing here introduces two new features, query optimization and neighbor expansion, and tests their efficiency.</p><p><strong>Query optimization rewrites your question</strong> (or query) into something that will more likely match information in the database. Such as &#8220;I want you to find out about Laura Palmer&#8221; might get re-written to &#8220;Laura Palmer.&#8221;</p><p>As for <strong>neighbor expansion</strong>, this refers to the ability to <strong>expand up and down in a document</strong> when a chunk has been matched. This should help when the answer is spread across multiple chunks in a section.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!rw2l!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3d99375-bc93-4bd3-bb51-253cc3a8245f_1400x742.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!rw2l!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3d99375-bc93-4bd3-bb51-253cc3a8245f_1400x742.png 424w, /__u/substackcdn.com/image/fetch/$s_!rw2l!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3d99375-bc93-4bd3-bb51-253cc3a8245f_1400x742.png 848w, /__u/substackcdn.com/image/fetch/$s_!rw2l!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3d99375-bc93-4bd3-bb51-253cc3a8245f_1400x742.png 1272w, /__u/substackcdn.com/image/fetch/$s_!rw2l!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3d99375-bc93-4bd3-bb51-253cc3a8245f_1400x742.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!rw2l!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3d99375-bc93-4bd3-bb51-253cc3a8245f_1400x742.png" width="1400" height="742" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b3d99375-bc93-4bd3-bb51-253cc3a8245f_1400x742.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:742,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!rw2l!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3d99375-bc93-4bd3-bb51-253cc3a8245f_1400x742.png 424w, /__u/substackcdn.com/image/fetch/$s_!rw2l!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3d99375-bc93-4bd3-bb51-253cc3a8245f_1400x742.png 848w, /__u/substackcdn.com/image/fetch/$s_!rw2l!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3d99375-bc93-4bd3-bb51-253cc3a8245f_1400x742.png 1272w, /__u/substackcdn.com/image/fetch/$s_!rw2l!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb3d99375-bc93-4bd3-bb51-253cc3a8245f_1400x742.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>There is a reason we add these features, and if you want to know exactly how they work you can read <a href="https://medium.com/data-science-collective/how-to-build-an-over-engineered-retrieval-system-923bd7466931">this</a> article, but both can backfire (which is what we&#8217;ll discuss here).</p><p>We&#8217;re using LLM judges and different datasets to evaluate automated metrics like faithfulness, along with A/B tests on quality, to see how the metrics move and change for each.</p><p>The introduction will walk through the setup and the experiment design.</p><h3>The setup</h3><p>Let&#8217;s first run through the setup, briefly covering detailed chunking and neighbor expansion, and what I define as complex versus naive for the purpose of this article.</p><p>The <strong>pipeline I&#8217;ve run here uses very detailed chunking methods</strong>, if you&#8217;ve read my previous article.</p><p>This means parsing the PDFs correctly, respecting document structure, using smart merging logic, intelligent boundary detection, numeric fragment handling, and document-level context (i.e. applying headings for each chunk).</p><p>I decided not to budge on this part, though this is obviously the hardest part of building a retrieval pipeline.</p><p>When processing, it also splits the sections and then references the chunk neighbors in the metadata as you saw above. This allows us to expand the content so the LLM can see where it comes from.</p><p>For this test,<strong> we use the same chunks</strong>, but we <strong>remove query optimization and context expansion for the naive pipeline</strong> to see if the fancy add-ons are actually doing any good.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!yKE7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1440fefb-4e6e-419b-8cd4-89ea7abed4b9_1400x752.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!yKE7!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1440fefb-4e6e-419b-8cd4-89ea7abed4b9_1400x752.png 424w, /__u/substackcdn.com/image/fetch/$s_!yKE7!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1440fefb-4e6e-419b-8cd4-89ea7abed4b9_1400x752.png 848w, /__u/substackcdn.com/image/fetch/$s_!yKE7!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1440fefb-4e6e-419b-8cd4-89ea7abed4b9_1400x752.png 1272w, /__u/substackcdn.com/image/fetch/$s_!yKE7!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1440fefb-4e6e-419b-8cd4-89ea7abed4b9_1400x752.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!yKE7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1440fefb-4e6e-419b-8cd4-89ea7abed4b9_1400x752.png" width="1400" height="752" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1440fefb-4e6e-419b-8cd4-89ea7abed4b9_1400x752.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:752,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!yKE7!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1440fefb-4e6e-419b-8cd4-89ea7abed4b9_1400x752.png 424w, /__u/substackcdn.com/image/fetch/$s_!yKE7!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1440fefb-4e6e-419b-8cd4-89ea7abed4b9_1400x752.png 848w, /__u/substackcdn.com/image/fetch/$s_!yKE7!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1440fefb-4e6e-419b-8cd4-89ea7abed4b9_1400x752.png 1272w, /__u/substackcdn.com/image/fetch/$s_!yKE7!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1440fefb-4e6e-419b-8cd4-89ea7abed4b9_1400x752.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This means that we only send in on average 6 chunks for the &#8220;naive&#8221; pipeline and an average of 55 chunks for the &#8220;complex&#8221; pipeline. The chunks themselves are quite small, cut off after each paragraph.</p><p>I should also mention that the use case for this was <a href="https://docs.google.com/spreadsheets/d/1uxkUBxxZ3T_UCg2XYYGlLrLjn9M4uRGwoqrl4ZsHzlE/edit?gid=1602207419#gid=1602207419">scientific RAG papers</a>. This is a semi-difficult case, and as such, for easier use cases some of these features may not apply (but we&#8217;ll get to that later too).</p><p>You can see the papers this pipeline has ingested <a href="https://docs.google.com/spreadsheets/d/1uxkUBxxZ3T_UCg2XYYGlLrLjn9M4uRGwoqrl4ZsHzlE/edit?gid=1602207419#gid=1602207419">here</a>, and read about the use case <a href="https://medium.com/data-science-collective/how-to-build-an-over-engineered-retrieval-system-923bd7466931">here</a>.</p><p>To conclude: the setup uses the same chunking, the same reranker, and the same LLM. The only difference is optimizing the queries and expanding the chunks to neighbors.</p><h3>The experiment design</h3><p>We have three different datasets that were run through several automated metrics, then through a head-to-head judge, along with examining the outputs to validate and understand the results.</p><p>We do this because each one can catch a <em>different way the system can lie to you</em>, and no single dataset or metric exposes all failure modes.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!Xwv6!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5570ab13-5580-4817-9418-e91cb9c1232b_1400x679.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!Xwv6!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5570ab13-5580-4817-9418-e91cb9c1232b_1400x679.png 424w, /__u/substackcdn.com/image/fetch/$s_!Xwv6!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5570ab13-5580-4817-9418-e91cb9c1232b_1400x679.png 848w, /__u/substackcdn.com/image/fetch/$s_!Xwv6!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5570ab13-5580-4817-9418-e91cb9c1232b_1400x679.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Xwv6!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5570ab13-5580-4817-9418-e91cb9c1232b_1400x679.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!Xwv6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5570ab13-5580-4817-9418-e91cb9c1232b_1400x679.png" width="1400" height="679" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5570ab13-5580-4817-9418-e91cb9c1232b_1400x679.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:679,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!Xwv6!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5570ab13-5580-4817-9418-e91cb9c1232b_1400x679.png 424w, /__u/substackcdn.com/image/fetch/$s_!Xwv6!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5570ab13-5580-4817-9418-e91cb9c1232b_1400x679.png 848w, /__u/substackcdn.com/image/fetch/$s_!Xwv6!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5570ab13-5580-4817-9418-e91cb9c1232b_1400x679.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Xwv6!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5570ab13-5580-4817-9418-e91cb9c1232b_1400x679.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>I started by creating a script that would generate around 256 questions from the ingested corpus. You can see this dataset <a href="https://github.com/ilsilfverskiold/Awesome-LLM-Resources-List/blob/main/guides/RAG/Custom/dataset_corpus.txt">here</a>.</p><p>This can be a good way to validate that your pipeline works, but if it&#8217;s too clean it can give you a false sense of security.</p><blockquote><p>Note that I did not specify how the questions should be generated. This means I did not ask it to generate questions that an entire section could answer, or that only a single chunk could answer.</p></blockquote><p>The <a href="https://github.com/ilsilfverskiold/Awesome-LLM-Resources-List/blob/main/guides/RAG/Custom/dataset_corpus_messy.txt">second dataset </a>was also generated from the corpus, but I intentionally asked the LLM to generate messy questions like &#8220;what are the plz three types of reward functions used in eviomni?&#8221;</p><p>The third dataset, and the most important one, was the <a href="https://github.com/ilsilfverskiold/Awesome-LLM-Resources-List/blob/main/guides/RAG/Custom/dataset_random.txt">random dataset</a>.</p><p>I asked an AI agent to research different RAG questions people had online, such as &#8220;best rag eval benchmarks and why?&#8221; and &#8220;when does using titles/abstracts beat full text retrieval.&#8221;</p><p>Remember, the pipeline had only ingested around 150 scientific papers from September/October that mentioned RAG. So we don&#8217;t know if the ingested documents even had the answers.</p><p>To run the first evals, I used automated metrics such as <strong>faithfulness</strong> (does the answer stay grounded in the context) and <strong>answer relevancy</strong> (does it answer the query) from RAGAS.</p><p>I also added a few metrics from DeepEval to look at <strong>context relevance</strong>, <strong>structure</strong>, and <strong>hallucinations</strong>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!8cMR!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86d6ee5f-fc6e-4016-802d-43df97ee74a3_1400x724.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!8cMR!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86d6ee5f-fc6e-4016-802d-43df97ee74a3_1400x724.png 424w, /__u/substackcdn.com/image/fetch/$s_!8cMR!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86d6ee5f-fc6e-4016-802d-43df97ee74a3_1400x724.png 848w, /__u/substackcdn.com/image/fetch/$s_!8cMR!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86d6ee5f-fc6e-4016-802d-43df97ee74a3_1400x724.png 1272w, /__u/substackcdn.com/image/fetch/$s_!8cMR!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86d6ee5f-fc6e-4016-802d-43df97ee74a3_1400x724.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!8cMR!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86d6ee5f-fc6e-4016-802d-43df97ee74a3_1400x724.png" width="1400" height="724" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/86d6ee5f-fc6e-4016-802d-43df97ee74a3_1400x724.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:724,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!8cMR!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86d6ee5f-fc6e-4016-802d-43df97ee74a3_1400x724.png 424w, /__u/substackcdn.com/image/fetch/$s_!8cMR!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86d6ee5f-fc6e-4016-802d-43df97ee74a3_1400x724.png 848w, /__u/substackcdn.com/image/fetch/$s_!8cMR!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86d6ee5f-fc6e-4016-802d-43df97ee74a3_1400x724.png 1272w, /__u/substackcdn.com/image/fetch/$s_!8cMR!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F86d6ee5f-fc6e-4016-802d-43df97ee74a3_1400x724.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>We ran both pipelines through the different datasets, and then through all of these metrics.</p><p><strong>Then I added another head-to-head judge to A/B test the quality of each pipeline for each dataset.</strong> This judge did not see the context, only the question, the answer, and the automated metrics.</p><p>Why not include the context in the evaluation?</p><p>Because you can&#8217;t overload these judges with too many variables. This is also why evals can be difficult to do. You need to understand the one key metric you want to measure for each one.</p><blockquote><p>I should note that this can be an unreliable way to test systems. If the hallucination score is mostly vibes, but we remove a data point because of it before sending it into the next judge that tests quality, we can end up with highly unreliable data once we start aggregating but alas.</p></blockquote><p>For the final part, we looked at semantic similarity between the answers and examined the ones with the largest differences, along with cases where one pipeline clearly won over the other.</p><p>Let&#8217;s now turn to running the experiment.</p><h2>Running the experiment</h2><p>Since we have several different datasets, we need to go through the results of each. The first two datasets proved pretty lackluster, but they did show us something, so it&#8217;s worth covering them.</p><p>The random dataset showed the most interesting results so far. This will be the main focus, and I&#8217;ll dig into the results a bit to show where it failed and where it succeeded.</p><p><em>Remember, I&#8217;m trying to reflect reality here, which is often messier than people want it to be. You&#8217;ll see all the results in this <a href="https://docs.google.com/spreadsheets/d/1wyq4FlG_gLEHF-QdZm_tC3gqTxfSHgXXDrCFJLcRNxA/edit?usp=sharing">sheet</a>.</em></p><h3>Clean questions from the corpus</h3><p>The <a href="https://github.com/ilsilfverskiold/Awesome-LLM-Resources-List/blob/main/guides/RAG/Custom/dataset_corpus.txt">clean corpus dataset</a> showed pretty identical results on all metrics. The judge seemed to prefer one over the other based on shallow preferences, but it showed us the issue with relying on synthetic datasets.</p><p>The first run was on the corpus dataset. Remember the clean questions that had been generated from the docs the pipeline had ingested.</p><p>The results of <strong>the automated metrics were eerily similar.</strong></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!d5gj!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8790b9b-5d30-487e-bad6-2aa52e3b42c7_1400x658.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!d5gj!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8790b9b-5d30-487e-bad6-2aa52e3b42c7_1400x658.png 424w, /__u/substackcdn.com/image/fetch/$s_!d5gj!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8790b9b-5d30-487e-bad6-2aa52e3b42c7_1400x658.png 848w, /__u/substackcdn.com/image/fetch/$s_!d5gj!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8790b9b-5d30-487e-bad6-2aa52e3b42c7_1400x658.png 1272w, /__u/substackcdn.com/image/fetch/$s_!d5gj!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8790b9b-5d30-487e-bad6-2aa52e3b42c7_1400x658.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!d5gj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8790b9b-5d30-487e-bad6-2aa52e3b42c7_1400x658.png" width="1400" height="658" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b8790b9b-5d30-487e-bad6-2aa52e3b42c7_1400x658.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:658,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!d5gj!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8790b9b-5d30-487e-bad6-2aa52e3b42c7_1400x658.png 424w, /__u/substackcdn.com/image/fetch/$s_!d5gj!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8790b9b-5d30-487e-bad6-2aa52e3b42c7_1400x658.png 848w, /__u/substackcdn.com/image/fetch/$s_!d5gj!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8790b9b-5d30-487e-bad6-2aa52e3b42c7_1400x658.png 1272w, /__u/substackcdn.com/image/fetch/$s_!d5gj!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8790b9b-5d30-487e-bad6-2aa52e3b42c7_1400x658.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Even context relevance was pretty much the same, ~0.95&#8211;0.97 for both. I had to double check it several times to confirm. As I&#8217;ve had great success with using context expansion, the results made me a bit uneasy.</p><p>It&#8217;s quite obvious though in hindsight, the questions are already well formatted for retrieval, and one passage may answer the question.</p><p>I did have the thought on why context relevance didn&#8217;t decrease for the expanded pipeline if one passage was good enough. This was because the extra contexts come from the same section as the seed chunks, making them semantically related and not considered &#8220;irrelevant&#8221; by RAGAS.</p><p>The A/B test for quality we ran it through had similar results.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!PHHn!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F45f732d1-1a48-4b8d-b6a5-141b40675bb2_1400x577.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!PHHn!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F45f732d1-1a48-4b8d-b6a5-141b40675bb2_1400x577.png 424w, /__u/substackcdn.com/image/fetch/$s_!PHHn!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F45f732d1-1a48-4b8d-b6a5-141b40675bb2_1400x577.png 848w, /__u/substackcdn.com/image/fetch/$s_!PHHn!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F45f732d1-1a48-4b8d-b6a5-141b40675bb2_1400x577.png 1272w, /__u/substackcdn.com/image/fetch/$s_!PHHn!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F45f732d1-1a48-4b8d-b6a5-141b40675bb2_1400x577.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!PHHn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F45f732d1-1a48-4b8d-b6a5-141b40675bb2_1400x577.png" width="1400" height="577" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/45f732d1-1a48-4b8d-b6a5-141b40675bb2_1400x577.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:577,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!PHHn!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F45f732d1-1a48-4b8d-b6a5-141b40675bb2_1400x577.png 424w, /__u/substackcdn.com/image/fetch/$s_!PHHn!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F45f732d1-1a48-4b8d-b6a5-141b40675bb2_1400x577.png 848w, /__u/substackcdn.com/image/fetch/$s_!PHHn!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F45f732d1-1a48-4b8d-b6a5-141b40675bb2_1400x577.png 1272w, /__u/substackcdn.com/image/fetch/$s_!PHHn!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F45f732d1-1a48-4b8d-b6a5-141b40675bb2_1400x577.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>For the cases <strong>where naive won, the judge liked the answer&#8217;s conciseness, clarity, and focus.</strong> It penalized the complex pipeline for more peripheral details (edge cases, extra citations) that weren&#8217;t directly asked for.</p><p><strong>When complex won, it liked the completeness/comprehensiveness of the answer over the naive one.</strong> This meant having specific numbers/metrics, step-by-step mechanisms, and &#8220;why&#8221; explanations, not just &#8220;what.&#8221;</p><p>Nevertheless, these results didn&#8217;t point to any failures. This was more a preference thing rather than about pure quality differences, both did exceptionally well.</p><p>So what did we learn from this? In a perfect world, you don&#8217;t need any fancy RAG add-ons, and using a test set from the corpus is highly unreliable.</p><h3>Messy questions from the corpus</h3><p>Next up we tested the <a href="https://github.com/ilsilfverskiold/Awesome-LLM-Resources-List/blob/main/guides/RAG/Custom/dataset_corpus_messy.txt">second dataset</a>, which showed similar results as the first one since it had been synthetically generated, but it started moving in another direction, which was interesting.</p><p>Remember that I introduced the messier questions generated from the corpus earlier. This dataset was generated the same way as the first one, but with messy phrasing (&#8220;can u explain like how plz&#8230;&#8221;).</p><p>The results from the automated metrics showed that the results were still very similar, though context relevance started to drop in the complex one while faithfulness started to rise slightly.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!wcz2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9196f9eb-6daa-4d5f-88ec-465f2b88b8d2_1400x707.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!wcz2!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9196f9eb-6daa-4d5f-88ec-465f2b88b8d2_1400x707.png 424w, /__u/substackcdn.com/image/fetch/$s_!wcz2!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9196f9eb-6daa-4d5f-88ec-465f2b88b8d2_1400x707.png 848w, /__u/substackcdn.com/image/fetch/$s_!wcz2!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9196f9eb-6daa-4d5f-88ec-465f2b88b8d2_1400x707.png 1272w, /__u/substackcdn.com/image/fetch/$s_!wcz2!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9196f9eb-6daa-4d5f-88ec-465f2b88b8d2_1400x707.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!wcz2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9196f9eb-6daa-4d5f-88ec-465f2b88b8d2_1400x707.png" width="1400" height="707" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9196f9eb-6daa-4d5f-88ec-465f2b88b8d2_1400x707.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:707,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!wcz2!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9196f9eb-6daa-4d5f-88ec-465f2b88b8d2_1400x707.png 424w, /__u/substackcdn.com/image/fetch/$s_!wcz2!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9196f9eb-6daa-4d5f-88ec-465f2b88b8d2_1400x707.png 848w, /__u/substackcdn.com/image/fetch/$s_!wcz2!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9196f9eb-6daa-4d5f-88ec-465f2b88b8d2_1400x707.png 1272w, /__u/substackcdn.com/image/fetch/$s_!wcz2!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9196f9eb-6daa-4d5f-88ec-465f2b88b8d2_1400x707.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>For the ones that failed the metrics, there were a few RAGAS false positives.</p><p>But there were also some failures for questions that had been formatted without specificity in the synthetic dataset, such as &#8220;how many posts tbh were used for dataset?&#8221; or &#8220;how many datasets did they test on?&#8221;</p><p>There were some questions that the query optimizer helped by removing noisy input. But I realized too late that the questions that had been generated were too directed at specific passages.</p><p>This meant that pushing them in as they were did well on the retrieval side. I.e., questions with specific names in them (like &#8220;how does CLAUSE compare&#8230;&#8221;) matched documents fine, and the query optimizer just made things worse.</p><p>There were times when the query optimization failed completely because of how the questions had been phrased.</p><p>Such as the question: &#8220;how does the btw pre-check phase in ac-rag work &amp; why is it important?&#8221; where direct search found the AC-RAG paper directly, as the question had been generated from there.</p><p>Running it through the A/B judge, the results favored the advanced pipeline a lot more than they had for the first corpus.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!uCW8!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b023bad-1d6a-4d1c-9091-7f9750acea15_1272x474.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!uCW8!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b023bad-1d6a-4d1c-9091-7f9750acea15_1272x474.png 424w, /__u/substackcdn.com/image/fetch/$s_!uCW8!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b023bad-1d6a-4d1c-9091-7f9750acea15_1272x474.png 848w, /__u/substackcdn.com/image/fetch/$s_!uCW8!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b023bad-1d6a-4d1c-9091-7f9750acea15_1272x474.png 1272w, /__u/substackcdn.com/image/fetch/$s_!uCW8!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b023bad-1d6a-4d1c-9091-7f9750acea15_1272x474.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!uCW8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b023bad-1d6a-4d1c-9091-7f9750acea15_1272x474.png" width="1272" height="474" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8b023bad-1d6a-4d1c-9091-7f9750acea15_1272x474.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:474,&quot;width&quot;:1272,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!uCW8!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b023bad-1d6a-4d1c-9091-7f9750acea15_1272x474.png 424w, /__u/substackcdn.com/image/fetch/$s_!uCW8!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b023bad-1d6a-4d1c-9091-7f9750acea15_1272x474.png 848w, /__u/substackcdn.com/image/fetch/$s_!uCW8!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b023bad-1d6a-4d1c-9091-7f9750acea15_1272x474.png 1272w, /__u/substackcdn.com/image/fetch/$s_!uCW8!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8b023bad-1d6a-4d1c-9091-7f9750acea15_1272x474.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The judge favored naive&#8217;s conciseness and brevity, while it favored the complex pipeline for completeness and comprehensiveness.</p><p>The reason we see the increase in wins for the complex pipeline is that the judge increasingly chose &#8220;complete but verbose&#8221; over &#8220;brief but potentially missing aspects&#8221; this time around.</p><p><strong>This is when I had the thought how useless answer quality is as a metric. </strong>These <strong>LLM judges run on vibes sometimes.</strong></p><p>In this run, I didn&#8217;t think the answers were different enough to warrant the difference in results. So remember, using a synthetic dataset like this can give you some intel, but it can be quite unreliable.</p><h3>Random questions dataset</h3><p>Lastly, we&#8217;ll go through the results from the <a href="https://github.com/ilsilfverskiold/Awesome-LLM-Resources-List/blob/main/guides/RAG/Custom/dataset_random.txt">random dataset</a>, which showed a lot more interesting results. Metrics started to move with a higher margin here, which gave us something to dig into.</p><p>Up to this point I had nothing to show for this, but this last dataset finally gave me something interesting to dig into.</p><p>See the results from the random dataset below.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!YuDT!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F057232dd-a0ca-43bd-a380-9a358226952c_1400x792.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!YuDT!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F057232dd-a0ca-43bd-a380-9a358226952c_1400x792.png 424w, /__u/substackcdn.com/image/fetch/$s_!YuDT!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F057232dd-a0ca-43bd-a380-9a358226952c_1400x792.png 848w, /__u/substackcdn.com/image/fetch/$s_!YuDT!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F057232dd-a0ca-43bd-a380-9a358226952c_1400x792.png 1272w, /__u/substackcdn.com/image/fetch/$s_!YuDT!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F057232dd-a0ca-43bd-a380-9a358226952c_1400x792.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!YuDT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F057232dd-a0ca-43bd-a380-9a358226952c_1400x792.png" width="1400" height="792" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/057232dd-a0ca-43bd-a380-9a358226952c_1400x792.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:792,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!YuDT!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F057232dd-a0ca-43bd-a380-9a358226952c_1400x792.png 424w, /__u/substackcdn.com/image/fetch/$s_!YuDT!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F057232dd-a0ca-43bd-a380-9a358226952c_1400x792.png 848w, /__u/substackcdn.com/image/fetch/$s_!YuDT!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F057232dd-a0ca-43bd-a380-9a358226952c_1400x792.png 1272w, /__u/substackcdn.com/image/fetch/$s_!YuDT!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F057232dd-a0ca-43bd-a380-9a358226952c_1400x792.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>On random questions, we actually saw a drop in faithfulness and answer relevancy for the naive baseline.</p><p>Context relevance was still higher for naive, along with structure, but this we had already established for the complex pipeline in the previous article.</p><p><strong>Noise will inevitably happen for the complex one, as we&#8217;re talking about 10x more chunks.</strong> Citation structure may be harder for the model when the context increases (or the judge has trouble judging the full context).</p><p>The A/B judge, though, gave it a very high score compared to the other datasets.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!erI0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F158160e5-e93f-4878-873b-d23e23346c88_1400x485.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!erI0!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F158160e5-e93f-4878-873b-d23e23346c88_1400x485.png 424w, /__u/substackcdn.com/image/fetch/$s_!erI0!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F158160e5-e93f-4878-873b-d23e23346c88_1400x485.png 848w, /__u/substackcdn.com/image/fetch/$s_!erI0!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F158160e5-e93f-4878-873b-d23e23346c88_1400x485.png 1272w, /__u/substackcdn.com/image/fetch/$s_!erI0!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F158160e5-e93f-4878-873b-d23e23346c88_1400x485.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!erI0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F158160e5-e93f-4878-873b-d23e23346c88_1400x485.png" width="1400" height="485" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/158160e5-e93f-4878-873b-d23e23346c88_1400x485.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:485,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!erI0!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F158160e5-e93f-4878-873b-d23e23346c88_1400x485.png 424w, /__u/substackcdn.com/image/fetch/$s_!erI0!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F158160e5-e93f-4878-873b-d23e23346c88_1400x485.png 848w, /__u/substackcdn.com/image/fetch/$s_!erI0!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F158160e5-e93f-4878-873b-d23e23346c88_1400x485.png 1272w, /__u/substackcdn.com/image/fetch/$s_!erI0!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F158160e5-e93f-4878-873b-d23e23346c88_1400x485.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>I ran it twice to check, and each time it favored the complex one over the naive one by a huge margin.</p><p><strong>Why the change? </strong>This time there were a lot of questions that one passage couldn&#8217;t answer on its own.</p><p>Specifically, the complex pipeline did well on tradeoff and comparison questions. The judge reasoned &#8220;more complete/comprehensive&#8221; compared to the naive pipeline.</p><p>An example was the question &#8220;what are pros and cons of hybrid vs knowledge-graph RAG for vague queries?&#8221; Naive had many unsupported claims (missing GraphRAG, HybridRAG, EM/F1 metrics).</p><p>At this point, I needed to understand why it won and why naive lost. This would give me intel on where the fancy features were actually helping.</p><h3>Looking into the results</h3><p>Now, without digging into the results, you can&#8217;t fully know why something is winning. Since the random dataset showed the most interesting results, this is where I decided to put my focus.</p><p>First, the judge has real issues evaluating the fuller context. This is why I could never create a judge to evaluate each context against the other. It would prefer naive because it&#8217;s cognitively easier to judge. This is what made this so hard.</p><p>Nevertheless, <strong>we can pinpoint some of the real failures.</strong></p><p>Even though the hallucination metric showed decent results, when digging into it, <strong>we could see that the naive pipeline fabricated information more often.</strong></p><p>We could locate this by looking at the low faithfulness scores.</p><p>To give you an example, for the question &#8220;how do I test prompt injection risks if the bad text is inside retrieved PDFs?&#8221; the naive pipeline filled in gaps in the context to provide the answer.</p><pre><code>Question: How do I test prompt injection risks if the bad text is inside retrieved PDFs?
Naive Response: Lists standard prompt-injection testing steps (PoisonedRAG, adaptive instructions, multihop poisoning) but synthesizes a generic evaluation recipe that is not fully supported by the specific retrieved sections and implicitly fills gaps with prior knowledge.
Complex Response: Derives testing steps directly from the retrieved experiment sections and threat models, including multihop-triggered attacks, single-text generation bias measurement, adaptive prompt attacks, and success-rate reporting, staying within what the cited papers actually describe.
Faithfulness: Naive: 0.0 | Complex: 0.83
What Happened: Unlike the naive answer, it is not inventing attacks, metrics, or techniques out of thin air. PoisonedRAG, trigger-based attacks, Hotflip-style perturbations, multihop attacks, ASR, DACC/FPR/FNR, PC1&#8211;PC3 all appear in the provided documents. However, the complex pipeline is subtly overstepping and has a case of scope inflation.</code></pre><p>The expanded content added the missing evaluation metrics, which bumped up the faithfulness score by 87%.</p><p><strong>Nevertheless, the complex pipeline was subtly overstepping and had a case of scope inflation</strong>. This could be an issue with the LLM generator, where we need to tune it to make sure that each claim is explicitly tied to a paper and to mark cross-paper synthesis as such.</p><p>For the question &#8220;how do I benchmark prompts that force the model to list contradictions explicitly?&#8221; naive again has very few metrics and thus invents metrics, reverses findings, and collapses task boundaries.</p><pre><code>Question: How do I benchmark prompts that force the model to list contradictions explicitly?
Naive Response: Mentions MAGIC by name and vaguely gestures at &#8220;conflicts&#8221; and &#8220;benchmarking,&#8221; but lacks concrete mechanics. No clear description of conflict generation, no separation of detection vs localization, no actual evaluation protocol. It fills gaps by inventing generic-sounding steps that are not grounded in the provided contexts.
Complex Response: Explicitly aligns with the MAGIC paper&#8217;s methodology. Describes KG-based conflict generation, single-hop vs multi-hop and 1 vs N conflicts, subgraph-level few-shot prompting, stepwise prompting (detect then localize), and the actual ID/LOC metrics used across multiple runs. Also correctly incorporates PC1&#8211;PC3 as auxiliary prompt components and explains their role, consistent with the cited sections.
Faithfulness: Naive: 0.35 | Complex: 0.73
What Happened: The complex pipeline has far more surface area, but most of it is anchored to actual sections of the MAGIC paper and related prompt-component work. In short: the naive answer hallucinates by necessity due to missing context, while the complex answer is verbose but materially supported. It over-synthesizes and over-prescribes, but mostly stays within the factual envelope. The higher faithfulness score is doing its job, even if it offends human patience.</code></pre><p>For complex, though, it over-synthesizes and over-prescribes, but stays within the factual information.</p><p>This pattern shows up in multiple examples. The naive pipeline lacks enough information for some of these questions, so it falls back to prior knowledge and pattern completion. Whereas the complex pipeline over-synthesizes under false coherence.</p><p><strong>Essentially, naive fails by making things up, and complex fails by saying true things too broadly.</strong></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!m6ud!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff643783d-7495-46ca-9918-96a23649c1ba_1350x598.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!m6ud!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff643783d-7495-46ca-9918-96a23649c1ba_1350x598.png 424w, /__u/substackcdn.com/image/fetch/$s_!m6ud!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff643783d-7495-46ca-9918-96a23649c1ba_1350x598.png 848w, /__u/substackcdn.com/image/fetch/$s_!m6ud!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff643783d-7495-46ca-9918-96a23649c1ba_1350x598.png 1272w, /__u/substackcdn.com/image/fetch/$s_!m6ud!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff643783d-7495-46ca-9918-96a23649c1ba_1350x598.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!m6ud!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff643783d-7495-46ca-9918-96a23649c1ba_1350x598.png" width="1350" height="598" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f643783d-7495-46ca-9918-96a23649c1ba_1350x598.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:598,&quot;width&quot;:1350,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!m6ud!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff643783d-7495-46ca-9918-96a23649c1ba_1350x598.png 424w, /__u/substackcdn.com/image/fetch/$s_!m6ud!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff643783d-7495-46ca-9918-96a23649c1ba_1350x598.png 848w, /__u/substackcdn.com/image/fetch/$s_!m6ud!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff643783d-7495-46ca-9918-96a23649c1ba_1350x598.png 1272w, /__u/substackcdn.com/image/fetch/$s_!m6ud!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff643783d-7495-46ca-9918-96a23649c1ba_1350x598.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This test was more about figuring out if these fancy features help, but it did point to us needing to work on claim scoping: forcing the model to say &#8220;Paper A shows X; Paper B shows Y,&#8221; and so on.</p><p><em>You can dig into a few of these questions in the sheet <a href="https://docs.google.com/spreadsheets/d/1wyq4FlG_gLEHF-QdZm_tC3gqTxfSHgXXDrCFJLcRNxA/edit?gid=917274535#gid=917274535">here.</a></em></p><p>Before we move on to the cost/latency analysis, we can try to isolate the query optimizer as well.</p><h3>How much did the query optimizer help?</h3><p>Since I did not test each part of the pipeline for each run, we had to look at different things to estimate whether the query optimizer was helping or hurting.</p><p>First, we looked at the seed chunk overlap for the complex vs naive pipeline, which showed 8.3% semantic overlap in the random pipeline, versus more than 50% overlap for the corpus pipeline.</p><p>We already know that the full pipeline won on the random dataset, and now we could also see that it surfaced different documents because of the query optimizer.</p><p><strong>Most documents were different, so I couldn&#8217;t isolate whether the quality degraded when there was little overlap.</strong></p><p>We also asked a judge to estimate the quality of the optimized queries compared to the original ones, in terms of preserving intent and being diverse enough, and<strong> it won with an 8% margin.</strong></p><p>A question that it excelled on was &#8220;why is everyone saying RAG doesn&#8217;t scale? how are people fixing that?&#8221;</p><pre><code>Orginal: why is everyone saying RAG doesn&#8217;t scale? how are people fixing that?
Optimized (1): RAG scalability challenges (hybrid)
Optimized (2): Solutions for RAG scalability (hybrid)</code></pre><p>Whereas a question that naive did well on its own was &#8220;what retrieval settings help reduce needle-in-a-haystack,&#8221; and other questions that were very well formatted from the start.</p><p><strong>We could reasonably deduce, though, that multi-questions and messier questions did better with the optimizer, as long as they were not domain specific.</strong></p><p><strong>The optimizer was overkill for well formatted questions.</strong></p><p>It also did badly when the question would already be understood by the underlying documents, in cases where someone asks something domain specific that the query optimizer wouldn&#8217;t understand.</p><p>You can look through a few examples in the<a href="https://docs.google.com/spreadsheets/d/1wyq4FlG_gLEHF-QdZm_tC3gqTxfSHgXXDrCFJLcRNxA/edit?gid=917274535#gid=917274535"> sheet.</a></p><p><strong>This teaches us how important it is to make sure that the optimizer is tuned well to the questions that users will ask. </strong>If your users keep asking with domain specific jargon that the optimizer is ignoring or filtering out, it won&#8217;t perform well.</p><p>We can see here that it is rescuing some questions and failing others at the same time, so it would need work for this use case.</p><h2>Let&#8217;s discuss it</h2><p>I&#8217;ve now overloaded you with a lot of data, so now it&#8217;s time to go through the cost/latency tradeoff, discuss what we can and cannot conclude, and the limitations of this experiment.</p><h3>The cost/latency tradeoff</h3><p>When looking at the cost and latency tradeoffs, the goal here is not to argue whether the results are &#8220;worth it&#8221;, but to put concrete numbers on what these features cost and their benefits.</p><p>I have collected the estimates here in the same <a href="https://docs.google.com/spreadsheets/d/1wyq4FlG_gLEHF-QdZm_tC3gqTxfSHgXXDrCFJLcRNxA/edit?gid=382108005#gid=382108005">sheet.</a></p><p>The cost of running this pipeline is very slim. We&#8217;re talking $0.00396 per run, and this does not include caching. <strong>Removing the query optimizer and neighbor expansion decreases costs by 41%.</strong></p><p>It&#8217;s not more than that because token inputs, the thing that increases with added context, are quite cheap.</p><p><strong>What actually costs money in this pipeline is the re-ranker from Cohere, </strong>which both the naive and the full pipeline use.</p><p>For the naive pipeline, <strong>the re-ranker accounts for 70% of the entire cost. </strong>So it&#8217;s worth looking at every part of the pipeline to figure out where you might implement smaller models to cut costs.</p><p>Nevertheless, at around 100k questions, you would be paying $400.00 for the full pipeline and $280.00 for the naive one.</p><p>There is also the case for latency.</p><p>We measured a +49% increase in latency with the complex pipeline, which amounts to about 6 seconds, mostly driven by the query optimizer using GPT-5-mini. It&#8217;s possible to use a faster and smaller model here.</p><p>For neighbor expansion, we measured the average increase to be 2&#8211;3 seconds longer. Do note that this doesn&#8217;t scale linearly.</p><p><strong>4.4x more input tokens only added 24% more time.</strong></p><p><em>Remember, you can see the entire breakdown in the sheet <a href="https://docs.google.com/spreadsheets/d/1wyq4FlG_gLEHF-QdZm_tC3gqTxfSHgXXDrCFJLcRNxA/edit?usp=sharing">here.</a></em></p><p>What this shows is that the cost difference is real but not extreme, while the latency difference is much more noticeable. Most of the money is still spent on re-ranking, not on adding context.</p><h3>What we can conclude</h3><p>Let&#8217;s focus on what worked, what failed, and why. We see that neighbor expansion may pull it&#8217;s weight when questions are diffuse, but each pipeline has it&#8217;s own failure modes.</p><p>The clearest finding from this experiment is that neighbor expansion earns its keep when retrieval gets hard and one chunk can&#8217;t answer the question.</p><p>We did a test in the previous article that looked at how much of the answer was generated from the expanded chunks, and on clean corpus questions, only 22% of the answer content came from expanded neighbors.</p><p>We also saw that the A/B results here in this article showed a tie.</p><p>On messy questions, this rose to 30% with a 10-point margin for the A/B test. On random questions, it hit 41% (used from the context) with a 44-point margin for the A/B test.</p><p>What&#8217;s happening underneath is a difference in failure modes. When naive fails, it fails by omission. The LLM doesn&#8217;t have enough context, so it either gives an incomplete answer or fabricates information to fill the gaps.</p><p>We saw this clearly in the prompt injection example, where naive scored 0.0 on faithfulness because it invented an evaluation recipe that wasn&#8217;t in the documents.</p><p>When complex fails, it fails by inflation. It has so much context that the LLM over-synthesizes and makes claims broader than any single source supports. But at least those claims are grounded in something real.</p><p>The faithfulness scores reflect this asymmetry. Naive bottoms out at 0.0 or 0.35, while complex&#8217;s worst cases still land around 0.73.</p><p>The query optimizer is harder to call. It helped on 38% of questions, hurt on 27%, and made no difference on 35%. The wins were dramatic when they happened, rescuing questions like &#8220;why is everyone saying RAG doesn&#8217;t scale?&#8221; where direct search returned nothing.</p><p>But the losses were equally dramatic when the user&#8217;s phrasing already matched the corpus vocabulary and the optimizer introduced drift.</p><p>This probably suggests you&#8217;d want to tune the optimizer carefully to your users, or find a way to detect when reformulation is likely to help versus hurt.</p><p>On cost and latency, the numbers weren&#8217;t where I expected. Adding 10x more chunks only increased generation time by 24% because reading tokens is parallelized while writing them is sequential.</p><p>The real cost driver is the reranker, at 70% of the naive pipeline&#8217;s total.</p><p>The query optimizer contributes the most latency, at nearly 3 seconds per question. If you&#8217;re optimizing for speed, that&#8217;s where to look first, along with the reranker.</p><p>So more context doesn&#8217;t necessarily mean chaos, but it does mean you need to control the LLM to a larger degree. When the question doesn&#8217;t need the complexity, the naive pipeline will rule, but once questions become diffuse, the more complex pipeline starts to pull its weight.</p><h2>Let&#8217;s talk limitations</h2><p>I have to cover the main limitations of the experiment and what we should be careful about when interpreting the results.</p><p><strong>The obvious one is that LLM judges run on vibes.</strong></p><p>The metrics moved in the right direction across datasets, but I wouldn&#8217;t trust the absolute numbers enough to set production thresholds on them.</p><p>The messy corpus showed a 10-point margin for complex, but honestly the answers weren&#8217;t different enough to warrant that gap. It it could be noise.</p><p><strong>I also didn&#8217;t isolate what happens when the docs genuinely can&#8217;t answer the question.</strong></p><p>The random dataset included questions where we didn&#8217;t know if the papers had relevant content, but I treated all 66 the same.</p><p>I did though hunt through the examples, but it&#8217;s still possible some of the complex pipeline&#8217;s wins came from being better at admitting ignorance rather than better at finding information.</p><p><strong>Finally, I tested two features together, </strong>query optimization and neighbor expansion, without fully isolating each one&#8217;s contribution. The seed overlap analysis gave us some signal on the optimizer, but a cleaner experiment would test them independently.</p><p>For now, we know the combination helps on hard questions and that the cost is 41% more per query. Whether that tradeoff makes sense depends entirely on what your users are actually asking.</p><h3>Notes</h3><p>I think we can conclude from this article that doing evals is hard, it&#8217;s even harder to put an experiment like this on paper.</p><p>I wish I could give you a clean answer, but it&#8217;s complicated.</p><p>I would personally say though that fabrication is worse than being overly verbose. But still if your corpus is incredibly clean and each answer usually points to a specific chunk, the neighbor expansion is overkill.</p><p>This then just tells you that these fancy features are insurance of a flawed system.</p><p><em>Remember you can see the full breakdown and numbers <a href="https://docs.google.com/spreadsheets/d/1wyq4FlG_gLEHF-QdZm_tC3gqTxfSHgXXDrCFJLcRNxA/edit?usp=sharing">here.</a></em></p><p>&#10084;</p>]]></content:encoded></item><item><title><![CDATA[Running Evals on a Real RAG Pipeline]]></title><description><![CDATA[To see how metrics behave across datasets and models]]></description><link>https://howtouseai.substack.com/p/running-evals-on-a-real-rag-pipeline</link><guid isPermaLink="false">https://howtouseai.substack.com/p/running-evals-on-a-real-rag-pipeline</guid><dc:creator><![CDATA[Ida Silfverskiold]]></dc:creator><pubDate>Tue, 01 Sep 2026 09:24:38 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!d5Io!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5376d8c-8379-4c7e-87ff-02efe7e7b9a9_1400x543.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!d5Io!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5376d8c-8379-4c7e-87ff-02efe7e7b9a9_1400x543.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!d5Io!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5376d8c-8379-4c7e-87ff-02efe7e7b9a9_1400x543.png 424w, /__u/substackcdn.com/image/fetch/$s_!d5Io!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5376d8c-8379-4c7e-87ff-02efe7e7b9a9_1400x543.png 848w, /__u/substackcdn.com/image/fetch/$s_!d5Io!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5376d8c-8379-4c7e-87ff-02efe7e7b9a9_1400x543.png 1272w, /__u/substackcdn.com/image/fetch/$s_!d5Io!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5376d8c-8379-4c7e-87ff-02efe7e7b9a9_1400x543.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!d5Io!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5376d8c-8379-4c7e-87ff-02efe7e7b9a9_1400x543.png" width="1400" height="543" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e5376d8c-8379-4c7e-87ff-02efe7e7b9a9_1400x543.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:543,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!d5Io!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5376d8c-8379-4c7e-87ff-02efe7e7b9a9_1400x543.png 424w, /__u/substackcdn.com/image/fetch/$s_!d5Io!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5376d8c-8379-4c7e-87ff-02efe7e7b9a9_1400x543.png 848w, /__u/substackcdn.com/image/fetch/$s_!d5Io!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5376d8c-8379-4c7e-87ff-02efe7e7b9a9_1400x543.png 1272w, /__u/substackcdn.com/image/fetch/$s_!d5Io!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5376d8c-8379-4c7e-87ff-02efe7e7b9a9_1400x543.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">The RAG pipeline to the RAG evals | All images by the author</figcaption></figure></div><p><em>This is an article dated January 2026 but has been re-published here.</em></p><p>This is a follow-up to <strong>Building an Overengineered Retrieval System</strong>. That one was about building the entire system. This one is about doing the evals for it.</p><p>In the previous article, I went through different parts of a RAG pipeline: chunking the data properly, query optimization, retrieval (semantic, BM25, or hybrid search), re-ranking, expanding chunks to neighbors, building the context, and then generation with an LLM.</p><p>One of the questions I got was: <strong>does expanding chunks to neighbors </strong>actually improve answers, or does it <strong>just add noise and make it harder for the model to stay grounded?</strong></p><p>So that&#8217;s what we&#8217;ll test here. We&#8217;ll run some basic evaluations and look at metrics like <strong>faithfulness</strong>, <strong>answer relevancy</strong>, <strong>context relevance</strong>, and <strong>hallucination rate</strong>, and compare results across different models and datasets.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!YEXu!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff91ed6e9-f118-485f-ae86-2073b8b795d7_1400x662.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!YEXu!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff91ed6e9-f118-485f-ae86-2073b8b795d7_1400x662.png 424w, /__u/substackcdn.com/image/fetch/$s_!YEXu!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff91ed6e9-f118-485f-ae86-2073b8b795d7_1400x662.png 848w, /__u/substackcdn.com/image/fetch/$s_!YEXu!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff91ed6e9-f118-485f-ae86-2073b8b795d7_1400x662.png 1272w, /__u/substackcdn.com/image/fetch/$s_!YEXu!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff91ed6e9-f118-485f-ae86-2073b8b795d7_1400x662.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!YEXu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff91ed6e9-f118-485f-ae86-2073b8b795d7_1400x662.png" width="1400" height="662" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f91ed6e9-f118-485f-ae86-2073b8b795d7_1400x662.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:662,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!YEXu!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff91ed6e9-f118-485f-ae86-2073b8b795d7_1400x662.png 424w, /__u/substackcdn.com/image/fetch/$s_!YEXu!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff91ed6e9-f118-485f-ae86-2073b8b795d7_1400x662.png 848w, /__u/substackcdn.com/image/fetch/$s_!YEXu!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff91ed6e9-f118-485f-ae86-2073b8b795d7_1400x662.png 1272w, /__u/substackcdn.com/image/fetch/$s_!YEXu!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff91ed6e9-f118-485f-ae86-2073b8b795d7_1400x662.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Some of the LLM-as-a-judge metrics we&#8217;ll run here</p><p>I&#8217;ve collected most of the results <a href="https://docs.google.com/spreadsheets/d/1x0Y5m3aBs4jmo3PeWb0r9w43kZUtuajV9NCQS2m2OuA/edit?usp=sharing">here</a> and <a href="https://docs.google.com/spreadsheets/d/1eLi5KYiNbxvlyetjz71IhOcvo7gPfHtN-eUuXMutw9k/edit?usp=sharing">here</a> already, but we&#8217;ll go through them too.</p><p>I usually include an introductory section before getting into the results. If evals aren&#8217;t new to you, skip straight to the results.</p><h3>Why we perform evals</h3><p>Evals are about pressure-testing the system on a larger (and more targeted) corpus than your favorite ten questions, and making sure that changes you push don&#8217;t change the quality of the system.</p><p><strong>Changes in data, prompts, or models can affect performance</strong> without you noticing it.</p><p>You may also need to show your team the general performance of the system you&#8217;ve built before being allowed to test it on real users.</p><p>But before you do this, you need to <strong>decide what to test. </strong>What does a successful system look like to you?</p><p>If you care about multi-hop, you need questions that actually require multi-hop. If you care about Q&amp;A quality and proper citations, you need to test for that. Otherwise, you end up evaluating the wrong thing.</p><p>This is a bit like <strong>investigative work</strong>: you test something, try to understand the results, and then build better tests.</p><p>To do this well, you should ideally aim to build a golden set, often from user logs. This isn&#8217;t always possible, so in situations like this, we build synthetic datasets. This may not be the best approach, since it&#8217;s clearly biased but you may need to start somewhere.</p><p>For this article, <strong>I&#8217;ve created three different datasets so we can discuss it.</strong></p><p>The first <a href="https://github.com/ilsilfverskiold/Awesome-LLM-Resources-List/blob/main/guides/RAG/Custom/dataset_corpus.txt">one</a> is created from the ingested corpus, the second <a href="https://github.com/ilsilfverskiold/Awesome-LLM-Resources-List/blob/main/guides/RAG/Custom/dataset_corpus_messy.txt">one</a> uses messy user questions from the corpus, and third <a href="https://github.com/ilsilfverskiold/Awesome-LLM-Resources-List/blob/main/guides/RAG/Custom/dataset_random.txt">one</a> has random questions on RAG that haven&#8217;t been generated from the corpus at all.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!f6_Z!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdada8b16-44e1-480b-a429-7ea1520576e5_1400x716.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!f6_Z!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdada8b16-44e1-480b-a429-7ea1520576e5_1400x716.png 424w, /__u/substackcdn.com/image/fetch/$s_!f6_Z!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdada8b16-44e1-480b-a429-7ea1520576e5_1400x716.png 848w, /__u/substackcdn.com/image/fetch/$s_!f6_Z!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdada8b16-44e1-480b-a429-7ea1520576e5_1400x716.png 1272w, /__u/substackcdn.com/image/fetch/$s_!f6_Z!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdada8b16-44e1-480b-a429-7ea1520576e5_1400x716.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!f6_Z!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdada8b16-44e1-480b-a429-7ea1520576e5_1400x716.png" width="1400" height="716" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/dada8b16-44e1-480b-a429-7ea1520576e5_1400x716.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:716,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!f6_Z!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdada8b16-44e1-480b-a429-7ea1520576e5_1400x716.png 424w, /__u/substackcdn.com/image/fetch/$s_!f6_Z!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdada8b16-44e1-480b-a429-7ea1520576e5_1400x716.png 848w, /__u/substackcdn.com/image/fetch/$s_!f6_Z!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdada8b16-44e1-480b-a429-7ea1520576e5_1400x716.png 1272w, /__u/substackcdn.com/image/fetch/$s_!f6_Z!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdada8b16-44e1-480b-a429-7ea1520576e5_1400x716.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>You&#8217;ll be able to see how these datasets give us different results on the metrics, but that they all mean different things.</p><h3>What to think about</h3><p>I&#8217;m not going to go through everything there is to think about here, because doing evals well is pretty difficult.</p><p>There are a few things you need to keep in mind.</p><p>LLM judges are biased, cherry-picking questions is a problem, gold answers are best if you have them, and structuring a larger dataset can help you understand where and how the system is failing.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!JeA1!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdcbf9c18-8473-48ca-9fbc-80ae338a159c_1400x627.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!JeA1!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdcbf9c18-8473-48ca-9fbc-80ae338a159c_1400x627.png 424w, /__u/substackcdn.com/image/fetch/$s_!JeA1!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdcbf9c18-8473-48ca-9fbc-80ae338a159c_1400x627.png 848w, /__u/substackcdn.com/image/fetch/$s_!JeA1!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdcbf9c18-8473-48ca-9fbc-80ae338a159c_1400x627.png 1272w, /__u/substackcdn.com/image/fetch/$s_!JeA1!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdcbf9c18-8473-48ca-9fbc-80ae338a159c_1400x627.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!JeA1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdcbf9c18-8473-48ca-9fbc-80ae338a159c_1400x627.png" width="1400" height="627" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/dcbf9c18-8473-48ca-9fbc-80ae338a159c_1400x627.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:627,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!JeA1!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdcbf9c18-8473-48ca-9fbc-80ae338a159c_1400x627.png 424w, /__u/substackcdn.com/image/fetch/$s_!JeA1!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdcbf9c18-8473-48ca-9fbc-80ae338a159c_1400x627.png 848w, /__u/substackcdn.com/image/fetch/$s_!JeA1!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdcbf9c18-8473-48ca-9fbc-80ae338a159c_1400x627.png 1272w, /__u/substackcdn.com/image/fetch/$s_!JeA1!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdcbf9c18-8473-48ca-9fbc-80ae338a159c_1400x627.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>If you&#8217;ve read the <a href="https://medium.com/data-science-collective/agentic-ai-working-with-evals-b0dcedbe97f8">eval metrics article</a>, you&#8217;ve already seen the idea of LLM-as-a-judge. It can be useful, but it&#8217;s not inherently reliable because it has baked-in preferences and blind spots.</p><p>There are things that will drive you a bit mad, like a judge punishing an answer that&#8217;s grounded in the corpus but not explicitly stated in the retrieved chunks, or judging the same answer differently depending on how the question is phrased.</p><p>You&#8217;ll realize this later, when you dig into failing questions to understand why.</p><p>Another thing to keep in mind is not to cherry-pick questions, even if the temptation is there. The goal is to get close to what users are actually asking, identify the issues, and continuously update the dataset based on where the system seems to fail.</p><p>It&#8217;s easy to get nice numbers if you mostly test &#8220;easy&#8221; questions, but then the eval becomes useless.</p><p>Ideally, you want not just real user questions but also gold answers.</p><p>So even if you can &#8220;bypass&#8221; references by using an LLM judge, having correct answers is still ideal. That&#8217;s when you can use the LLM to judge whether the output matches the gold answer, instead of asking it to judge the answer with vibes.</p><p>Sample size matters too.</p><p>Too small and the results aren&#8217;t reliable. Too big and it&#8217;s easy to miss smaller problems. If you have enough data, you can tag questions by topic, phrasing (pessimistic, typical), and type (short, long, messy) to see what breaks where.</p><p>I&#8217;ve seen recommendations to start with something like 200&#8211;1,000 real queries with gold answers if you want this to resemble a real evaluation setup.</p><p>Since this entire exercise is hypothetical, and the system has ingested documents to demo the idea of expanding to neighbors, some of the eval datasets here are synthetically generated. That makes them less reliable, but there are still things we can learn from them.</p><h3>Deciding on metrics &amp; datasets</h3><p>This section is about two things: which metrics I&#8217;m using to evaluate the pipeline, and how I&#8217;m using them across datasets to see if neighbor expansion seems to help.</p><p>First, if you haven&#8217;t read about evals for LLM systems at all, go read <a href="https://medium.com/data-science-collective/agentic-ai-working-with-evals-b0dcedbe97f8">this article</a>. It gives you a taxonomy of the different metrics out there (RAG included).</p><p>Since I&#8217;m lazy for this, I needed reference-free metrics, but this will also limit us to what we can actually test. We can have the judge look at the context, the question, and the generated answer.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!Q2aX!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56ef39fe-ed4a-4c66-add0-68a2e7e70298_1400x784.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!Q2aX!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56ef39fe-ed4a-4c66-add0-68a2e7e70298_1400x784.png 424w, /__u/substackcdn.com/image/fetch/$s_!Q2aX!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56ef39fe-ed4a-4c66-add0-68a2e7e70298_1400x784.png 848w, /__u/substackcdn.com/image/fetch/$s_!Q2aX!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56ef39fe-ed4a-4c66-add0-68a2e7e70298_1400x784.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Q2aX!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56ef39fe-ed4a-4c66-add0-68a2e7e70298_1400x784.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!Q2aX!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56ef39fe-ed4a-4c66-add0-68a2e7e70298_1400x784.png" width="1400" height="784" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/56ef39fe-ed4a-4c66-add0-68a2e7e70298_1400x784.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:784,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!Q2aX!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56ef39fe-ed4a-4c66-add0-68a2e7e70298_1400x784.png 424w, /__u/substackcdn.com/image/fetch/$s_!Q2aX!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56ef39fe-ed4a-4c66-add0-68a2e7e70298_1400x784.png 848w, /__u/substackcdn.com/image/fetch/$s_!Q2aX!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56ef39fe-ed4a-4c66-add0-68a2e7e70298_1400x784.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Q2aX!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F56ef39fe-ed4a-4c66-add0-68a2e7e70298_1400x784.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>A few metrics that can help here are <strong>faithfulness</strong> (is the answer grounded in the provided context), <strong>answer relevancy</strong> (does it actually answer the question), <strong>context precision</strong> (how much of the context is just noise), and <strong>hallucination</strong> <strong>rate</strong> (how many claims are actually backed up by the provided context).</p><p>Since we want to figure out if seed expansion is useful, and without building two different pipelines, we can do one simple comparison: ask the judge to look at the seed chunks vs. the final expanded context and score how much of the answer comes from each for the faithfulness metric.</p><p><strong>If grounding improves when the judge sees the expanded context</strong>, that&#8217;s at least <strong>evidence that the model is using the expanded chunks</strong> and it&#8217;s not just noise. We would need more testing, though, to say for sure which is the winner.</p><p>Finally, the datasets matter as much as the metrics.</p><p>If you&#8217;ve read the over-engineered RAG <a href="https://medium.com/data-science-collective/how-to-build-an-over-engineered-retrieval-system-923bd7466931">article</a>, you know that all the docs that have been ingested are scientific articles that mention RAG. So all the questions that we create here need to be about RAG.</p><p>I have generated three different datasets with a different RAG flavor.</p><p>The <a href="https://github.com/ilsilfverskiold/Awesome-LLM-Resources-List/blob/main/guides/RAG/Custom/dataset_corpus.txt">first</a> is based on the ingested corpus, going through each scientific article and writing two questions each that it can answer.</p><p>The <a href="https://github.com/ilsilfverskiold/Awesome-LLM-Resources-List/blob/main/guides/RAG/Custom/dataset_corpus_messy.txt">second</a> is doing the same but providing messy questions like: <em>&#8220;how does k2 btw rag improve answer fetching compared to naive rag, like what&#8217;s the similarity scores in terms of q3?&#8221; </em>This messy user questions dataset could tell us if stating things differently would skew the results.</p><p>The <a href="https://github.com/ilsilfverskiold/Awesome-LLM-Resources-List/blob/main/guides/RAG/Custom/dataset_random.txt">third</a> dataset is based on 66 random RAG questions found online.</p><p>This means that these questions may not have answers in the corpus (the ingested RAG articles are just from September to October 2025, so we don&#8217;t know exactly what they contain).</p><p>To conclude, the first two will evaluate how well the pipeline behaves, whether it can answer questions on the documents it has, and the third one tells us what it is missing and how it behaves on questions that it might not be able to answer.</p><p>Though this is a bit simplified, as the first questions may be structured on sections and the random ones may be better answered by seed chunks.</p><h3>Setting up to run the evals</h3><p>To run the evals, you first need to run the pipeline on every question, for every model, and store the results.</p><p>If you don&#8217;t store everything you need, you can&#8217;t debug later. You want to be able to go from a low score back to the exact answer, the exact retrieved context, and the exact model settings.</p><p>I also wanted to compare models, because <strong>people assume &#8220;bigger model = better answers,&#8221;</strong> and that&#8217;s <strong>not always true</strong>, <strong>especially for easier tasks</strong>. So I&#8217;m running the same pipeline across GPT-5-mini, GPT-5.1, and GPT-5.2, for several datasets.</p><p>Once that&#8217;s done, I build the eval layer on top of those stored outputs. I used <a href="https://docs.ragas.io/en/stable/">RAGAS</a> for the standard metrics and <a href="https://deepeval.com/docs/getting-started">DeepEval</a> for the custom ones.</p><p>You can obviously build the tests manually, but it&#8217;s much easier this way.</p><p>I love how seamless DeepEval is, though it&#8217;s harder to debug if you find issues with the judge later.</p><p>A few specifics before I dig into the results: the pipeline runs with no context cap, the judge model is gpt-4o-mini, and we use n=3 for RAGAS and n=1 for the custom judges.</p><p>Since neighbor expansion is the whole point of this pipeline, we also run this check: for <strong>faithfulness</strong>, we score grounding against the seed chunks and against the full expanded context, to see if there&#8217;s a difference.</p><h3>Running the evals</h3><p>Let&#8217;s run the evals for the different datasets, metrics, and models to see how the pipeline is doing and how we can interpret the results.</p><p>You&#8217;ll find the full results <a href="https://docs.google.com/spreadsheets/d/1x0Y5m3aBs4jmo3PeWb0r9w43kZUtuajV9NCQS2m2OuA/edit?usp=sharing">here</a> and <a href="https://docs.google.com/spreadsheets/d/1eLi5KYiNbxvlyetjz71IhOcvo7gPfHtN-eUuXMutw9k/edit?usp=sharing">here</a> (especially if you dislike my childish sketches).</p><p>We can start with the results from the dataset generated by the corpus. You&#8217;ll see a fun sketch below with the numbers.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!6Ajm!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cb50dbf-7599-4a5e-ab65-ca2aa3aedd64_1400x865.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!6Ajm!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cb50dbf-7599-4a5e-ab65-ca2aa3aedd64_1400x865.png 424w, /__u/substackcdn.com/image/fetch/$s_!6Ajm!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cb50dbf-7599-4a5e-ab65-ca2aa3aedd64_1400x865.png 848w, /__u/substackcdn.com/image/fetch/$s_!6Ajm!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cb50dbf-7599-4a5e-ab65-ca2aa3aedd64_1400x865.png 1272w, /__u/substackcdn.com/image/fetch/$s_!6Ajm!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cb50dbf-7599-4a5e-ab65-ca2aa3aedd64_1400x865.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!6Ajm!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cb50dbf-7599-4a5e-ab65-ca2aa3aedd64_1400x865.png" width="1400" height="865" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9cb50dbf-7599-4a5e-ab65-ca2aa3aedd64_1400x865.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:865,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!6Ajm!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cb50dbf-7599-4a5e-ab65-ca2aa3aedd64_1400x865.png 424w, /__u/substackcdn.com/image/fetch/$s_!6Ajm!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cb50dbf-7599-4a5e-ab65-ca2aa3aedd64_1400x865.png 848w, /__u/substackcdn.com/image/fetch/$s_!6Ajm!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cb50dbf-7599-4a5e-ab65-ca2aa3aedd64_1400x865.png 1272w, /__u/substackcdn.com/image/fetch/$s_!6Ajm!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cb50dbf-7599-4a5e-ab65-ca2aa3aedd64_1400x865.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The table above shows the first RAGAS metrics. <strong>Faithfulness</strong> (does it stay grounded in the context provided) and <strong>answer relevancy</strong> (does it answer the question) are very high at around 0.95.</p><p>The models seem to perform pretty much the same.</p><p>This is to be expected, as we&#8217;re basically giving it questions that it should be able to answer with the documents. If these showed low numbers, there would be something severely off in the RAG pipeline.</p><p>It also gives us back <strong>seed faithfulness</strong>, where the judge is estimating how grounded the answer is to the seed chunks. This one is showing lower numbers than the full context <strong>faithfulness</strong>, 12&#8211;18 points across the different models.</p><p>This means that the chunk neighborhood expansion increases support coverage for an answer&#8217;s claims (higher judged <strong>faithfulness</strong> when the judge has full context).</p><p><strong>In fewer words: </strong>we can say that the LLM is using the full context, not just the seed chunks, when generating its answer.</p><p>What we can&#8217;t judge though is if the seed-only answer would have been just as good. This will require us to run two pipelines and compare the same metrics and datasets for each.</p><p>Now let&#8217;s look at these next metrics (for the same dataset).</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!mPb6!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93d835e1-d351-4801-9e0d-5f2b23e31588_1400x811.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!mPb6!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93d835e1-d351-4801-9e0d-5f2b23e31588_1400x811.png 424w, /__u/substackcdn.com/image/fetch/$s_!mPb6!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93d835e1-d351-4801-9e0d-5f2b23e31588_1400x811.png 848w, /__u/substackcdn.com/image/fetch/$s_!mPb6!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93d835e1-d351-4801-9e0d-5f2b23e31588_1400x811.png 1272w, /__u/substackcdn.com/image/fetch/$s_!mPb6!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93d835e1-d351-4801-9e0d-5f2b23e31588_1400x811.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!mPb6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93d835e1-d351-4801-9e0d-5f2b23e31588_1400x811.png" width="1400" height="811" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/93d835e1-d351-4801-9e0d-5f2b23e31588_1400x811.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:811,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!mPb6!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93d835e1-d351-4801-9e0d-5f2b23e31588_1400x811.png 424w, /__u/substackcdn.com/image/fetch/$s_!mPb6!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93d835e1-d351-4801-9e0d-5f2b23e31588_1400x811.png 848w, /__u/substackcdn.com/image/fetch/$s_!mPb6!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93d835e1-d351-4801-9e0d-5f2b23e31588_1400x811.png 1272w, /__u/substackcdn.com/image/fetch/$s_!mPb6!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93d835e1-d351-4801-9e0d-5f2b23e31588_1400x811.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>I would have estimated that <strong>context relevance</strong> would decrease here, as it&#8217;s looking at the full context that pulls in up to 10 different chunk neighbors for the section.</p><p>A reason for this may be that the questions generated are based on sections, which means that added context helps to answer them.</p><p><strong>Structure citations</strong> (i.e. does it cite its claims correctly) looks alright, but <strong>hallucination</strong> is high, which is good (1 means no made-up claims in the answer).</p><p>Now you&#8217;ll see that the different models provide very little difference in terms of performance.</p><p>Yes, this is quite an easy Q&amp;A task. But it does demonstrate that the additional size of the model may not be needed for everything, and the added context expansion may be able to act as a buffer for the smaller models.</p><p>Now let&#8217;s look at the results if we change the dataset to those messy user questions instead.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!Aqdw!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F105e7bf6-fb88-4da9-ae0c-22effb6a66fe_1400x592.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!Aqdw!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F105e7bf6-fb88-4da9-ae0c-22effb6a66fe_1400x592.png 424w, /__u/substackcdn.com/image/fetch/$s_!Aqdw!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F105e7bf6-fb88-4da9-ae0c-22effb6a66fe_1400x592.png 848w, /__u/substackcdn.com/image/fetch/$s_!Aqdw!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F105e7bf6-fb88-4da9-ae0c-22effb6a66fe_1400x592.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Aqdw!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F105e7bf6-fb88-4da9-ae0c-22effb6a66fe_1400x592.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!Aqdw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F105e7bf6-fb88-4da9-ae0c-22effb6a66fe_1400x592.png" width="1400" height="592" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/105e7bf6-fb88-4da9-ae0c-22effb6a66fe_1400x592.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:592,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!Aqdw!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F105e7bf6-fb88-4da9-ae0c-22effb6a66fe_1400x592.png 424w, /__u/substackcdn.com/image/fetch/$s_!Aqdw!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F105e7bf6-fb88-4da9-ae0c-22effb6a66fe_1400x592.png 848w, /__u/substackcdn.com/image/fetch/$s_!Aqdw!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F105e7bf6-fb88-4da9-ae0c-22effb6a66fe_1400x592.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Aqdw!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F105e7bf6-fb88-4da9-ae0c-22effb6a66fe_1400x592.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Here we compare messy with normal questions both using just GPT-5-mini</p><p>The table above shows us messy versus normal questions running GPT-5-mini.</p><p>We see a few drops in points, but they still stay high, though without isolating the outliers here we can&#8217;t say why.</p><p>But the median looks lower for <strong>faithfulness</strong> when only judging with the seed chunks for the messy user questions by an additional 7 points, which is interesting.</p><p>This could just be a result of using LLM judges that can fluctuate a few points up and down for each run (yes this happens).</p><p>Let&#8217;s now turn to the third dataset, which will be able to tell us a lot more. If we run the evals on it we get the results below.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!4ODB!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F95dcf1a0-7c03-4d36-ad7a-6da06b531f81_1400x759.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!4ODB!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F95dcf1a0-7c03-4d36-ad7a-6da06b531f81_1400x759.png 424w, /__u/substackcdn.com/image/fetch/$s_!4ODB!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F95dcf1a0-7c03-4d36-ad7a-6da06b531f81_1400x759.png 848w, /__u/substackcdn.com/image/fetch/$s_!4ODB!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F95dcf1a0-7c03-4d36-ad7a-6da06b531f81_1400x759.png 1272w, /__u/substackcdn.com/image/fetch/$s_!4ODB!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F95dcf1a0-7c03-4d36-ad7a-6da06b531f81_1400x759.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!4ODB!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F95dcf1a0-7c03-4d36-ad7a-6da06b531f81_1400x759.png" width="1400" height="759" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/95dcf1a0-7c03-4d36-ad7a-6da06b531f81_1400x759.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:759,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!4ODB!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F95dcf1a0-7c03-4d36-ad7a-6da06b531f81_1400x759.png 424w, /__u/substackcdn.com/image/fetch/$s_!4ODB!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F95dcf1a0-7c03-4d36-ad7a-6da06b531f81_1400x759.png 848w, /__u/substackcdn.com/image/fetch/$s_!4ODB!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F95dcf1a0-7c03-4d36-ad7a-6da06b531f81_1400x759.png 1272w, /__u/substackcdn.com/image/fetch/$s_!4ODB!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F95dcf1a0-7c03-4d36-ad7a-6da06b531f81_1400x759.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>We see all around worse numbers which is expected, the corpus that has been ingested probably can&#8217;t answer all of these questions so well. This will help us point to where we have missing information.</p><p><strong>Faithfulness</strong> stays high though still for the full context runs. Here the difference from the seed-only runs are a lot higher, which means the added expansion is being used more in the answer.</p><p>Something that was strange here was how GPT-5.2 consistently did worse for <strong>answer relevance</strong> across two different runs. This can be a metric thing, or it can be a model thing where it answers more cautiously than before, thus getting a lower score.</p><p>This also tells you why it&#8217;s so important to test these new models on your own pipelines before adding them in.</p><p>Let&#8217;s check the results of the additional metrics for this dataset too.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!VKpD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd53bbd76-efdc-4f08-afa6-c0fa17d00c17_1400x859.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!VKpD!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd53bbd76-efdc-4f08-afa6-c0fa17d00c17_1400x859.png 424w, /__u/substackcdn.com/image/fetch/$s_!VKpD!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd53bbd76-efdc-4f08-afa6-c0fa17d00c17_1400x859.png 848w, /__u/substackcdn.com/image/fetch/$s_!VKpD!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd53bbd76-efdc-4f08-afa6-c0fa17d00c17_1400x859.png 1272w, /__u/substackcdn.com/image/fetch/$s_!VKpD!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd53bbd76-efdc-4f08-afa6-c0fa17d00c17_1400x859.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!VKpD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd53bbd76-efdc-4f08-afa6-c0fa17d00c17_1400x859.png" width="1400" height="859" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d53bbd76-efdc-4f08-afa6-c0fa17d00c17_1400x859.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:859,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!VKpD!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd53bbd76-efdc-4f08-afa6-c0fa17d00c17_1400x859.png 424w, /__u/substackcdn.com/image/fetch/$s_!VKpD!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd53bbd76-efdc-4f08-afa6-c0fa17d00c17_1400x859.png 848w, /__u/substackcdn.com/image/fetch/$s_!VKpD!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd53bbd76-efdc-4f08-afa6-c0fa17d00c17_1400x859.png 1272w, /__u/substackcdn.com/image/fetch/$s_!VKpD!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd53bbd76-efdc-4f08-afa6-c0fa17d00c17_1400x859.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><strong>Context relevance</strong> is very low, so the judge thinks there is a lot of noise in the context that does not directly answer the question for this dataset.</p><p>The <strong>context relevance</strong> metric rewards retrieving a high proportion of directly relevant chunks, but seed+expand intentionally adds neighbor chunks (definitions, adjacent sections, captions) that should improve grounding.</p><p>Unfortunately, there may be some noise that comes with that.</p><p>Both <strong>structure citations</strong> and <strong>hallucination</strong> score worse here, probably because it&#8217;s harder to stay grounded if you&#8217;re not provided any relevant information to use.</p><p>Now this was a first build for this pipeline. Based on these results, you can improve it until you drive those numbers up. Maybe change the metrics to be more custom to your pipeline.</p><p>Both datasets show different things: the corpus-based one shows the pipeline works, the random one shows there is missing information and that there is a lot more noise to navigate around.</p><h3>What to examine</h3><p>So what do you do now that you have all these scores? You can look at regressions, worst offenders, and do some random sampling.</p><p>Like I said before, this whole exercise is a bit like investigative work. But, there are three checks you can start with.</p><p><strong>First is regressions: </strong>check the stuff that used to pass and now fails following changes.</p><p><strong>Second is checking the worst offenders</strong> (bottom ~5&#8211;10 data points) to see what is happening. It could be the metric, the judge, it could be the retrieval, and it could be the model.</p><p>Below is one of the worst ones for full faithfulness and answer relevancy.</p><pre><code>&#8220;question&#8221;: &#8220;how often does entity linking mess up acronyms and lookalikes, and how do i test that&#8221;,
&#8220;answer&#8221;: &#8220;The documents do not report a direct number for &#8220;how often&#8221; entity linking fails specifically on acronyms or look&#8209;alike names.\n\nWhat we do see is that:\n- Even when structure is correct, LLMs often link to the wrong property or entity (semantic inaccuracies) in SPARQL generation over a scholarly KG [2]. This is the same failure mode you&#8217;d see when an acronym (e.g., NMF, ACE) is mapped to the wrong node.\n- In medical KGs, entity linking relies on exact keywords and then semantic similarity of embeddings when no exact match exists [3]. This last step explicitly allows linking to a merely &#8220;semantically related&#8221; concept rather than the exact one, which is where lookalikes and acronym collisions can occur [3].\n- In conversational entity&#8209;centric systems, the largest single error category is coreference/entity confusion (27% of failures) [4], showing that confusing similar mentions is a major practical problem even outside strict EL.\n\nHow to test this in your setting, using patterns from the papers:\n1) Build a targeted test set\n   - Create a small benchmark that stresses acronyms and visually/lexically similar entities, analogous to how MATE is used to stress cross&#8209;modal entity linking [1].\n   - Include positive pairs (correct acronym&#8594;entity) and hard negatives (different entities sharing acronym or near&#8209;identical names).\n\n2) Run your full EL pipeline\n   - Use the same identifier / keyword / embedding&#8209;based linking strategy described in MedKGent [3].\n\n3) Manually and/or LLM&#8209;assisted evaluation\n   - Have humans or an LLM score whether each link is correct vs. incorrect, similar to the triple&#8209;validity rubric used in MedKGent [3] and the SPARQL error audit in FIRESPARQL [2].\n\n4) Analyze error types\n   - Break down errors like the semantic vs. structural analysis in [2] and the error table in [4], but with categories &#8220;acronym collision,&#8221; &#8220;lookalike surface form,&#8221; etc.\n\nThis will give you an empirical error rate for your own stack, focused specifically on acronyms and lookalikes.&#8221;,
&#8220;full_faithfulness&#8221;: 0.24,
&#8220;seed_faithfulness&#8221;: 0.8260869565000001,
&#8220;answer_relevancy&#8221;: 0.0,
&#8220;context_relevance&#8221;: 0.208549739206933,
&#8220;context_relevance_reason&#8221;: &#8220;The context provided does not directly address the user&#8217;s question about how often entity linking messes up acronyms and lookalikes, nor does it offer methods for testing that. While it discusses entity linking and its evolution, it lacks specific information on the issues related to acronyms and lookalikes, which is the core of the user&#8217;s inquiry.&#8221;,
&#8220;hallucination_score&#8221;: 0.6572611409640697,
&#8220;hallucination_reason&#8221;: &#8220;The response accurately identifies that the documents do not provide a specific frequency for how often entity linking fails with acronyms or lookalikes, which aligns with the input query. It also discusses relevant issues such as semantic inaccuracies and coreference confusion, which are pertinent to the topic. However, it lacks direct references to specific claims made in the context, such as the limitations of traditional EL methods or the role of exact keywords in medical KGs, which could have strengthened the response further.&#8221;,
&#8220;full_contexts&#8221;: [&#8221;Entity Linking\n\nEntity Linking (EL) has evolved from text-only methods to Multimodal Entity Linking (MEL), and more recently to Cross-Modal Entity Linking (CMEL), which supports crossmodal reasoning. Traditional EL methods associate textual entities with their corresponding entries in a knowledge base, but overlook non-textual information (Shen, Wang, and Han 2015; Shen et al. 2023). MEL extends EL by incorporating visual [truncated]]</code></pre><p>Here we can see the corpus didn&#8217;t actually contain direct evidence for what was asked but the seed chunks were more contained so faithfulness got a higher score when only providing the seed chunks.</p><p>Answer relevancy was punished unusually low for not being able to answer the direct question.</p><p>This one was an outlier though and in general full faithfulness is higher than seed faithfulness.</p><p>You can <strong>also check a good one</strong> to understand how it estimates the ones that passes.</p><pre><code>&#8220;question&#8221;: &#8220;What retrieval settings help reduce needle-in-a-haystack issues for medical QA?&#8221;,
&#8220;answer&#8221;: &#8220;To reduce &#8220;needle&#8209;in&#8209;a&#8209;haystack&#8221; problems in medical QA, the retrieval setup should focus on bringing back a small, very focused set of highly relevant items instead of large, noisy context.\n\n1. Control context size and granularity\n- Use abstracts or snippets rather than full articles; long, unfocused context diluted model performance in biomedical QA, so reranking down to a smaller, high&#8209;quality subset was necessary for good answers [1].\n- Limit the number of documents passed to the model (e.g., rerank from thousands down to a few hundred or less) to avoid disorienting the model with too much text [1].\n\n2. Use multi&#8209;stage retrieval\n- Start with high&#8209;recall lexical retrieval (e.g., BM25 over PubMed) to get a broad set of candidates (up to thousands) [1].\n- Apply a strong semantic reranker to pick only the top slice for RAG (e.g., top 300) [1].\n- If evidence is missing, fall back to broader web&#8209;or multi&#8209;source search over trusted medical sites to fill gaps, rather than over&#8209;stuffing the first pass with everything [1].\n\n3. Improve query quality\n- Use an LLM to rewrite or expand queries (synonyms, related terms, less rare jargon) when initial recall is poor, boosting the chance that the key paper is retrieved without over&#8209;expanding every query by default [1].\n\n4. Use structured, corpus&#8209;aware retrieval\n- Retrieve from structured medical knowledge sources such as knowledge graphs and prepend only the most relevant subgraph, which consistently improved accuracy across several medical QA benchmarks [3].\n- In multimodal/heterogeneous setups, use corpus&#8209;specific retrieval for different knowledge sources so each corpus is searched with methods tuned to its content, instead of one generic retriever over everything [2].&#8221;,
&#8220;full_faithfulness&#8221;: 1.0,
&#8220;seed_faithfulness&#8221;: 0.8636363636000001,
&#8220;answer_relevancy&#8221;: 0.9135841092,
&#8220;context_relevance&#8221;: 0.8976322813389481,
&#8220;context_relevance_reason&#8221;: &#8220;The context passages provide comprehensive insights into retrieval settings that can mitigate needle-in-a-haystack issues in medical QA. Specifically, the discussion on the integration of LLMs for information retrieval, the use of semantic reranking, and the multi-stage retrieval approach directly addresses the user&#8217;s question. The emphasis on maintaining relevance while expanding query coverage and the mention of ensemble methods highlight effective strategies for improving retrieval accuracy in complex biomedical queries. However, while the information is highly relevant, a more explicit connection to specific &#8216;needle-in-a-haystack&#8217; challenges could enhance clarity.&#8221;,
&#8220;hallucination_score&#8221;: 0.8893376167284271,
&#8220;full_contexts&#8221;: [&#8221;Abstract\n\nBiomedical question answering (QA) poses significant challenges due to the need for precise interpretation of specialized knowledge drawn from a vast, complex, and rapidly evolving corpus. In this work, we explore how large language models (LLMs) [truncated] ]</code></pre><p>Here, the extra (full) context actually contains the missing evidence which helped and therefore got a higher score than the seeds.</p><p><strong>Lastly, check a few random ones</strong> and see how it is evaluating these.</p><pre><code>&#8220;question&#8221;: &#8220;how do i measure if i\u2019m getting diverse sources instead of 10 chunks from the same pdf&#8221;, 
&#8220;answer&#8221;: &#8220;You can treat \u201cdiverse sources\u201d as something you measure directly, not just eyeball.\n\n1. Simple source-diversity score (like in the paper)\nIn Document 1, they define a retrieval diversity metric as the fraction of unique review IDs among the top\u201110 chunks: Diversity = (number of distinct IDs) / 10 [1].\nYou can copy this idea for documents:\n- Give every PDF (or document) a unique ID.\n- When you retrieve K chunks (say K=10), look at which document each chunk came from.\n- Compute: Diversity = (number of distinct document IDs) / K.\n  \u2022 If all 10 chunks come from the same PDF, Diversity = 1 / 10 = 0.1 (very low).\n  \u2022 If all 10 chunks come from different PDFs, Diversity = 10 / 10 = 1.0 (perfect diversity).\n\nYou can log this number for each query to monitor whether you\u2019re repeatedly pulling from the same file or actually spreading across your corpus.\n\n2. Combine with relevance\nIn Document 1, they also track how close the chunks are to the query using an average cosine similarity between the query embedding and each chunk [1]. In practice you want both:\n- High similarity (on\u2011topic).\n- High diversity (many different source IDs).\n\n3. Optional: richer diversity later\nDocument 2 shows that diversity can also be measured at different levels (lexical, semantic, ingredient) when you have multiple outputs for one input [2]. By analogy, once you\u2019re happy with source diversity, you could later add:\n- Semantic diversity between chunks (are they saying different things?).\nBut the easiest starting point is the per-query \u2018distinct document IDs / K\u2019 metric.&#8221;,
&#8220;full_faithfulness&#8221;: 0.8823529412000001, 
&#8220;seed_faithfulness&#8221;: 0.5294117647000001,
&#8220;answer_relevancy&#8221;: 0.8588673985, 
&#8220;context_relevance&#8221;: 0.5303662744007874,
&#8220;context_relevance_reason&#8221;: &#8220;The context passages provide relevant information about measuring diversity in retrieval systems, particularly in the context of recipe adaptation and LLMs. However, while some passages discuss diversity metrics and retrieval methods, they do not directly address the user&#8217;s specific question about measuring diverse sources versus multiple chunks from the same PDF. The relevance of the context is somewhat indirect, leading to a moderate score.&#8221;,
&#8220;hallucination_score&#8221;: 0.7209711030557213,
&#8220;hallucination_reason&#8221;: &#8220;The response effectively outlines a method for measuring source diversity by introducing a simple source-diversity score and providing a clear formula. It aligns well with the context, which discusses retrieval diversity metrics. However, while it mentions combining relevance with diversity, it does not explicitly connect this to the context&#8217;s focus on average cosine similarity, which could enhance the completeness of the answer. Overall, the claims are mostly supported, with minor gaps in direct references to the context.&#8221;
&#8220;full_context&#8221;: [&#8221;D. Question and Answering (QA)\n\nFor retrieval of reviews, we sampled five Spotify-centric queries and retrieved the top K = 10 review chunks for each. We measured two unsupervised metrics... [truncated]</code></pre><p>This above is interesting as you see that the evaluator is taking a reasonable generalization and treats it as &#8220;kinda supported&#8221; or &#8220;meh.&#8221;</p><p>Evaluating this item above with another LLM, it said that it thought <em>the context relevance</em> comment was a bit whiny.</p><p>But as you see, low scores don&#8217;t have to mean that the system is bad. You have to examine why they are low and also why they are high to understand how the judge works and why the pipeline is failing.</p><p>A good example is <strong>context precision </strong>for this.</p><p>Context precision is measuring how much of the retrieved context was useful. If you&#8217;re doing neighbor expansion, you will almost always pull in some irrelevant text, so context precision will look worse, especially if the corpus can&#8217;t answer the question in the first place.</p><p>The question is whether the extra context actually helps grounding (faithfulness / hallucination rate) enough to be worth the noise.</p><h3>Some ending notes</h3><p>Okay, some notes before I round this off.</p><p>Testing seeds here is clearly biased, and it doesn&#8217;t tell us whether they were actually useful on their own. We&#8217;d have to build two different pipelines and compare them side by side to say that properly.</p><p>I&#8217;ll do this in the future, and I&#8217;ll be honest about it.</p><p>I should note that the system has very few docs in the pipeline: only about 150 PDF files along with some Excel files, which is a few thousand pages.</p><p><strong>But I have to demo this in public, and this was the only way.</strong></p><p>Remember we used only metrics on the generation side here, looking at the context that was retrieved. If the context retrieved is lying or has conflicting information, these metrics may not show it, you have to measure that before.</p><p>Furthermore many teams also build their own custom metrics, that&#8217;s unique to their pipeline and to what they want to test, there are pitfalls to this, so I&#8217;m not sure I would recommend it, but you should always test what is useful to your application.</p><p>The last thing to note is LLM judge bias. I&#8217;m using OpenAI models both for the RAG pipeline and for the evaluator. You should be fine as long as you make sure it&#8217;s not the same model, but ideally it&#8217;s good to use different providers for the judges.</p><p>If you&#8217;ve gotten this far, I hope you liked it and that it gave you some intel into how to work with evals.</p><p>I hope you learned that running evals with LLMs as judges is risky and that smaller models do fine when you serve information up front.</p><p>As for this use case, we can say that the fancy add-ons doesn&#8217;t seem to cause that much issues, but without testing two different pipelines against each other it&#8217;s hard to understand which one wins. So stay tuned for the article that will test both against each other.</p><p>Remember you can look at the full results <a href="https://docs.google.com/spreadsheets/d/1eLi5KYiNbxvlyetjz71IhOcvo7gPfHtN-eUuXMutw9k/edit?gid=1924336653#gid=1924336653">here</a> and <a href="https://docs.google.com/spreadsheets/d/1x0Y5m3aBs4jmo3PeWb0r9w43kZUtuajV9NCQS2m2OuA/edit?gid=817165998#gid=817165998">here</a>.</p><p>&#10084;</p>]]></content:encoded></item><item><title><![CDATA[How Web Search Inside AI Chatbots Works]]></title><description><![CDATA[And what this architecture means for generative engine optimization (GEO)]]></description><link>https://howtouseai.substack.com/p/how-web-search-inside-ai-chatbots</link><guid isPermaLink="false">https://howtouseai.substack.com/p/how-web-search-inside-ai-chatbots</guid><dc:creator><![CDATA[Ida Silfverskiold]]></dc:creator><pubDate>Tue, 01 Sep 2026 08:36:06 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!S8wI!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e2858aa-100e-4871-b9ad-2823cc908156_1400x694.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!S8wI!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e2858aa-100e-4871-b9ad-2823cc908156_1400x694.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!S8wI!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e2858aa-100e-4871-b9ad-2823cc908156_1400x694.png 424w, /__u/substackcdn.com/image/fetch/$s_!S8wI!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e2858aa-100e-4871-b9ad-2823cc908156_1400x694.png 848w, /__u/substackcdn.com/image/fetch/$s_!S8wI!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e2858aa-100e-4871-b9ad-2823cc908156_1400x694.png 1272w, /__u/substackcdn.com/image/fetch/$s_!S8wI!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e2858aa-100e-4871-b9ad-2823cc908156_1400x694.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!S8wI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e2858aa-100e-4871-b9ad-2823cc908156_1400x694.png" width="1400" height="694" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5e2858aa-100e-4871-b9ad-2823cc908156_1400x694.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:694,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!S8wI!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e2858aa-100e-4871-b9ad-2823cc908156_1400x694.png 424w, /__u/substackcdn.com/image/fetch/$s_!S8wI!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e2858aa-100e-4871-b9ad-2823cc908156_1400x694.png 848w, /__u/substackcdn.com/image/fetch/$s_!S8wI!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e2858aa-100e-4871-b9ad-2823cc908156_1400x694.png 1272w, /__u/substackcdn.com/image/fetch/$s_!S8wI!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5e2858aa-100e-4871-b9ad-2823cc908156_1400x694.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Splitting discovery and retrieval when looking at the pipeline | All images by author</figcaption></figure></div><p><em>This is an article dated December 2025 but has been re-published here.</em></p><p>When you ask ChatGPT or Claude to &#8220;search the web,&#8221; it isn&#8217;t just answering from its training data. It&#8217;s calling a separate search system.</p><p>Most people know that part.</p><p>What&#8217;s less clear is how much traditional search engines matter and how much has been built on top of them.</p><p>All of it isn&#8217;t fully public, so I&#8217;m doing some mental deduction here. But we can use different hints from looking at larger systems to build a useful mental model.</p><p>We&#8217;ll go through query optimization, how search engines are used for discovery, chunking content, &#8220;on-the-fly&#8221; retrieval, and how you could potentially reverse-engineer a system like this to build a &#8220;GEO [Generative Engine Optimization] scoring system.&#8221;</p><p>If you&#8217;re familiar with RAG, some of this will be repetition, but it can still be useful to see how larger systems split the pipeline into a discovery phase and a retrieval phase.</p><p>If you&#8217;re short on time, you can read the TL;DR.</p><h3><strong>TL;DR</strong></h3><p>Web search in these AI chatbots is likely a two-part process. The first part leans on traditional search engines to find and rank candidate docs. In the second part, they fetch the content from those URLs and pull out the most relevant passages using passage-level retrieval.</p><p>The big change (from traditional SEO) is query rewriting and passage-level chunking, which let lower-ranked pages outrank higher ones if their specific paragraphs match the question better.</p><h2>The technical process</h2><p>The companies behind Claude and ChatGPT aren&#8217;t fully transparent about how their web search systems work within the UI chat, but we can infer a lot by piecing things together.</p><p>We know they lean on search engines to find candidates, at this scale, it would be absurd not to. We also know that what the LLM actually sees are pieces of text (chunks or passages) when grounding their answer.</p><p>This strongly hints at some kind of embedding-based retrieval over those chunks rather than over full pages.</p><p>This process has several parts, so we&#8217;ll go through it step by step.</p><h3>Query re-writing &amp; fan-out</h3><p>First, we&#8217;ll look at how the system cleans up human queries and expands them. We&#8217;ll cover the rewrite step, the fan-out step, and why this matters for SEO.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!j_OV!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8c1c08e-e62d-4408-b2f9-19926f753aef_1400x485.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!j_OV!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8c1c08e-e62d-4408-b2f9-19926f753aef_1400x485.png 424w, /__u/substackcdn.com/image/fetch/$s_!j_OV!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8c1c08e-e62d-4408-b2f9-19926f753aef_1400x485.png 848w, /__u/substackcdn.com/image/fetch/$s_!j_OV!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8c1c08e-e62d-4408-b2f9-19926f753aef_1400x485.png 1272w, /__u/substackcdn.com/image/fetch/$s_!j_OV!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8c1c08e-e62d-4408-b2f9-19926f753aef_1400x485.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!j_OV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8c1c08e-e62d-4408-b2f9-19926f753aef_1400x485.png" width="1400" height="485" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f8c1c08e-e62d-4408-b2f9-19926f753aef_1400x485.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:485,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!j_OV!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8c1c08e-e62d-4408-b2f9-19926f753aef_1400x485.png 424w, /__u/substackcdn.com/image/fetch/$s_!j_OV!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8c1c08e-e62d-4408-b2f9-19926f753aef_1400x485.png 848w, /__u/substackcdn.com/image/fetch/$s_!j_OV!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8c1c08e-e62d-4408-b2f9-19926f753aef_1400x485.png 1272w, /__u/substackcdn.com/image/fetch/$s_!j_OV!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff8c1c08e-e62d-4408-b2f9-19926f753aef_1400x485.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>I think this part might be the most transparent, and the one most people seem to agree on online.</p><p>The query optimization step is about taking a human query and turning it into something more precise. For example, &#8220;please search for those red shoes we talked about earlier&#8221; becomes &#8220;brown-red Nike sneakers.&#8221;</p><p>The fan-out step, on the other hand, is about generating additional rewrites. So if a user asks about hiking routes near me, the system might try things like &#8220;beginner hikes near Stockholm,&#8221; &#8220;day hikes near Stockholm public transport,&#8221; or &#8220;family-friendly trails near Stockholm.&#8221;</p><p>This is different from just using synonyms, which traditional search engines are already optimized for.</p><p>If this is the first time you&#8217;re hearing about it and you&#8217;re unconvinced, take a look at Google&#8217;s own docs on <a href="https://developers.google.com/search/docs/appearance/ai-features">AI query fan-out</a> or do a bit of digging around query rewriting.</p><p>To what extent this actually works, we can&#8217;t know. They may not fan it out that much and just work with a single query, then send additional ones down the pipeline if the results are lackluster.</p><p>What we <em>can</em> say is that it&#8217;s probably not a big model doing this part. If you look at the research, <a href="https://aclanthology.org/2023.findings-emnlp.398/">Ye et al.</a> explicitly use an LLM to generate strong rewrites, then distill that into a smaller rewriter to avoid latency and cost overhead.</p><p>As for what this part of the pipeline means, for the business and SEO people out there, it means those human queries you&#8217;ve been optimizing for are getting transformed into more robotic, document-shaped ones.</p><p>SEO, as I understand it, used to care a lot about matching the exact long-tail phrase in titles and headings. If someone searched for &#8220;best running shoes for bad knees,&#8221; you&#8217;d stick to that exact string.</p><p>What you need to care about now is also entities, attributes, and relationships.</p><p>So, if a user asks for &#8220;something for dry skin,&#8221; the rewrites might include things like &#8220;moisturizer,&#8221; &#8220;occlusive,&#8221; &#8220;humectant,&#8221; &#8220;ceramides,&#8221; &#8220;fragrance-free,&#8221; &#8220;avoid alcohols&#8221; and not just &#8220;how would I find a good product for dry skin.&#8221;</p><p>But let&#8217;s be clear so there&#8217;s no confusion: we can&#8217;t see the internal rewrites themselves, so these are just examples.</p><p>If you&#8217;re interested in this part, you can dig deeper. I bet there are plenty of papers out there on how to do this well.</p><p>Let&#8217;s move on to what these optimized queries are actually used for.</p><h3>Using search engines (for doc level discovery)</h3><p>It&#8217;s pretty common knowledge by now that, to get up-to-date answers, most AI bots rely on traditional search engines. That&#8217;s not the whole story, but it does cut the web down to something smaller to work with.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!jVXi!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0156a9c1-bf4d-4b0e-a984-6f27f65caae8_1400x531.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!jVXi!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0156a9c1-bf4d-4b0e-a984-6f27f65caae8_1400x531.png 424w, /__u/substackcdn.com/image/fetch/$s_!jVXi!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0156a9c1-bf4d-4b0e-a984-6f27f65caae8_1400x531.png 848w, /__u/substackcdn.com/image/fetch/$s_!jVXi!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0156a9c1-bf4d-4b0e-a984-6f27f65caae8_1400x531.png 1272w, /__u/substackcdn.com/image/fetch/$s_!jVXi!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0156a9c1-bf4d-4b0e-a984-6f27f65caae8_1400x531.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!jVXi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0156a9c1-bf4d-4b0e-a984-6f27f65caae8_1400x531.png" width="1400" height="531" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0156a9c1-bf4d-4b0e-a984-6f27f65caae8_1400x531.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:531,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!jVXi!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0156a9c1-bf4d-4b0e-a984-6f27f65caae8_1400x531.png 424w, /__u/substackcdn.com/image/fetch/$s_!jVXi!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0156a9c1-bf4d-4b0e-a984-6f27f65caae8_1400x531.png 848w, /__u/substackcdn.com/image/fetch/$s_!jVXi!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0156a9c1-bf4d-4b0e-a984-6f27f65caae8_1400x531.png 1272w, /__u/substackcdn.com/image/fetch/$s_!jVXi!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0156a9c1-bf4d-4b0e-a984-6f27f65caae8_1400x531.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>I&#8217;m assuming the full web is too large, too noisy, and too fast-changing for an LLM pipeline to pull raw content directly. So by using already established search engines, you get a way to narrow the universe.</p><p>If you look at larger RAG pipelines that work with millions of documents, they do something similar. I.e. using a filter of some sort to decide which documents are important and worth further processing.</p><p>For this part, we do have proof.</p><p>Both <a href="https://help.openai.com/en/articles/9237897-chatgpt-search">OpenAI</a> and <a href="https://techcrunch.com/2025/03/21/anthropic-appears-to-be-using-brave-to-power-web-searches-for-its-claude-chatbot/">Anthropic</a> have said they use third-party search engines like Bing and Brave, alongside their own crawlers.</p><p>Perplexity may have built out this part on their own by now, but in the beginning, they would have done the same.</p><p>We also have to consider that traditional search engines like Google and Bing have already solved the hardest problems. They&#8217;re an established technology that handles things like language detection, authority scoring, domain trust, spam filtering, recency, and so on.</p><p>Throwing all of that away to embed the entire web yourself seems unlikely.</p><p>However, we don&#8217;t know how many results they actually fetch per query, whether it&#8217;s just the top 20 or 30. One <a href="https://www.seerinteractive.com/insights/87-percent-of-searchgpt-citations-match-bings-top-results">unofficial article</a> compared citations from ChatGPT and Bing, looked at the ranking order, and found that some came from as far down as 22nd place.</p><p>If true, this suggests you need to aim for top-20-ish visibility.</p><p>Furthermore, we also don&#8217;t know what other metrics they use to decide what surfaces from there. This <a href="https://arxiv.org/html/2509.08919v1">article</a> argues that AI engines heavily favor <em>earned media</em> rather than official sites or socials.</p><p>Still, the search engine&#8217;s job (whether it&#8217;s fully third-party or a mix) is discovery. It ranks the URL based on authority and keywords.</p><p>It might include a snippet of information, but that alone won&#8217;t be enough to answer the question. Unless of course it&#8217;s a very easy question like &#8220;who is the CEO of X?&#8221;</p><p>But for deeper questions, if the model relied only on the snippet, plus the title and URL, it would likely hallucinate the details. It&#8217;s not enough context.</p><p>So this pushes us toward a two-stage architecture, where a retrieval step is baked in (which we&#8217;ll get to soon).</p><p>What does this mean in terms of SEO?</p><p>It means you still need to rank high in traditional search engines to be included in that initial batch of documents that gets processed. So, yes, classic SEO still matters.</p><p>But it may also mean you need to think about po<em>tential new</em> metrics they might be using to rank those results.</p><p>Nevertheless, this stage is all about narrowing the universe to a few pages worth digging into, using established search tech plus internal knobs. Everything else (the &#8220;it returns passages of information&#8221; part) comes after this step, using standard retrieval techniques.</p><h3>Crawl, chunk &amp; retrieve</h3><p>Now let&#8217;s move on to what happens when the system has identified a handful of interesting URLs.</p><p>Once a small set of URLs passes the first filter, the pipeline is fairly straightforward: crawl the page, break it into pieces, embed those pieces, retrieve the ones that match the query, and then re-rank them. This is what&#8217;s called retrieval.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!CFsx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16f3b59e-b796-4187-8c81-51882902daee_1400x621.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!CFsx!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16f3b59e-b796-4187-8c81-51882902daee_1400x621.png 424w, /__u/substackcdn.com/image/fetch/$s_!CFsx!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16f3b59e-b796-4187-8c81-51882902daee_1400x621.png 848w, /__u/substackcdn.com/image/fetch/$s_!CFsx!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16f3b59e-b796-4187-8c81-51882902daee_1400x621.png 1272w, /__u/substackcdn.com/image/fetch/$s_!CFsx!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16f3b59e-b796-4187-8c81-51882902daee_1400x621.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!CFsx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16f3b59e-b796-4187-8c81-51882902daee_1400x621.png" width="1400" height="621" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/16f3b59e-b796-4187-8c81-51882902daee_1400x621.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:621,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!CFsx!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16f3b59e-b796-4187-8c81-51882902daee_1400x621.png 424w, /__u/substackcdn.com/image/fetch/$s_!CFsx!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16f3b59e-b796-4187-8c81-51882902daee_1400x621.png 848w, /__u/substackcdn.com/image/fetch/$s_!CFsx!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16f3b59e-b796-4187-8c81-51882902daee_1400x621.png 1272w, /__u/substackcdn.com/image/fetch/$s_!CFsx!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F16f3b59e-b796-4187-8c81-51882902daee_1400x621.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>I call it <em>on-the-fly</em> here because the system only embeds chunks once a URL becomes a candidate, then it caches those embeddings for reuse. This part might be new if you&#8217;re already familiar with retrieval.</p><p>To crawl the page, it seem like they use their own crawlers. For OpenAI, this is OAI-SearchBot, which should then fect the raw HTML so it can be processed.</p><p>Crawlers don&#8217;t typically execute JavaScript. They most likely rely on server-rendered HTML, so the same SEO rules apply: content needs to be accessible.</p><p>Once the HTML is fetched, the content has to be turned into something searchable.</p><p>If you&#8217;re new to this, it might feel like the AI &#8220;scans the document,&#8221; but that&#8217;s not what happens. Scanning entire pages per query would be too slow and too expensive.</p><p>Instead, pages are split into passages, usually guided by HTML structure: headings, paragraphs, lists, section breaks, that kind of thing. These are called <em>chunks</em> in the context of retrieval.</p><p>Each chunk becomes a small, self-contained unit. Token-wise, you can see from Perplexity UI citations that chunks are on the order of tens of tokens, maybe around 150, not 1,000. That&#8217;s about 110&#8211;120 words.</p><p>After chunking, those units are embedded using both sparse and dense vectors. This enables the system to run hybrid search and match a query both semantically and by keyword.</p><p>Once a popular page has been chunked and embedded, those embeddings are probably cached. No one is re-embedding the same StackOverflow answer thousands of times a day.</p><p>This is obviously why the system feels so fast, probably the hot 95&#8211;98 % of the web that actually gets cited is already embedded, and cached aggressively.</p><p>We don&#8217;t know to what extent though and how much they pre-embed to make sure the system runs fast for popular queries.</p><p>Nevertheless, now the system needs to figure out which chunks matter. It uses the embeddings for each chunk of text to compute a score for both semantic and keyword matching.</p><p>If you&#8217;re new to semantic search, in short, it means the system searches for meaning instead of exact words. <em>So a query like &#8220;symptoms of iron deficiency&#8221; and &#8220;signs your body is low on iron&#8221; would still land near each other in embedding space. You can read more on embeddings <a href="https://medium.com/data-science/working-with-embeddings-closed-versus-open-source-39491f0b95c2">here</a> if you&#8217;re keen to learn how it works.</em></p><p>After this it picks the chunks with the highest scores. This can be anything from 10 to 50 top-scoring chunks.</p><p>Most mature systems also add in a re-ranker (cross-encoder) to process those top chunks again, doing another round of ranking. This is the &#8220;fix the retrieval mess&#8221; stage, because unfortunately retrieval isn&#8217;t always completely reliable for a lot of reasons.</p><p>Although they say nothing about using a cross-encoder, Perplexity is one of the few that documents their retrieval process openly.</p><p>Their <a href="https://www.perplexity.ai/hub/blog/introducing-the-perplexity-search-api">Search API</a> says they &#8220;divide documents into fine-grained units&#8221; and score those units individually so they can return the &#8220;most relevant snippets already ranked.&#8221;</p><p>What does this all mean for SEO? If the system is doing retrieval like this, your page isn&#8217;t treated as one big blob.</p><p>It&#8217;s broken into pieces (often paragraph or heading level) and those pieces are what get scored. The full page matters during discovery, but once retrieval begins, it&#8217;s the chunks that matter.</p><p>That means each chunk needs to answer the user&#8217;s question.</p><p>It also means that if your important information isn&#8217;t contained inside a single chunk, the system can lose context. Retrieval isn&#8217;t magic. The model never sees your full page.</p><p><em>Note, this does depend on their chunking strategy, but I&#8217;m assuming they don&#8217;t do anything too fancy here.</em></p><p>So now we&#8217;ve covered the retrieval stage: where the system crawls pages, chops them into units, embeds those units, and then uses hybrid retrieval and re-ranking to pull out only the passages that can answer the user&#8217;s question.</p><h3>Doing another pass &amp; handing over chunks to LLM</h3><p>Now let&#8217;s move on to what happens after the retrieval part, including the &#8220;continuing to search&#8221; feature and handing the chunks to the main LLM.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!xmqK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18e41707-96d7-4285-af2e-dff17ca204a0_1400x575.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!xmqK!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18e41707-96d7-4285-af2e-dff17ca204a0_1400x575.png 424w, /__u/substackcdn.com/image/fetch/$s_!xmqK!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18e41707-96d7-4285-af2e-dff17ca204a0_1400x575.png 848w, /__u/substackcdn.com/image/fetch/$s_!xmqK!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18e41707-96d7-4285-af2e-dff17ca204a0_1400x575.png 1272w, /__u/substackcdn.com/image/fetch/$s_!xmqK!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18e41707-96d7-4285-af2e-dff17ca204a0_1400x575.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!xmqK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18e41707-96d7-4285-af2e-dff17ca204a0_1400x575.png" width="1400" height="575" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/18e41707-96d7-4285-af2e-dff17ca204a0_1400x575.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:575,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!xmqK!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18e41707-96d7-4285-af2e-dff17ca204a0_1400x575.png 424w, /__u/substackcdn.com/image/fetch/$s_!xmqK!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18e41707-96d7-4285-af2e-dff17ca204a0_1400x575.png 848w, /__u/substackcdn.com/image/fetch/$s_!xmqK!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18e41707-96d7-4285-af2e-dff17ca204a0_1400x575.png 1272w, /__u/substackcdn.com/image/fetch/$s_!xmqK!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F18e41707-96d7-4285-af2e-dff17ca204a0_1400x575.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Once the system has identified a few high-ranking chunks, it has to decide whether they&#8217;re good enough or if it needs to keep searching. This decision is almost certainly made by a small controller model, not the main LLM.</p><p>I&#8217;m guessing here, but if the material looks thin or off-topic, it may run another round of retrieval. If it looks solid, it can hand those chunks over to the LLM.</p><p>At some point, that handoff happens. The selected passages, along with some metadata, are passed to the main LLM.</p><p>The model reads all the provided chunks and picks whichever one best supports the answer it wants to generate.</p><p>It does not mechanically follow the retriever&#8217;s order though. So there&#8217;s no guarantee the LLM will use the &#8220;top&#8221; chunk. It may prefer a lower-ranked passage simply because it&#8217;s clearer, more self-contained, or closer to the phrasing needed for the answer.</p><p>So just like us, it decides what to take in and what to ignore. And even if your chunk scores the highest, there&#8217;s no assurance it will be the first one mentioned.</p><h3>What to think about</h3><p>This system isn&#8217;t really a black box. It&#8217;s a system people have built to hand the LLMs the right information to answer a user&#8217;s question.</p><p>If what I&#8217;ve inferred here is true, then it finds candidates, splits documents into units, searches and ranks those units, and then hands them over to an LLM to summarize.</p><p>From this we can also figure out what we need to think about when creating content for it.</p><p>Traditional SEO still matters a lot, because this system leans on the old one. Things like having a proper sitemap, easily rendered content, proper headings, domain authority, and accurate last-modified tags are all important for your content to be sorted correctly.</p><p>As I pointed out, they may be mixing search engines with their own technology to decide which URLs get picked, which is worth keeping in mind.</p><p>But if they use retrieval on top of it, then paragraph level relevance is the new leverage point.</p><p>This means answer-in-one-chunk design might rule. (Just don&#8217;t do it in a way that feels weird, maybe a TL;DR.) And remember to use the right vocabulary: entities, attributes, relationships, like we talked about in the query optimization section.</p><h3>How to build a &#8220;GEO Scoring System&#8221; (for fun)</h3><p>To figure out how well your content will do, we&#8217;ll have to simulate the hostile environment your content will live in. So let&#8217;s try to reverse engineer this pipeline.</p><p><em>Note, this is non-trivial, as we don&#8217;t know the internal metrics they use nor is this system public, so think of this as a rough blueprint.</em></p><p>The idea is to create a pipeline that can do query rewrite, discovery, retrieval, re-ranking and an LLM judge, and then see where you end up compared to your competitors for different topics.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!qtF7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F84e9f62a-7449-4b8d-b5f4-44f308a0d4ee_1400x491.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!qtF7!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F84e9f62a-7449-4b8d-b5f4-44f308a0d4ee_1400x491.png 424w, /__u/substackcdn.com/image/fetch/$s_!qtF7!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F84e9f62a-7449-4b8d-b5f4-44f308a0d4ee_1400x491.png 848w, /__u/substackcdn.com/image/fetch/$s_!qtF7!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F84e9f62a-7449-4b8d-b5f4-44f308a0d4ee_1400x491.png 1272w, /__u/substackcdn.com/image/fetch/$s_!qtF7!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F84e9f62a-7449-4b8d-b5f4-44f308a0d4ee_1400x491.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!qtF7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F84e9f62a-7449-4b8d-b5f4-44f308a0d4ee_1400x491.png" width="1400" height="491" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/84e9f62a-7449-4b8d-b5f4-44f308a0d4ee_1400x491.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:491,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!qtF7!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F84e9f62a-7449-4b8d-b5f4-44f308a0d4ee_1400x491.png 424w, /__u/substackcdn.com/image/fetch/$s_!qtF7!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F84e9f62a-7449-4b8d-b5f4-44f308a0d4ee_1400x491.png 848w, /__u/substackcdn.com/image/fetch/$s_!qtF7!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F84e9f62a-7449-4b8d-b5f4-44f308a0d4ee_1400x491.png 1272w, /__u/substackcdn.com/image/fetch/$s_!qtF7!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F84e9f62a-7449-4b8d-b5f4-44f308a0d4ee_1400x491.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>You begin with a few topics like &#8220;hybrid retrieval for enterprise RAG&#8221; or &#8220;LLM evaluation with LLM-as-judge,&#8221; and then build a system that generates natural queries around them.</p><p>Then you pass those queries through an LLM rewrite step, because these systems often reformulate the user query before retrieval. Those rewritten queries are what you actually push through the pipeline.</p><p>The first check is visibility. For each query, look at the top 20&#8211;30 results across Brave, Google and Bing. Note whether your page appears and where it sits relative to competitors.</p><p>At the same time, collect domain-level authority metrics (Moz DA, Ahrefs DR, etc.) so you can fold that in later, since these systems probably still lean heavily on those signals.</p><p>If your page appears in these first results, you move on to the retrieval part.</p><p>Fetch your page and the competing pages, clean the HTML, split them into chunks, embed those chunks, and build a small hybrid retrieval setup that combines semantic and keyword matching. Add a re-ranking step.</p><p>Somewhere here you also inject the authority signal, because higher-authority domains realistically get scored higher (even though we don&#8217;t know exactly how much).</p><p>Once you have the top chunks, you add the final layer: an LLM-as-a-judge. Being in the top five doesn&#8217;t guarantee citation, so you simulate the last step by handing the LLM a few of the top-scored chunks (with some metadata) and see which one it cites first.</p><p>When you run this for your pages and competitors, you see where you win or lose: the search layer, the retrieval layer or the LLM layer.</p><p>But remember this is still a rough sketch, but it gives you something to start with if you want to build a similar system.</p><p>This article focused on the mechanics rather than the strategy side of SEO/GEO, which I get won&#8217;t be for everyone.</p><p>The goal was to map the flow from a user query to the final answer and show that the AI search tool isn&#8217;t some opaque force.</p><p>Even if parts of the system aren&#8217;t public, we can still infer a reasonable sketch of what&#8217;s happening. What&#8217;s clear so far is that the AI web search doesn&#8217;t replace traditional search engines. It just layers retrieval on top of them.</p><p>There&#8217;s still more to figure out before saying what actually matters in practice. Here I&#8217;ve mostly gone through the technical pipeline but if this is new stuff I hoped it explain it well.</p><p>&#10084;</p>]]></content:encoded></item><item><title><![CDATA[Agentic AI: On Evaluations]]></title><description><![CDATA[Metrics to track for RAG and agents, plus the frameworks that help]]></description><link>https://howtouseai.substack.com/p/agentic-ai-on-evaluations-29a</link><guid isPermaLink="false">https://howtouseai.substack.com/p/agentic-ai-on-evaluations-29a</guid><dc:creator><![CDATA[Ida Silfverskiold]]></dc:creator><pubDate>Tue, 01 Sep 2026 08:31:49 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!eVif!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8574cca3-dd05-462f-a585-527da18ffa20_1400x729.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!eVif!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8574cca3-dd05-462f-a585-527da18ffa20_1400x729.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!eVif!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8574cca3-dd05-462f-a585-527da18ffa20_1400x729.png 424w, /__u/substackcdn.com/image/fetch/$s_!eVif!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8574cca3-dd05-462f-a585-527da18ffa20_1400x729.png 848w, /__u/substackcdn.com/image/fetch/$s_!eVif!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8574cca3-dd05-462f-a585-527da18ffa20_1400x729.png 1272w, /__u/substackcdn.com/image/fetch/$s_!eVif!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8574cca3-dd05-462f-a585-527da18ffa20_1400x729.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!eVif!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8574cca3-dd05-462f-a585-527da18ffa20_1400x729.png" width="1400" height="729" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8574cca3-dd05-462f-a585-527da18ffa20_1400x729.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:729,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!eVif!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8574cca3-dd05-462f-a585-527da18ffa20_1400x729.png 424w, /__u/substackcdn.com/image/fetch/$s_!eVif!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8574cca3-dd05-462f-a585-527da18ffa20_1400x729.png 848w, /__u/substackcdn.com/image/fetch/$s_!eVif!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8574cca3-dd05-462f-a585-527da18ffa20_1400x729.png 1272w, /__u/substackcdn.com/image/fetch/$s_!eVif!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8574cca3-dd05-462f-a585-527da18ffa20_1400x729.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Different metrics we can measure when we evaluate different systems | Image by author</figcaption></figure></div><p><em>This is an article dated August 2025 but has been re-published here.</em></p><p>Agentic Evals are mostly about testing your LLM application to make sure it performs consistently.</p><p>It&#8217;s not the most exciting topic, but more and more companies are paying attention. So it&#8217;s worth digging into which metrics to track to actually measure that performance.</p><p>It also helps to have proper evals in place anytime you push changes, to make sure things don&#8217;t go haywire.</p><p>So, for this article I&#8217;ve done some research on common metrics for multi-turn chatbots, RAG, and agentic applications.</p><p>I&#8217;ve also included a quick review of frameworks like DeepEval, RAGAS, and OpenAI&#8217;s Evals library, so you know when to pick what.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!r4T-!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8a64ada-a193-477d-9716-58db26b2ede3_1400x722.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!r4T-!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8a64ada-a193-477d-9716-58db26b2ede3_1400x722.png 424w, /__u/substackcdn.com/image/fetch/$s_!r4T-!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8a64ada-a193-477d-9716-58db26b2ede3_1400x722.png 848w, /__u/substackcdn.com/image/fetch/$s_!r4T-!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8a64ada-a193-477d-9716-58db26b2ede3_1400x722.png 1272w, /__u/substackcdn.com/image/fetch/$s_!r4T-!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8a64ada-a193-477d-9716-58db26b2ede3_1400x722.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!r4T-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8a64ada-a193-477d-9716-58db26b2ede3_1400x722.png" width="1400" height="722" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e8a64ada-a193-477d-9716-58db26b2ede3_1400x722.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:722,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!r4T-!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8a64ada-a193-477d-9716-58db26b2ede3_1400x722.png 424w, /__u/substackcdn.com/image/fetch/$s_!r4T-!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8a64ada-a193-477d-9716-58db26b2ede3_1400x722.png 848w, /__u/substackcdn.com/image/fetch/$s_!r4T-!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8a64ada-a193-477d-9716-58db26b2ede3_1400x722.png 1272w, /__u/substackcdn.com/image/fetch/$s_!r4T-!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8a64ada-a193-477d-9716-58db26b2ede3_1400x722.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">The different popular frameworks we&#8217;ll cover later around performing evals </figcaption></figure></div><p>This article is split in two. If you&#8217;re new, Part 1 talks a bit about traditional metrics like BLEU and ROUGE, touches on LLM benchmarks, and introduces the idea of using an LLM as a judge in evals.</p><p>If this isn&#8217;t new to you, you can skip this. Part 2 digs into evaluations of different kinds of LLM applications.</p><h2>What we did before</h2><p>If you&#8217;re well versed in how we evaluate NLP tasks and how public benchmarks work, you can skip this first part.</p><p>If you&#8217;re not, it&#8217;s good to know what the earlier metrics like accuracy and BLEU were originally used for and how they work, along with understanding how we test for public benchmarks like MMLU.</p><h3>Evaluating NLP tasks</h3><p>When we evaluate traditional NLP tasks such as classification, translation, summarization, and so on, we turn to traditional metrics like accuracy, precision, F1, BLEU, and ROUGE.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!l4wO!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88560248-a084-47ef-a8f9-af4e4b55da4a_1400x482.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!l4wO!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88560248-a084-47ef-a8f9-af4e4b55da4a_1400x482.png 424w, /__u/substackcdn.com/image/fetch/$s_!l4wO!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88560248-a084-47ef-a8f9-af4e4b55da4a_1400x482.png 848w, /__u/substackcdn.com/image/fetch/$s_!l4wO!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88560248-a084-47ef-a8f9-af4e4b55da4a_1400x482.png 1272w, /__u/substackcdn.com/image/fetch/$s_!l4wO!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88560248-a084-47ef-a8f9-af4e4b55da4a_1400x482.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!l4wO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88560248-a084-47ef-a8f9-af4e4b55da4a_1400x482.png" width="1400" height="482" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/88560248-a084-47ef-a8f9-af4e4b55da4a_1400x482.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:482,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!l4wO!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88560248-a084-47ef-a8f9-af4e4b55da4a_1400x482.png 424w, /__u/substackcdn.com/image/fetch/$s_!l4wO!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88560248-a084-47ef-a8f9-af4e4b55da4a_1400x482.png 848w, /__u/substackcdn.com/image/fetch/$s_!l4wO!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88560248-a084-47ef-a8f9-af4e4b55da4a_1400x482.png 1272w, /__u/substackcdn.com/image/fetch/$s_!l4wO!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88560248-a084-47ef-a8f9-af4e4b55da4a_1400x482.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>These metrics are still used today, but mostly when the model produces a single, easily comparable &#8220;right&#8221; answer.</p><p>Take classification, for example, where the task is to assign each text a single label. To test this, we can use accuracy by comparing the label assigned by the model to the reference label in the eval dataset to see if it got it right.</p><p>It&#8217;s very clear-cut: if it assigns the wrong label, it gets a 0; if it assigns the correct label, it gets a 1.</p><p>This means if we build a classifier for a spam dataset with 1,000 emails, and the model labels 910 of them correctly, the accuracy would be 0.91.</p><p>For text classification, we often also use F1, precision, and recall.</p><p>When it comes to NLP tasks like summarization and machine translation, people often use ROUGE and BLEU to see how closely the model&#8217;s translation or summary lines up with a reference text.</p><p>Both scores count overlapping n-grams, and while the direction of the comparison is different<strong>,</strong> essentially it just means the more shared word chunks, the higher the score.</p><p>This is pretty simplistic, since if the outputs use different wording, it will score low.</p><p>All of these metrics work best when there&#8217;s a single right answer to a response and are often not the right choice for the LLM applications we build today.</p><h3>LLM benchmarks</h3><p>If you&#8217;ve watched the news, you&#8217;ve probably seen that every time a new version of a large language model gets released, it follows a few benchmarks: MMLU Pro, GPQA, or Big-Bench.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!egRM!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F850b8722-3626-48ea-a876-f41444bcbf10_1400x504.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!egRM!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F850b8722-3626-48ea-a876-f41444bcbf10_1400x504.png 424w, /__u/substackcdn.com/image/fetch/$s_!egRM!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F850b8722-3626-48ea-a876-f41444bcbf10_1400x504.png 848w, /__u/substackcdn.com/image/fetch/$s_!egRM!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F850b8722-3626-48ea-a876-f41444bcbf10_1400x504.png 1272w, /__u/substackcdn.com/image/fetch/$s_!egRM!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F850b8722-3626-48ea-a876-f41444bcbf10_1400x504.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!egRM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F850b8722-3626-48ea-a876-f41444bcbf10_1400x504.png" width="1400" height="504" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/850b8722-3626-48ea-a876-f41444bcbf10_1400x504.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:504,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!egRM!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F850b8722-3626-48ea-a876-f41444bcbf10_1400x504.png 424w, /__u/substackcdn.com/image/fetch/$s_!egRM!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F850b8722-3626-48ea-a876-f41444bcbf10_1400x504.png 848w, /__u/substackcdn.com/image/fetch/$s_!egRM!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F850b8722-3626-48ea-a876-f41444bcbf10_1400x504.png 1272w, /__u/substackcdn.com/image/fetch/$s_!egRM!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F850b8722-3626-48ea-a876-f41444bcbf10_1400x504.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>These are generic evals for which the proper term is more &#8220;benchmark.&#8221;</p><p>Although there&#8217;s a variety of other evaluations done for each model, including for toxicity, hallucination, and bias, the ones that get most of the attention are more like exams or leaderboards.</p><p>Datasets like MMLU are multiple-choice and have been around for quite some time. I&#8217;ve actually skimmed through it before and seen how messy it is.</p><p>Some questions and answers are quite ambiguous, which makes me think that LLM providers will try to train their models on these datasets just to make sure they get them right.</p><p>This creates some fear in the general public that most LLMs are just overfitting when they do well on these benchmarks and why there&#8217;s a need for newer datasets and independent evaluations.</p><h3>LLM scorers</h3><p>To run evaluations on these datasets, you can usually use accuracy and unit tests. However, what&#8217;s different now is the addition of something called LLM-as-a-judge.</p><p>To benchmark the models, teams will mostly use traditional methods.</p><p>So as long as it&#8217;s multiple choice or there&#8217;s just one right answer, there&#8217;s no need for anything else but to compare the answer to the reference for an exact match.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!Sd4R!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26617df9-3c80-4747-af23-d2626df263ca_1400x437.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!Sd4R!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26617df9-3c80-4747-af23-d2626df263ca_1400x437.png 424w, /__u/substackcdn.com/image/fetch/$s_!Sd4R!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26617df9-3c80-4747-af23-d2626df263ca_1400x437.png 848w, /__u/substackcdn.com/image/fetch/$s_!Sd4R!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26617df9-3c80-4747-af23-d2626df263ca_1400x437.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Sd4R!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26617df9-3c80-4747-af23-d2626df263ca_1400x437.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!Sd4R!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26617df9-3c80-4747-af23-d2626df263ca_1400x437.png" width="1400" height="437" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/26617df9-3c80-4747-af23-d2626df263ca_1400x437.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:437,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!Sd4R!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26617df9-3c80-4747-af23-d2626df263ca_1400x437.png 424w, /__u/substackcdn.com/image/fetch/$s_!Sd4R!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26617df9-3c80-4747-af23-d2626df263ca_1400x437.png 848w, /__u/substackcdn.com/image/fetch/$s_!Sd4R!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26617df9-3c80-4747-af23-d2626df263ca_1400x437.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Sd4R!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26617df9-3c80-4747-af23-d2626df263ca_1400x437.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Multiple answer questions we simply parse the responses</figcaption></figure></div><p>This is the case for datasets such as MMLU and GPQA, which have multiple choice answers.</p><p>For the coding tests (HumanEval, SWE-Bench), the grader can simply run the model&#8217;s patch or function. If every test passes, the problem counts as solved, and vice versa.</p><p>However, as you can imagine, if the questions are ambiguous or open-ended, the answers may fluctuate. This gap led to the rise of &#8220;LLM-as-a-judge,&#8221; where a large language model like GPT-4 scores the answers.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!B37o!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce28eb6a-3f2b-404b-8a7a-ee1a982e9177_1400x484.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!B37o!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce28eb6a-3f2b-404b-8a7a-ee1a982e9177_1400x484.png 424w, /__u/substackcdn.com/image/fetch/$s_!B37o!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce28eb6a-3f2b-404b-8a7a-ee1a982e9177_1400x484.png 848w, /__u/substackcdn.com/image/fetch/$s_!B37o!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce28eb6a-3f2b-404b-8a7a-ee1a982e9177_1400x484.png 1272w, /__u/substackcdn.com/image/fetch/$s_!B37o!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce28eb6a-3f2b-404b-8a7a-ee1a982e9177_1400x484.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!B37o!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce28eb6a-3f2b-404b-8a7a-ee1a982e9177_1400x484.png" width="1400" height="484" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ce28eb6a-3f2b-404b-8a7a-ee1a982e9177_1400x484.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:484,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!B37o!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce28eb6a-3f2b-404b-8a7a-ee1a982e9177_1400x484.png 424w, /__u/substackcdn.com/image/fetch/$s_!B37o!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce28eb6a-3f2b-404b-8a7a-ee1a982e9177_1400x484.png 848w, /__u/substackcdn.com/image/fetch/$s_!B37o!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce28eb6a-3f2b-404b-8a7a-ee1a982e9177_1400x484.png 1272w, /__u/substackcdn.com/image/fetch/$s_!B37o!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce28eb6a-3f2b-404b-8a7a-ee1a982e9177_1400x484.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">We let an LLM score an answer with reasoning</figcaption></figure></div><p>MT-Bench is one of the benchmarks that uses LLMs as scorers, as it feeds GPT-4 two competing multi-turn answers and asks which one is better.</p><p>Chatbot Arena, which use human raters, I think now scales up by also incorporating the use of an LLM-as-a-judge.</p><p><em>For transparency, you can also use semantic rulers such as BERTScore to compare for semantic similarity. I&#8217;m glossing over what&#8217;s out there to keep it condensed.</em></p><p>So, teams may still use overlap metrics like BLEU or ROUGE for quick sanity checks, or rely on exact-match parsing when possible, but what&#8217;s new is to have another large language model judge the output.</p><h2>What we do with LLM apps</h2><p>The primary thing that changes now is that we&#8217;re not just testing the LLM itself but the entire system.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!EFLQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43c41529-55df-484e-bb4e-235d5529392f_1244x400.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!EFLQ!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43c41529-55df-484e-bb4e-235d5529392f_1244x400.png 424w, /__u/substackcdn.com/image/fetch/$s_!EFLQ!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43c41529-55df-484e-bb4e-235d5529392f_1244x400.png 848w, /__u/substackcdn.com/image/fetch/$s_!EFLQ!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43c41529-55df-484e-bb4e-235d5529392f_1244x400.png 1272w, /__u/substackcdn.com/image/fetch/$s_!EFLQ!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43c41529-55df-484e-bb4e-235d5529392f_1244x400.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!EFLQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43c41529-55df-484e-bb4e-235d5529392f_1244x400.png" width="1244" height="400" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/43c41529-55df-484e-bb4e-235d5529392f_1244x400.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:400,&quot;width&quot;:1244,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!EFLQ!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43c41529-55df-484e-bb4e-235d5529392f_1244x400.png 424w, /__u/substackcdn.com/image/fetch/$s_!EFLQ!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43c41529-55df-484e-bb4e-235d5529392f_1244x400.png 848w, /__u/substackcdn.com/image/fetch/$s_!EFLQ!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43c41529-55df-484e-bb4e-235d5529392f_1244x400.png 1272w, /__u/substackcdn.com/image/fetch/$s_!EFLQ!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F43c41529-55df-484e-bb4e-235d5529392f_1244x400.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">We&#8217;re evaluating the outputs of an entire system instead of just the LLM</figcaption></figure></div><p>When we can, we still use programmatic methods to evaluate, just like before.</p><p>For more nuanced outputs, we can start with something cheap and deterministic like BLEU or ROUGE to look at n-gram overlap, but most modern frameworks out there will now use LLM scorers to evaluate.</p><p>There are three areas worth talking about: how to evaluate multi-turn conversations, RAG, and agents, in terms of how it&#8217;s done and what kinds of metrics we can turn to.</p><p>You can see below the massive amount of metrics that have already been defined in these three areas.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!XWop!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8d4ca1e-56e7-4133-870e-5f29cfd08fc1_1400x636.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!XWop!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8d4ca1e-56e7-4133-870e-5f29cfd08fc1_1400x636.png 424w, /__u/substackcdn.com/image/fetch/$s_!XWop!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8d4ca1e-56e7-4133-870e-5f29cfd08fc1_1400x636.png 848w, /__u/substackcdn.com/image/fetch/$s_!XWop!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8d4ca1e-56e7-4133-870e-5f29cfd08fc1_1400x636.png 1272w, /__u/substackcdn.com/image/fetch/$s_!XWop!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8d4ca1e-56e7-4133-870e-5f29cfd08fc1_1400x636.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!XWop!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8d4ca1e-56e7-4133-870e-5f29cfd08fc1_1400x636.png" width="1400" height="636" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a8d4ca1e-56e7-4133-870e-5f29cfd08fc1_1400x636.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:636,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!XWop!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8d4ca1e-56e7-4133-870e-5f29cfd08fc1_1400x636.png 424w, /__u/substackcdn.com/image/fetch/$s_!XWop!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8d4ca1e-56e7-4133-870e-5f29cfd08fc1_1400x636.png 848w, /__u/substackcdn.com/image/fetch/$s_!XWop!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8d4ca1e-56e7-4133-870e-5f29cfd08fc1_1400x636.png 1272w, /__u/substackcdn.com/image/fetch/$s_!XWop!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8d4ca1e-56e7-4133-870e-5f29cfd08fc1_1400x636.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>We&#8217;ll talk about all of these briefly before moving on to the different frameworks that help us out.</p><h3>Multi-turn conversations</h3><p>The first part of this is about building evals for multi-turn conversations, the ones we see in chatbots.</p><p>When we interact with chatbots, we want the conversation to feel natural, professional, and for it to remember the right bits. We want it to stay on topic throughout the conversation and actually answer the thing we asked.</p><p>There are quite a few standard metrics people track here. The first we can talk about are <strong>Relevancy/Coherence</strong> and <strong>Completeness</strong>.</p><p><strong>Relevancy</strong> is a metric that should track if the LLM appropriately addresses the user&#8217;s query and stays on topic, whereas Completeness is high if the final outcome actually addresses the user&#8217;s goal.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!LKYw!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3926aa6-2dd9-431b-9645-50cf6777ed02_1400x382.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!LKYw!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3926aa6-2dd9-431b-9645-50cf6777ed02_1400x382.png 424w, /__u/substackcdn.com/image/fetch/$s_!LKYw!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3926aa6-2dd9-431b-9645-50cf6777ed02_1400x382.png 848w, /__u/substackcdn.com/image/fetch/$s_!LKYw!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3926aa6-2dd9-431b-9645-50cf6777ed02_1400x382.png 1272w, /__u/substackcdn.com/image/fetch/$s_!LKYw!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3926aa6-2dd9-431b-9645-50cf6777ed02_1400x382.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!LKYw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3926aa6-2dd9-431b-9645-50cf6777ed02_1400x382.png" width="1400" height="382" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d3926aa6-2dd9-431b-9645-50cf6777ed02_1400x382.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:382,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!LKYw!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3926aa6-2dd9-431b-9645-50cf6777ed02_1400x382.png 424w, /__u/substackcdn.com/image/fetch/$s_!LKYw!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3926aa6-2dd9-431b-9645-50cf6777ed02_1400x382.png 848w, /__u/substackcdn.com/image/fetch/$s_!LKYw!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3926aa6-2dd9-431b-9645-50cf6777ed02_1400x382.png 1272w, /__u/substackcdn.com/image/fetch/$s_!LKYw!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd3926aa6-2dd9-431b-9645-50cf6777ed02_1400x382.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>That is, if we can track satisfaction across the entire conversation, we can also track whether it really does &#8220;reduce support costs&#8221; and increase trust, along with providing high &#8220;self-service rates.&#8221;</p><p>The second part, is <strong>Knowledge Retention</strong> and <strong>Reliability</strong>.</p><p>That is: does it remember key details from the conversation, and can we trust it not to get &#8220;lost&#8221;? It&#8217;s not just enough that it remembers details. It also needs to be able to correct itself.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!xHQj!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f625779-bde1-414a-94d1-7a64d806d570_1400x424.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!xHQj!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f625779-bde1-414a-94d1-7a64d806d570_1400x424.png 424w, /__u/substackcdn.com/image/fetch/$s_!xHQj!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f625779-bde1-414a-94d1-7a64d806d570_1400x424.png 848w, /__u/substackcdn.com/image/fetch/$s_!xHQj!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f625779-bde1-414a-94d1-7a64d806d570_1400x424.png 1272w, /__u/substackcdn.com/image/fetch/$s_!xHQj!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f625779-bde1-414a-94d1-7a64d806d570_1400x424.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!xHQj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f625779-bde1-414a-94d1-7a64d806d570_1400x424.png" width="1400" height="424" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4f625779-bde1-414a-94d1-7a64d806d570_1400x424.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:424,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!xHQj!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f625779-bde1-414a-94d1-7a64d806d570_1400x424.png 424w, /__u/substackcdn.com/image/fetch/$s_!xHQj!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f625779-bde1-414a-94d1-7a64d806d570_1400x424.png 848w, /__u/substackcdn.com/image/fetch/$s_!xHQj!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f625779-bde1-414a-94d1-7a64d806d570_1400x424.png 1272w, /__u/substackcdn.com/image/fetch/$s_!xHQj!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f625779-bde1-414a-94d1-7a64d806d570_1400x424.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This is something we see in vibe coding tools. They forget the mistakes they&#8217;ve made and then keep making them. We should be tracking this as low <strong>Reliability</strong> or Stability.</p><p>The third part we can track is <strong>Role Adherence</strong> and <strong>Prompt Alignment</strong>. This tracks whether the LLM sticks to the role it&#8217;s been given and whether it follows the instructions in the system prompt.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!If7G!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff5e12ce2-408a-4180-8a19-d8575615b1ee_1400x470.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!If7G!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff5e12ce2-408a-4180-8a19-d8575615b1ee_1400x470.png 424w, /__u/substackcdn.com/image/fetch/$s_!If7G!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff5e12ce2-408a-4180-8a19-d8575615b1ee_1400x470.png 848w, /__u/substackcdn.com/image/fetch/$s_!If7G!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff5e12ce2-408a-4180-8a19-d8575615b1ee_1400x470.png 1272w, /__u/substackcdn.com/image/fetch/$s_!If7G!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff5e12ce2-408a-4180-8a19-d8575615b1ee_1400x470.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!If7G!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff5e12ce2-408a-4180-8a19-d8575615b1ee_1400x470.png" width="1400" height="470" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f5e12ce2-408a-4180-8a19-d8575615b1ee_1400x470.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:470,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!If7G!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff5e12ce2-408a-4180-8a19-d8575615b1ee_1400x470.png 424w, /__u/substackcdn.com/image/fetch/$s_!If7G!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff5e12ce2-408a-4180-8a19-d8575615b1ee_1400x470.png 848w, /__u/substackcdn.com/image/fetch/$s_!If7G!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff5e12ce2-408a-4180-8a19-d8575615b1ee_1400x470.png 1272w, /__u/substackcdn.com/image/fetch/$s_!If7G!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff5e12ce2-408a-4180-8a19-d8575615b1ee_1400x470.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Next are metrics around safety, such as <strong>Hallucination</strong> and <strong>Bias/Toxicity</strong>.</p><p><strong>Hallucination</strong> is important to track but also quite difficult. People may try to set up web search to evaluate the output, or they split the output into different claims that are evaluated by a larger model (LLM-as-a-judge style).</p><p>There are also other methods, such as SelfCheckGPT, which checks the model&#8217;s consistency by calling it several times on the same prompt to see if it sticks to its original answer and how many times it diverges.</p><p>For <strong>Bias/Toxicity</strong>, you can use other NLP methods, such as a fine-tuned classifier.</p><p>Other metrics you may want to track could be custom to your application, for example, code correctness, security vulnerabilities, JSON correctness, and so on.</p><p>As for how to do the evaluations, you don&#8217;t always have to use an LLM, although in most of these cases the standard solutions do.</p><p>In cases where we can extract the correct answer, such as parsing JSON, we naturally don&#8217;t need to use an LLM. As I said earlier, many LLM providers also benchmark with unit tests for code-related metrics.</p><p>It goes without saying that LLMs used to judge aren&#8217;t always super reliable, just like the applications they measure, but I don&#8217;t have any numbers for you here, so you&#8217;ll have to hunt for that on your own.</p><h3>Retrieval Augmented Generation (RAG)</h3><p>To continue building on what we can track for multi-turn conversations, we can turn to what we need to measure when using RAG.</p><p>With RAG systems, we need to split the process into two: measuring retrieval and generation metrics separately.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!760V!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe30edd92-c876-4883-908f-f8a72a3ce054_1350x628.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!760V!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe30edd92-c876-4883-908f-f8a72a3ce054_1350x628.png 424w, /__u/substackcdn.com/image/fetch/$s_!760V!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe30edd92-c876-4883-908f-f8a72a3ce054_1350x628.png 848w, /__u/substackcdn.com/image/fetch/$s_!760V!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe30edd92-c876-4883-908f-f8a72a3ce054_1350x628.png 1272w, /__u/substackcdn.com/image/fetch/$s_!760V!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe30edd92-c876-4883-908f-f8a72a3ce054_1350x628.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!760V!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe30edd92-c876-4883-908f-f8a72a3ce054_1350x628.png" width="1350" height="628" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e30edd92-c876-4883-908f-f8a72a3ce054_1350x628.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:628,&quot;width&quot;:1350,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!760V!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe30edd92-c876-4883-908f-f8a72a3ce054_1350x628.png 424w, /__u/substackcdn.com/image/fetch/$s_!760V!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe30edd92-c876-4883-908f-f8a72a3ce054_1350x628.png 848w, /__u/substackcdn.com/image/fetch/$s_!760V!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe30edd92-c876-4883-908f-f8a72a3ce054_1350x628.png 1272w, /__u/substackcdn.com/image/fetch/$s_!760V!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe30edd92-c876-4883-908f-f8a72a3ce054_1350x628.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The first part to measure is retrieval and whether the documents that are fetched are the correct ones for the query.</p><p>If we get low scores on the retrieval side, we can tune the system by setting up better chunking strategies, changing the embedding model, adding techniques such as hybrid search and re-ranking, filtering with metadata, and similar approaches.</p><p>To measure retrieval, we can use older metrics that rely on a curated dataset, or we can use reference-free methods that use an LLM as a judge.</p><p>I need to mention the classic IR metrics first because they were the first on the scene. For these, we need &#8220;gold&#8221; answers, where we set up a query and then rank each document for that particular query.</p><p>Although you can use an LLM to build these datasets, we don&#8217;t use an LLM to measure, since we already have scores in the dataset to compare against.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!_f2E!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11e32cac-6d9d-4d6f-8414-a62b2d871367_1400x363.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!_f2E!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11e32cac-6d9d-4d6f-8414-a62b2d871367_1400x363.png 424w, /__u/substackcdn.com/image/fetch/$s_!_f2E!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11e32cac-6d9d-4d6f-8414-a62b2d871367_1400x363.png 848w, /__u/substackcdn.com/image/fetch/$s_!_f2E!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11e32cac-6d9d-4d6f-8414-a62b2d871367_1400x363.png 1272w, /__u/substackcdn.com/image/fetch/$s_!_f2E!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11e32cac-6d9d-4d6f-8414-a62b2d871367_1400x363.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!_f2E!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11e32cac-6d9d-4d6f-8414-a62b2d871367_1400x363.png" width="1400" height="363" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/11e32cac-6d9d-4d6f-8414-a62b2d871367_1400x363.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:363,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!_f2E!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11e32cac-6d9d-4d6f-8414-a62b2d871367_1400x363.png 424w, /__u/substackcdn.com/image/fetch/$s_!_f2E!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11e32cac-6d9d-4d6f-8414-a62b2d871367_1400x363.png 848w, /__u/substackcdn.com/image/fetch/$s_!_f2E!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11e32cac-6d9d-4d6f-8414-a62b2d871367_1400x363.png 1272w, /__u/substackcdn.com/image/fetch/$s_!_f2E!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F11e32cac-6d9d-4d6f-8414-a62b2d871367_1400x363.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The most well-known IR metrics are Precision@k, Recall@k, and Hit@k.</p><p>These measure the amount of relevant documents fetched, how many relevant documents were retrieved based on the gold reference answers, and whether at least one relevant document made it into the results.</p><p>The newer frameworks such as RAGAS and DeepEval introduce reference-free, LLM-judge style metrics like Context Recall and Context Precision.</p><p>These count how many of the truly relevant chunks made it into the top K list based on the query, using an LLM to judge.</p><p>That is, based on the query, did the system actually return any relevant documents, or are there too many irrelevant ones to answer the question properly?</p><p>To build datasets for evaluating retrieval, you can mine questions from real logs and then use a human to curate them.</p><p>You can also use dataset generators with the help of an LLM, which exist in most frameworks or as standalone tools like YourBench.</p><p>If you were to set up your own dataset generator using an LLM, you could do something like below.</p><pre><code># Prompt to generate questions
qa_generate_prompt_tmpl = &#8220;&#8221;&#8220;\
Context information is below.

---------------------
{context_str}
---------------------

Given the context information and no prior knowledge
generate only {num} questions and {num} answers based on the above context.

...
&#8220;&#8221;&#8220;</code></pre><p>If we turn to the generation part of the RAG system, we are now measuring how well it answers the question using the provided docs.</p><p>If this part isn&#8217;t performing well, we can adjust the prompt, tweak the model settings (temperature, etc.), replace the model entirely, or fine-tune it for domain expertise. We can also force it to &#8220;reason&#8221; using CoT-style loops, check for self-consistency, and so on.</p><p>For this part, RAGAS is useful with its metrics: Answer Relevancy, Faithfulness, and Noise Sensitivity.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!dsAA!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3b279b4-b9f8-4eef-9c57-363e79ae2c06_1400x409.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!dsAA!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3b279b4-b9f8-4eef-9c57-363e79ae2c06_1400x409.png 424w, /__u/substackcdn.com/image/fetch/$s_!dsAA!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3b279b4-b9f8-4eef-9c57-363e79ae2c06_1400x409.png 848w, /__u/substackcdn.com/image/fetch/$s_!dsAA!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3b279b4-b9f8-4eef-9c57-363e79ae2c06_1400x409.png 1272w, /__u/substackcdn.com/image/fetch/$s_!dsAA!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3b279b4-b9f8-4eef-9c57-363e79ae2c06_1400x409.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!dsAA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3b279b4-b9f8-4eef-9c57-363e79ae2c06_1400x409.png" width="1400" height="409" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c3b279b4-b9f8-4eef-9c57-363e79ae2c06_1400x409.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:409,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!dsAA!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3b279b4-b9f8-4eef-9c57-363e79ae2c06_1400x409.png 424w, /__u/substackcdn.com/image/fetch/$s_!dsAA!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3b279b4-b9f8-4eef-9c57-363e79ae2c06_1400x409.png 848w, /__u/substackcdn.com/image/fetch/$s_!dsAA!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3b279b4-b9f8-4eef-9c57-363e79ae2c06_1400x409.png 1272w, /__u/substackcdn.com/image/fetch/$s_!dsAA!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc3b279b4-b9f8-4eef-9c57-363e79ae2c06_1400x409.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>These metrics ask whether the answer actually addresses the user&#8217;s question, whether every claim in the answer is supported by the retrieved docs, and whether a bit of irrelevant context throws the model off course.</p><p>If we look at RAGAS, what they likely do for the first metric is ask the LLM to &#8220;Rate from 0 to 1 how directly this answer addresses the question,&#8221; providing it with the question, answer, and retrieved context. This returns a raw 0&#8211;1 score that can be used to compute averages.</p><p>So, to conclude we split the system into two to evaluate, and although you can use methods that rely on the IR metrics you can also use reference free methods that rely on an LLM to score.</p><p>The last thing we need to cover is how agents are expanding the set of metrics we now need to track, beyond what we&#8217;ve already covered.</p><h3>Agents</h3><p>With agents, we&#8217;re not just looking at the output, the conversation, and the context.</p><p>Now we&#8217;re also evaluating how it &#8220;moves&#8221;: whether it can complete a task or workflow, how effectively it does so, and whether it calls the right tools at the right time.</p><p>Frameworks will call these metrics differently, but essentially the top two you want to track are Task Completion and Tool Correctness.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!jwbS!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ad95199-ac15-4a90-82e1-39d76832763a_1400x547.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!jwbS!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ad95199-ac15-4a90-82e1-39d76832763a_1400x547.png 424w, /__u/substackcdn.com/image/fetch/$s_!jwbS!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ad95199-ac15-4a90-82e1-39d76832763a_1400x547.png 848w, /__u/substackcdn.com/image/fetch/$s_!jwbS!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ad95199-ac15-4a90-82e1-39d76832763a_1400x547.png 1272w, /__u/substackcdn.com/image/fetch/$s_!jwbS!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ad95199-ac15-4a90-82e1-39d76832763a_1400x547.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!jwbS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ad95199-ac15-4a90-82e1-39d76832763a_1400x547.png" width="1400" height="547" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9ad95199-ac15-4a90-82e1-39d76832763a_1400x547.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:547,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!jwbS!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ad95199-ac15-4a90-82e1-39d76832763a_1400x547.png 424w, /__u/substackcdn.com/image/fetch/$s_!jwbS!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ad95199-ac15-4a90-82e1-39d76832763a_1400x547.png 848w, /__u/substackcdn.com/image/fetch/$s_!jwbS!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ad95199-ac15-4a90-82e1-39d76832763a_1400x547.png 1272w, /__u/substackcdn.com/image/fetch/$s_!jwbS!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ad95199-ac15-4a90-82e1-39d76832763a_1400x547.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>For tracking tool usage, we want to know if the correct tool was used for the user&#8217;s query.</p><p>We do need some kind of gold script with ground truth built in to test each run, but you can author that once and then use it each time you make changes.</p><p>For Task Completion, the evaluation is to read the entire trace and the goal, and return a number between 0 and 1 with a rationale. This should measure how effective the agent is at accomplishing the task.</p><p>For agents, you&#8217;ll still need to test other things we&#8217;ve already covered, depending on your application.</p><p><em>I just have to note: even if there are quite a few defined metrics available, your use case will differ, so it&#8217;s worth registering what the common ones are but don&#8217;t assume they are the best ones to track for your app.</em></p><p>Next, let&#8217;s turn to get an overview of the popular frameworks out there that can help you out.</p><h3>Frameworks that help you out</h3><p>There are quite a few frameworks that help you out with evals, but I want to talk about a few popular ones: RAGAS, DeepEval, OpenAI&#8217;s and MLFlow&#8217;s Evals, and break down what they&#8217;re good at and when to use what.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!2_qe!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5eb0e80-139e-4004-96b8-0d38fee9f9d5_1332x696.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!2_qe!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5eb0e80-139e-4004-96b8-0d38fee9f9d5_1332x696.png 424w, /__u/substackcdn.com/image/fetch/$s_!2_qe!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5eb0e80-139e-4004-96b8-0d38fee9f9d5_1332x696.png 848w, /__u/substackcdn.com/image/fetch/$s_!2_qe!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5eb0e80-139e-4004-96b8-0d38fee9f9d5_1332x696.png 1272w, /__u/substackcdn.com/image/fetch/$s_!2_qe!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5eb0e80-139e-4004-96b8-0d38fee9f9d5_1332x696.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!2_qe!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5eb0e80-139e-4004-96b8-0d38fee9f9d5_1332x696.png" width="1332" height="696" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e5eb0e80-139e-4004-96b8-0d38fee9f9d5_1332x696.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:696,&quot;width&quot;:1332,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!2_qe!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5eb0e80-139e-4004-96b8-0d38fee9f9d5_1332x696.png 424w, /__u/substackcdn.com/image/fetch/$s_!2_qe!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5eb0e80-139e-4004-96b8-0d38fee9f9d5_1332x696.png 848w, /__u/substackcdn.com/image/fetch/$s_!2_qe!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5eb0e80-139e-4004-96b8-0d38fee9f9d5_1332x696.png 1272w, /__u/substackcdn.com/image/fetch/$s_!2_qe!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5eb0e80-139e-4004-96b8-0d38fee9f9d5_1332x696.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><em>You can find the full list of different eval frameworks I&#8217;ve found in <a href="https://github.com/ilsilfverskiold/Awesome-LLM-Resources-List/blob/main/README.md#evaluation-frameworks-and-add-ons">this repository</a>.</em></p><p><em>You can also use quite a few framework-specific eval systems, such as LlamaIndex, especially for quick prototyping.</em></p><p>OpenAI and MLFlow&#8217;s Evals are add-ons rather than stand-alone frameworks, whereas RAGAS was primarily built as a metric library for evaluating RAG applications (although they offer other metrics as well).</p><p>DeepEval is possibly the most comprehensive evaluation library out of all of them.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!WTsK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4144572-fcb6-4af6-acee-1285e0f94f9d_1400x442.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!WTsK!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4144572-fcb6-4af6-acee-1285e0f94f9d_1400x442.png 424w, /__u/substackcdn.com/image/fetch/$s_!WTsK!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4144572-fcb6-4af6-acee-1285e0f94f9d_1400x442.png 848w, /__u/substackcdn.com/image/fetch/$s_!WTsK!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4144572-fcb6-4af6-acee-1285e0f94f9d_1400x442.png 1272w, /__u/substackcdn.com/image/fetch/$s_!WTsK!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4144572-fcb6-4af6-acee-1285e0f94f9d_1400x442.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!WTsK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4144572-fcb6-4af6-acee-1285e0f94f9d_1400x442.png" width="1400" height="442" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d4144572-fcb6-4af6-acee-1285e0f94f9d_1400x442.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:442,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!WTsK!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4144572-fcb6-4af6-acee-1285e0f94f9d_1400x442.png 424w, /__u/substackcdn.com/image/fetch/$s_!WTsK!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4144572-fcb6-4af6-acee-1285e0f94f9d_1400x442.png 848w, /__u/substackcdn.com/image/fetch/$s_!WTsK!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4144572-fcb6-4af6-acee-1285e0f94f9d_1400x442.png 1272w, /__u/substackcdn.com/image/fetch/$s_!WTsK!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4144572-fcb6-4af6-acee-1285e0f94f9d_1400x442.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>However, it&#8217;s important to mention that they all offer the ability to run evals on your own dataset, work for multi-turn, RAG, and agents in some way or another, support LLM-as-a-judge, allow setting up custom metrics, and are CI-friendly.</p><p>They differ, as mentioned, in how comprehensive they are.</p><p>MLFlow was primarily built to evaluate traditional ML pipelines, so the number of metrics they offer is lower for LLM-based apps. OpenAI is a very lightweight solution that expects you to set up your own metrics, although they provide an example library to help you get started.</p><p>RAGAS provides quite a few metrics and integrates with LangChain so you can run them easily.</p><p>DeepEval offers a lot out of the box, including the RAGAS metrics.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!sAFM!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0da5eb02-679d-4f6f-850c-715043d23d18_1400x664.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!sAFM!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0da5eb02-679d-4f6f-850c-715043d23d18_1400x664.png 424w, /__u/substackcdn.com/image/fetch/$s_!sAFM!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0da5eb02-679d-4f6f-850c-715043d23d18_1400x664.png 848w, /__u/substackcdn.com/image/fetch/$s_!sAFM!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0da5eb02-679d-4f6f-850c-715043d23d18_1400x664.png 1272w, /__u/substackcdn.com/image/fetch/$s_!sAFM!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0da5eb02-679d-4f6f-850c-715043d23d18_1400x664.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!sAFM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0da5eb02-679d-4f6f-850c-715043d23d18_1400x664.png" width="1400" height="664" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0da5eb02-679d-4f6f-850c-715043d23d18_1400x664.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:664,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!sAFM!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0da5eb02-679d-4f6f-850c-715043d23d18_1400x664.png 424w, /__u/substackcdn.com/image/fetch/$s_!sAFM!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0da5eb02-679d-4f6f-850c-715043d23d18_1400x664.png 848w, /__u/substackcdn.com/image/fetch/$s_!sAFM!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0da5eb02-679d-4f6f-850c-715043d23d18_1400x664.png 1272w, /__u/substackcdn.com/image/fetch/$s_!sAFM!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0da5eb02-679d-4f6f-850c-715043d23d18_1400x664.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Comparison between frameworks, see the Github repo <a href="https://github.com/ilsilfverskiold/Awesome-LLM-Resources-List/blob/main/README.md#evaluation-frameworks-and-add-ons">here</a> | Image by author</p><p>If we look at the metrics being offered, we can get a sense of how extensive these solutions are.</p><p>It&#8217;s worth noting that the ones offering metrics don&#8217;t always follow a standard in naming. They may mean the same thing but call it something different.</p><p>For example, faithfulness in one may mean the same as groundedness in another. Answer relevancy may be the same as response relevance, and so on.</p><p>This creates a lot of unnecessary confusion and complexity around evaluating systems in general.</p><p>Nevertheless, DeepEval stands out with over 40 metrics available and also offers a framework called G-Eval, which helps you set up custom metrics quickly making it the fastest way from idea to a runnable metric.</p><p>OpenAI&#8217;s Evals framework is better suited when you want bespoke logic, not when you just need a quick judge.</p><p>According to the DeepEval team, custom metrics are what developers set up the most, so don&#8217;t get stuck on who offers what metric. Your use case will be unique, and so will how you evaluate it.</p><p>So, <strong>which should you use for what situation?</strong></p><p>Use RAGAS when you need specialized metrics for RAG pipelines with minimal setup. Pick DeepEval when you want a complete, out-of-the-box eval suite.</p><p>MLFlow is a good choice if you&#8217;re already invested in MLFlow or prefer built-in tracking and UI features. OpenAI&#8217;s Evals framework is the most barebones, so it&#8217;s best if you&#8217;re tied into OpenAI infrastructure and want flexibility.</p><p>Lastly, DeepEval also provides red teaming via their DeepTeam framework, which automates adversarial testing of LLM systems. There are other frameworks out there that do this too, although perhaps not as extensively.</p><p>I&#8217;ll have to do something on adversarial testing of LLM systems and prompt injections in the future. It&#8217;s an interesting topic.</p><p>The dataset business is lucrative business which is why it&#8217;s great that we&#8217;re now at this point where we can use other LLMs to annotate data, or score tests.</p><p>However, LLM judges aren&#8217;t magic and the evals you&#8217;ll set up you&#8217;ll probably find a bit flaky, just as with any other LLM application you build. According to the world wide web, most teams and companies sample-audit with humans every few weeks to stay real.</p><p>The metrics you set up for your app will likely be custom, so even though I&#8217;ve now put you through hearing about quite many you&#8217;ll probably build something on your own.</p><p>It&#8217;s good to know what the standard ones are though.</p><p>Hopefully it proved educational anyhow.</p><p>If you liked this one, be sure to read some of my other articles on agentic frameworks or building advanced RAG agents.</p><p>&#10084;</p>]]></content:encoded></item><item><title><![CDATA[Predicting Tech Trends with Natural Language Processing]]></title><description><![CDATA[Using small, fine-tuned models]]></description><link>https://howtouseai.substack.com/p/predicting-tech-trends-with-natural</link><guid isPermaLink="false">https://howtouseai.substack.com/p/predicting-tech-trends-with-natural</guid><dc:creator><![CDATA[Ida Silfverskiold]]></dc:creator><pubDate>Tue, 01 Sep 2026 08:25:00 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!spU8!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F429a12dc-052b-4931-8a7d-c10d4c34a517_1382x694.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!spU8!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F429a12dc-052b-4931-8a7d-c10d4c34a517_1382x694.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!spU8!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F429a12dc-052b-4931-8a7d-c10d4c34a517_1382x694.png 424w, /__u/substackcdn.com/image/fetch/$s_!spU8!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F429a12dc-052b-4931-8a7d-c10d4c34a517_1382x694.png 848w, /__u/substackcdn.com/image/fetch/$s_!spU8!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F429a12dc-052b-4931-8a7d-c10d4c34a517_1382x694.png 1272w, /__u/substackcdn.com/image/fetch/$s_!spU8!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F429a12dc-052b-4931-8a7d-c10d4c34a517_1382x694.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!spU8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F429a12dc-052b-4931-8a7d-c10d4c34a517_1382x694.png" width="1382" height="694" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/429a12dc-052b-4931-8a7d-c10d4c34a517_1382x694.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:694,&quot;width&quot;:1382,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!spU8!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F429a12dc-052b-4931-8a7d-c10d4c34a517_1382x694.png 424w, /__u/substackcdn.com/image/fetch/$s_!spU8!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F429a12dc-052b-4931-8a7d-c10d4c34a517_1382x694.png 848w, /__u/substackcdn.com/image/fetch/$s_!spU8!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F429a12dc-052b-4931-8a7d-c10d4c34a517_1382x694.png 1272w, /__u/substackcdn.com/image/fetch/$s_!spU8!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F429a12dc-052b-4931-8a7d-c10d4c34a517_1382x694.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Using small models to derive insights and build applications | Image by the author</figcaption></figure></div><p><em>This is an article dated February 2024 but has been re-published here.</em></p><p>Looking at the image at the top, you can at least figure out that I&#8217;ll be talking about using these smaller, fine-tuned NLP models in an application.</p><p>My last article was about fine-tuning smaller natural language models for specific use cases. Specifically, I talked about fine-tuning a BART model for keyword extraction. I also <a href="https://huggingface.co/ilsilfverskiold/tech-keywords-extractor">open-sourced</a> it for anyone who wanted to use it.</p><p>For this piece, though, I will go into more <strong>application building</strong> with these smaller NLP models. The models I have used here are two models that were fine-tuned by me, and one was obtained from the Hugging Face hub.</p><p>When I show people the finished application, they find that it&#8217;s a bit of an odd concept like some kind of Google Trends for the tech space coupled with search.</p><p>The idea though is that it will crawl certain tech websites and then using NLPs it will breakdown the crawl-able content to analyze sentiment, extract keywords, organize content by category.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!11Mk!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fbdf333-a0f5-462a-9413-db5cf86083fc_1400x669.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!11Mk!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fbdf333-a0f5-462a-9413-db5cf86083fc_1400x669.png 424w, /__u/substackcdn.com/image/fetch/$s_!11Mk!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fbdf333-a0f5-462a-9413-db5cf86083fc_1400x669.png 848w, /__u/substackcdn.com/image/fetch/$s_!11Mk!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fbdf333-a0f5-462a-9413-db5cf86083fc_1400x669.png 1272w, /__u/substackcdn.com/image/fetch/$s_!11Mk!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fbdf333-a0f5-462a-9413-db5cf86083fc_1400x669.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!11Mk!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fbdf333-a0f5-462a-9413-db5cf86083fc_1400x669.png" width="1400" height="669" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3fbdf333-a0f5-462a-9413-db5cf86083fc_1400x669.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:669,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!11Mk!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fbdf333-a0f5-462a-9413-db5cf86083fc_1400x669.png 424w, /__u/substackcdn.com/image/fetch/$s_!11Mk!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fbdf333-a0f5-462a-9413-db5cf86083fc_1400x669.png 848w, /__u/substackcdn.com/image/fetch/$s_!11Mk!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fbdf333-a0f5-462a-9413-db5cf86083fc_1400x669.png 1272w, /__u/substackcdn.com/image/fetch/$s_!11Mk!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fbdf333-a0f5-462a-9413-db5cf86083fc_1400x669.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Example process for processing three titles using three different NLP models | Image by author</p><p>The end results will allow you to see what keywords are &#8216;trending&#8217; and in what capacity for each category, or as a whole.</p><p>To demonstrate an example, check the graph below using the category <strong>Tools &amp; Services.</strong></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!7iq7!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F730a3dc8-0d4c-4860-8fb9-85964bc2b3ba_1400x679.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!7iq7!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F730a3dc8-0d4c-4860-8fb9-85964bc2b3ba_1400x679.png 424w, /__u/substackcdn.com/image/fetch/$s_!7iq7!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F730a3dc8-0d4c-4860-8fb9-85964bc2b3ba_1400x679.png 848w, /__u/substackcdn.com/image/fetch/$s_!7iq7!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F730a3dc8-0d4c-4860-8fb9-85964bc2b3ba_1400x679.png 1272w, /__u/substackcdn.com/image/fetch/$s_!7iq7!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F730a3dc8-0d4c-4860-8fb9-85964bc2b3ba_1400x679.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!7iq7!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F730a3dc8-0d4c-4860-8fb9-85964bc2b3ba_1400x679.png" width="1400" height="679" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/730a3dc8-0d4c-4860-8fb9-85964bc2b3ba_1400x679.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:679,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!7iq7!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F730a3dc8-0d4c-4860-8fb9-85964bc2b3ba_1400x679.png 424w, /__u/substackcdn.com/image/fetch/$s_!7iq7!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F730a3dc8-0d4c-4860-8fb9-85964bc2b3ba_1400x679.png 848w, /__u/substackcdn.com/image/fetch/$s_!7iq7!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F730a3dc8-0d4c-4860-8fb9-85964bc2b3ba_1400x679.png 1272w, /__u/substackcdn.com/image/fetch/$s_!7iq7!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F730a3dc8-0d4c-4860-8fb9-85964bc2b3ba_1400x679.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Graphing keywords per category to demonstrate the application | Image by author</p><p>As you see from above we can deduce that <strong>Copilot</strong> is having a bad week based on the increase in mentions along with a majority of negative sentiment.</p><p>It will then allow you to get sources for the keywords you find interesting, so you can navigate to each source or analyze the content altogether. So in this case, it would be interesting to check out the urls where people are talking about Copilot and to read what they have to say there.</p><p>The application is primarily an API that I&#8217;m continuously building &#8212; so it is by no means done yet &#8212; but I have been playing with a frontend for it as well. I call it<a href="https://www.safron.io/"> Safron.</a></p><p>The frontend gives you a fun way to click on a keyword within a category to see what has been said about it from the previous day.</p><p>You can go <a href="https://www.safron.io/">test</a> it out; it works well as is for now and updates data daily.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!yXdt!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce597bfd-4f55-44bc-869b-bcc2ca62c0bd_800x386.gif" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!yXdt!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce597bfd-4f55-44bc-869b-bcc2ca62c0bd_800x386.gif 424w, /__u/substackcdn.com/image/fetch/$s_!yXdt!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce597bfd-4f55-44bc-869b-bcc2ca62c0bd_800x386.gif 848w, /__u/substackcdn.com/image/fetch/$s_!yXdt!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce597bfd-4f55-44bc-869b-bcc2ca62c0bd_800x386.gif 1272w, /__u/substackcdn.com/image/fetch/$s_!yXdt!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce597bfd-4f55-44bc-869b-bcc2ca62c0bd_800x386.gif 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!yXdt!,w_1456,c_limit,f_auto,q_auto:good,fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce597bfd-4f55-44bc-869b-bcc2ca62c0bd_800x386.gif" width="800" height="386" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ce597bfd-4f55-44bc-869b-bcc2ca62c0bd_800x386.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:386,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!yXdt!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce597bfd-4f55-44bc-869b-bcc2ca62c0bd_800x386.gif 424w, /__u/substackcdn.com/image/fetch/$s_!yXdt!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce597bfd-4f55-44bc-869b-bcc2ca62c0bd_800x386.gif 848w, /__u/substackcdn.com/image/fetch/$s_!yXdt!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce597bfd-4f55-44bc-869b-bcc2ca62c0bd_800x386.gif 1272w, /__u/substackcdn.com/image/fetch/$s_!yXdt!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fce597bfd-4f55-44bc-869b-bcc2ca62c0bd_800x386.gif 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Clicking on the keyword &#8216;FTX&#8217; that was trending gives you a summary and sources | <a href="http://safron.io">safron.io</a></p><p>If you decide to play around with the application, decide on your category and then click on the keyword to get a summary from AI about what is going on. You&#8217;ll be able to see the sources it is using as well. If it is in blue, the system says it is &#8216;trending.&#8217;</p><p>If you want to play around with the API instead &#8212; to build news bots &#8212; see <a href="https://medium.com/gitconnected/build-a-personal-ai-tech-news-agent-94e7a2e508fe">this</a> article. You can fetch trending and top keywords for up to one week back in time. It always updates in the mornings for the previous day.</p><p>It would be possible to have it process in real-time, but it would also be costly to keep the NLP models running 24/7. I pay very little to keep the system running as it is now.</p><p>How is it possible to build something like this, where these NLP models will do the brunt of the work for me? I&#8217;ll go through a brief introduction on the application itself, and then go into the technical bits.</p><p>It&#8217;s not groundbreaking stuff but interesting if you&#8217;re looking for inspiration and if you&#8217;re building something similar.</p><h2>Introduction</h2><p>Was it hard to build? Is it expensive to maintain? Why did I build it?</p><p>It wasn&#8217;t easy to build, mostly because there is so much data you need to clean and maintain. I do though believe that advancements in ML have made it possible to accomplish something like this faster than before.</p><p>It&#8217;s interesting to analyze public opinion and behavior, and since I work within tech, it made sense to build it.</p><p>When you build something you find interesting, you tend to improve at it, and then you think of new, innovative ways to continue working with it. So, in a way, sometimes you don&#8217;t need to have a clear goal because the process itself will produce something worthwhile.</p><h3><strong>Is it expensive to build and run?</strong></h3><p>The main reason for fine-tuning these smaller open-source models was to keep costs low. I went into some detail regarding costs using LLMs versus SLMs in my last piece. The total cost hasn&#8217;t gone over $80 so far, if I exclude the purchase of the domain name.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!d_Qa!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcafd43bc-cf32-47ba-92c8-70fabd7a5eba_1350x602.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!d_Qa!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcafd43bc-cf32-47ba-92c8-70fabd7a5eba_1350x602.png 424w, /__u/substackcdn.com/image/fetch/$s_!d_Qa!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcafd43bc-cf32-47ba-92c8-70fabd7a5eba_1350x602.png 848w, /__u/substackcdn.com/image/fetch/$s_!d_Qa!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcafd43bc-cf32-47ba-92c8-70fabd7a5eba_1350x602.png 1272w, /__u/substackcdn.com/image/fetch/$s_!d_Qa!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcafd43bc-cf32-47ba-92c8-70fabd7a5eba_1350x602.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!d_Qa!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcafd43bc-cf32-47ba-92c8-70fabd7a5eba_1350x602.png" width="1350" height="602" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/cafd43bc-cf32-47ba-92c8-70fabd7a5eba_1350x602.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:602,&quot;width&quot;:1350,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!d_Qa!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcafd43bc-cf32-47ba-92c8-70fabd7a5eba_1350x602.png 424w, /__u/substackcdn.com/image/fetch/$s_!d_Qa!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcafd43bc-cf32-47ba-92c8-70fabd7a5eba_1350x602.png 848w, /__u/substackcdn.com/image/fetch/$s_!d_Qa!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcafd43bc-cf32-47ba-92c8-70fabd7a5eba_1350x602.png 1272w, /__u/substackcdn.com/image/fetch/$s_!d_Qa!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcafd43bc-cf32-47ba-92c8-70fabd7a5eba_1350x602.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This price includes data extraction, database storage, hosting, fine-tuning the open-source NLP models, API, and cloud hosting. Now, if traffic increases, that&#8217;s a separate problem.</p><p>The biggest cost was using GPT-4 Turbo to help process the initial datasets for the smaller models. But there are open source options you can look into.</p><p>The question then becomes, how expensive would something like this have been if I had used a larger language model? Calling an API up to 15,000 times per day or hosting a very large model would have been very costly.</p><p>I did the maths on this in another article, where I looked at the cost of calling an API for GPT-4 and Claude as comparison using zero-shot with a hefty prompt template. You can read more on this <a href="https://medium.com/gitconnected/fine-tune-smaller-nlp-models-with-hugging-face-for-specific-use-cases-1745813471dc">here</a>.</p><h3><strong>So, why did I build it?</strong></h3><p>You can achieve a lot with structured data like this.</p><p>Just attaching yourself to keywords and categories for product and community research is one approach, but also being able to use the API to feed into AI agents can keep you updated without having to sift through a lot of texts on a daily basis.</p><p>A clear application of this is to see which AI models are being discussed on a day to day basis, and to what extent. That is, how often do we hear that a new model is being introduced and it&#8217;s groundbreaking? By filtering the data with the category AI models, you can identify the top mentioned AI models during the week and which ones have an unusual count.</p><p>You can then tap into the sources on what people are saying, having an LLM summarizing for you.</p><p>You can see how this can be done below.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!lTIq!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f05c458-0374-4fe7-ab49-ba3ccefcc865_1400x595.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!lTIq!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f05c458-0374-4fe7-ab49-ba3ccefcc865_1400x595.png 424w, /__u/substackcdn.com/image/fetch/$s_!lTIq!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f05c458-0374-4fe7-ab49-ba3ccefcc865_1400x595.png 848w, /__u/substackcdn.com/image/fetch/$s_!lTIq!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f05c458-0374-4fe7-ab49-ba3ccefcc865_1400x595.png 1272w, /__u/substackcdn.com/image/fetch/$s_!lTIq!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f05c458-0374-4fe7-ab49-ba3ccefcc865_1400x595.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!lTIq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f05c458-0374-4fe7-ab49-ba3ccefcc865_1400x595.png" width="1400" height="595" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7f05c458-0374-4fe7-ab49-ba3ccefcc865_1400x595.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:595,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!lTIq!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f05c458-0374-4fe7-ab49-ba3ccefcc865_1400x595.png 424w, /__u/substackcdn.com/image/fetch/$s_!lTIq!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f05c458-0374-4fe7-ab49-ba3ccefcc865_1400x595.png 848w, /__u/substackcdn.com/image/fetch/$s_!lTIq!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f05c458-0374-4fe7-ab49-ba3ccefcc865_1400x595.png 1272w, /__u/substackcdn.com/image/fetch/$s_!lTIq!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7f05c458-0374-4fe7-ab49-ba3ccefcc865_1400x595.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Sorting weekly data based on AI Models as Category (Note: this is an outdated UI)</p><p>You&#8217;ll see Mistral and Code Llama were mentioned significantly more than usual in the graph above &#8212; this was some week in January 2024. Mistral kept gaining ground, but that&#8217;s not always the case.</p><p>If mentions and engagement sharply increases from one period to another it will show up as trending by the API.</p><p>The incredible market share that ChatGPT holds, however, is amazing in comparison to these other models. It makes you think about how much ground it has already covered.</p><p>To check it out yourself, filter with the <a href="https://www.safron.io/">Category</a> for AI models &amp; assistants. You can filter by 11 categories such as companies, websites, tools, and platforms.</p><blockquote><p>The UI has changed since this piece was written but you can still sort the keywords based on category and the click on the keyword to see the sources.</p></blockquote><p>I&#8217;ll be honest, the model that does this categorization for me was pretty hard to create. I had to settle for a hybrid approach at first that I later changed to a model I built that understood context better. Today, it runs on its own but it took quite awhile to build the dataset for it.</p><p>Another useful application I discovered is using AI agents with the API to summarize the issues people are experiencing with certain keywords. This is great for generating regular reports but also for product research.</p><p>See an example using data for last week &#8212; in January 2024 &#8212; for the word &#8220;Copilot&#8221; with GPT-4.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!fTuq!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F477c1f55-f613-468d-b265-892d7d0ecdb8_1399x699.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!fTuq!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F477c1f55-f613-468d-b265-892d7d0ecdb8_1399x699.png 424w, /__u/substackcdn.com/image/fetch/$s_!fTuq!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F477c1f55-f613-468d-b265-892d7d0ecdb8_1399x699.png 848w, /__u/substackcdn.com/image/fetch/$s_!fTuq!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F477c1f55-f613-468d-b265-892d7d0ecdb8_1399x699.png 1272w, /__u/substackcdn.com/image/fetch/$s_!fTuq!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F477c1f55-f613-468d-b265-892d7d0ecdb8_1399x699.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!fTuq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F477c1f55-f613-468d-b265-892d7d0ecdb8_1399x699.png" width="1399" height="699" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/477c1f55-f613-468d-b265-892d7d0ecdb8_1399x699.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:699,&quot;width&quot;:1399,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!fTuq!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F477c1f55-f613-468d-b265-892d7d0ecdb8_1399x699.png 424w, /__u/substackcdn.com/image/fetch/$s_!fTuq!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F477c1f55-f613-468d-b265-892d7d0ecdb8_1399x699.png 848w, /__u/substackcdn.com/image/fetch/$s_!fTuq!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F477c1f55-f613-468d-b265-892d7d0ecdb8_1399x699.png 1272w, /__u/substackcdn.com/image/fetch/$s_!fTuq!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F477c1f55-f613-468d-b265-892d7d0ecdb8_1399x699.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Or a report for the word &#8220;Unity&#8221; that ChatGPT has summarized.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!YsQb!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52e19eb6-8978-4772-9ae0-61d86a6c94e9_1302x1033.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!YsQb!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52e19eb6-8978-4772-9ae0-61d86a6c94e9_1302x1033.png 424w, /__u/substackcdn.com/image/fetch/$s_!YsQb!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52e19eb6-8978-4772-9ae0-61d86a6c94e9_1302x1033.png 848w, /__u/substackcdn.com/image/fetch/$s_!YsQb!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52e19eb6-8978-4772-9ae0-61d86a6c94e9_1302x1033.png 1272w, /__u/substackcdn.com/image/fetch/$s_!YsQb!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52e19eb6-8978-4772-9ae0-61d86a6c94e9_1302x1033.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!YsQb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52e19eb6-8978-4772-9ae0-61d86a6c94e9_1302x1033.png" width="1302" height="1033" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/52e19eb6-8978-4772-9ae0-61d86a6c94e9_1302x1033.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1033,&quot;width&quot;:1302,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!YsQb!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52e19eb6-8978-4772-9ae0-61d86a6c94e9_1302x1033.png 424w, /__u/substackcdn.com/image/fetch/$s_!YsQb!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52e19eb6-8978-4772-9ae0-61d86a6c94e9_1302x1033.png 848w, /__u/substackcdn.com/image/fetch/$s_!YsQb!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52e19eb6-8978-4772-9ae0-61d86a6c94e9_1302x1033.png 1272w, /__u/substackcdn.com/image/fetch/$s_!YsQb!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F52e19eb6-8978-4772-9ae0-61d86a6c94e9_1302x1033.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>It would be straightforward enough to tap into these keywords by polling the API daily, let an LLM process it, and then have the reports sent to your email or your preferred medium.</p><p>This is what I mean by working with structured data. It can help the LLM provide you with accurate reports.</p><p>Going through this data, I&#8217;ve observed that there is a vast number of tools that you never hear about. If you follow the trends, you&#8217;ll notice that most people talk about the same tools, companies, and individuals on a daily basis.</p><p>It&#8217;s quite rare for something to immediately hit the mainstream track after being introduced.</p><p>The key will be, once the system has accumulated months of data, to begin to use this data to create better ML processes to understand how and when something is gaining traction.</p><h2>Technical Process</h2><p>I won&#8217;t delve into extensive detail about web crawling or the architecture behind building something like this, there should be a ton of content on this already.</p><p>Nevertheless, I store long-term data in BigQuery and host cached data in MongoDB. To construct the front-facing API, it made sense to create temporary collections in MongoDB using more complex SQL queries in BigQuery on a daily basis through automated scripts. This approach allows me to keep BigQuery query costs down to zero.</p><p>I do pay for a MongoDB dedicated cluster, which is a temporary solution as I aim to keep costs extremely low. This setup is feasible, but in this scenario, it would be beneficial to cache the table data on the frontend to reduce the number of requests. Something I decided to skip for now because I have a bit of spaghetti code going on.</p><p>The point I wanted to make with this application was to demonstrate it could be done at virtually no cost. I think I&#8217;ve managed to convey that message.</p><p>Let&#8217;s dig into the parts where I&#8217;ve used natural language processing though.</p><h3>Natural Language Processing</h3><p>For everyone new to natural language processing, transformer models were introduced in 2017. Transformer models understood the nuances of natural language much better than previous models.</p><p>With Google&#8217;s release of BERT in 2018, the practice of model fine-tuning became more common. BERT and other base models introduced the concept that you could take a pre-trained model and fine-tune it on a smaller dataset for specific tasks, achieving good results.</p><p>Platforms like <a href="https://huggingface.co/">HuggingFace</a> have made it really easy to work with advanced NLP, allowing anyone to engage in the work I&#8217;m doing here and to apply NLP for various use cases.</p><p>One of the models I&#8217;m using someone else has built, the others I fine-tuned myself. If you&#8217;re keen to read about how to fine-tune these models, check out <a href="https://medium.com/gitconnected/fine-tune-smaller-nlp-models-with-hugging-face-for-specific-use-cases-1745813471dc">this</a> article or <a href="https://medium.com/towards-data-science/fine-tune-smaller-transformer-models-text-classification-77cbbd3bf02b">this</a>.</p><h3>Sentiment, Keyword Extraction &amp; Categorization</h3><p>Under the hood, Safron is powered by three NLP models.</p><p>I wanted to build six models, but to maintain my sanity, I&#8217;ve stuck with using three in total. The first model I&#8217;m using is, unsurprisingly, the <a href="https://huggingface.co/ilsilfverskiold/tech-keywords-extractor">tech keyword extractor</a> that I built some time ago.</p><p>I&#8217;ve detailed how I built this in a previous <a href="https://medium.com/gitconnected/fine-tune-smaller-nlp-models-with-hugging-face-for-specific-use-cases-1745813471dc">article</a>. This model is important as it has the ability to identify important words automatically, which would have been very messy otherwise.</p><p>Keyword extraction is one of those things people think is very straightforward. Just think about how you would build a system to recognize what is a company, what is a person and what is a tool without the system understanding natural language.</p><p>After fine-tuning this model, I went back to fine-tune it again with subpar results. It makes me wonder if, in the future, there will be jobs only dedicated to sorting datasets for language models. It&#8217;s tough work and you do tend to give up pretty easily. GPT-4 can provide some help, but it&#8217;s not consistent enough without the use of human supervision.</p><p>The second model I used was the <a href="https://huggingface.co/cardiffnlp/twitter-roberta-base-sentiment-latest">sentiment analyzer</a>. I used someone else&#8217;s model here. This model can categorize a text as neutral, positive, or negative, assigning it a specific score.</p><p>Having used this for some time now, it&#8217;s rare to encounter many positive texts online; they are usually negative or neutral, especially within the tech sector. If I rebuild it, I will build it to understand context better for the exact keyword and perhaps with emotions rather than sentiment.</p><p>So, how do I use these models? The idea is to take the collected crawled data that had been previously processed and stored, and then applying these two models to the texts on a daily basis in batches.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!R_qK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc60a196-a551-42cd-9413-a2dab6f1df3c_1202x706.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!R_qK!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc60a196-a551-42cd-9413-a2dab6f1df3c_1202x706.png 424w, /__u/substackcdn.com/image/fetch/$s_!R_qK!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc60a196-a551-42cd-9413-a2dab6f1df3c_1202x706.png 848w, /__u/substackcdn.com/image/fetch/$s_!R_qK!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc60a196-a551-42cd-9413-a2dab6f1df3c_1202x706.png 1272w, /__u/substackcdn.com/image/fetch/$s_!R_qK!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc60a196-a551-42cd-9413-a2dab6f1df3c_1202x706.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!R_qK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc60a196-a551-42cd-9413-a2dab6f1df3c_1202x706.png" width="1202" height="706" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/dc60a196-a551-42cd-9413-a2dab6f1df3c_1202x706.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:706,&quot;width&quot;:1202,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!R_qK!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc60a196-a551-42cd-9413-a2dab6f1df3c_1202x706.png 424w, /__u/substackcdn.com/image/fetch/$s_!R_qK!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc60a196-a551-42cd-9413-a2dab6f1df3c_1202x706.png 848w, /__u/substackcdn.com/image/fetch/$s_!R_qK!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc60a196-a551-42cd-9413-a2dab6f1df3c_1202x706.png 1272w, /__u/substackcdn.com/image/fetch/$s_!R_qK!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc60a196-a551-42cd-9413-a2dab6f1df3c_1202x706.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Crawlable titles gets processed with NLPs to break it down into keywords</p><p>These keywords are stored and then linked back to their original content in another data table. I collect all the data, aggregate it, and send it to MongoDB, making it easily accessible from there.</p><p>It&#8217;s important to note that since the application applies sentiment analysis to texts as a whole, the analysis of the keywords is contextual. However, if a keyword consistently shows a high amount of negative sentiment, it usually indicates negative public opinion.</p><p>Tesla is one of those companies that have had a rough time lately.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!3isq!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa538d306-ab96-49e6-b270-517e7a637483_1373x1237.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!3isq!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa538d306-ab96-49e6-b270-517e7a637483_1373x1237.png 424w, /__u/substackcdn.com/image/fetch/$s_!3isq!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa538d306-ab96-49e6-b270-517e7a637483_1373x1237.png 848w, /__u/substackcdn.com/image/fetch/$s_!3isq!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa538d306-ab96-49e6-b270-517e7a637483_1373x1237.png 1272w, /__u/substackcdn.com/image/fetch/$s_!3isq!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa538d306-ab96-49e6-b270-517e7a637483_1373x1237.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!3isq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa538d306-ab96-49e6-b270-517e7a637483_1373x1237.png" width="1373" height="1237" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a538d306-ab96-49e6-b270-517e7a637483_1373x1237.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1237,&quot;width&quot;:1373,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!3isq!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa538d306-ab96-49e6-b270-517e7a637483_1373x1237.png 424w, /__u/substackcdn.com/image/fetch/$s_!3isq!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa538d306-ab96-49e6-b270-517e7a637483_1373x1237.png 848w, /__u/substackcdn.com/image/fetch/$s_!3isq!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa538d306-ab96-49e6-b270-517e7a637483_1373x1237.png 1272w, /__u/substackcdn.com/image/fetch/$s_!3isq!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa538d306-ab96-49e6-b270-517e7a637483_1373x1237.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>ChatGPT summary of data for keyword &#8216;Tesla&#8217; picked up by Safron&#8212; somewhere in January 2024 | Image by author</p><p>I would have attached an LLM to the application so instead of giving you the sources it gives you a nice summary.</p><blockquote><p>This is something you can do now, via safron.io, as I utilize a very cheap LLM to do the last part of summarizing the texts per keyword.</p></blockquote><p>Let&#8217;s get back on track though, I wasn&#8217;t quite done with extracting keywords and sentiment but I wanted to be able to categorize the words that were coming in. Having thousands of keywords in front of me on a day to day basis was too difficult to analyze by just looking at it, so I embarked on fine-tuning an encoder model for classification.</p><p>I opted for a larger <a href="https://huggingface.co/FacebookAI/roberta-large">RoBERTa</a> model for this task because I needed it to be trained on as much data as possible. The goal was for it to identify each keyword and then assign a category from 12 different labels.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!8dFk!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65e9d79c-7dda-4dc5-85f2-1c5360c5e2d9_1400x729.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!8dFk!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65e9d79c-7dda-4dc5-85f2-1c5360c5e2d9_1400x729.png 424w, /__u/substackcdn.com/image/fetch/$s_!8dFk!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65e9d79c-7dda-4dc5-85f2-1c5360c5e2d9_1400x729.png 848w, /__u/substackcdn.com/image/fetch/$s_!8dFk!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65e9d79c-7dda-4dc5-85f2-1c5360c5e2d9_1400x729.png 1272w, /__u/substackcdn.com/image/fetch/$s_!8dFk!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65e9d79c-7dda-4dc5-85f2-1c5360c5e2d9_1400x729.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!8dFk!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65e9d79c-7dda-4dc5-85f2-1c5360c5e2d9_1400x729.png" width="1400" height="729" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/65e9d79c-7dda-4dc5-85f2-1c5360c5e2d9_1400x729.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:729,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!8dFk!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65e9d79c-7dda-4dc5-85f2-1c5360c5e2d9_1400x729.png 424w, /__u/substackcdn.com/image/fetch/$s_!8dFk!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65e9d79c-7dda-4dc5-85f2-1c5360c5e2d9_1400x729.png 848w, /__u/substackcdn.com/image/fetch/$s_!8dFk!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65e9d79c-7dda-4dc5-85f2-1c5360c5e2d9_1400x729.png 1272w, /__u/substackcdn.com/image/fetch/$s_!8dFk!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65e9d79c-7dda-4dc5-85f2-1c5360c5e2d9_1400x729.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>I used a hybrid approach that uses both a database and the NLP model to determine the category, though it sometimes makes mistakes. Later though, rebuilt the model to understand the keyword along with the text as context, this one does really well. I was able to achieve 99% accuraccy across 12 different labels last I gave it a go.</p><p>It does sometimes though make mistakes, but that can be rectified later by re-training the model with more quality data.</p><p>If you want to work with text classification, see <a href="https://medium.com/towards-data-science/fine-tune-smaller-transformer-models-text-classification-77cbbd3bf02b">this</a> article.</p><h3>Hosting Smaller NLP Models</h3><p>You can boot up a smaller model using Colab, but using Hugging Face&#8217;s free pipeline to interact with the models would take about 2 hours to process up to 2,000 texts with a 400M parameter model.</p><p>I process up to 10,000 texts per day, which means it would need to run for about 8 hours daily. Hosting the models on a small GPU is incredibly efficient for these smaller models; by utilizing an Nvidia Tesla T4 GPU, I can process 10,000 texts in 30 minutes, batching 20&#8211;40 texts at a time.</p><p>Currently, I&#8217;ve only tried using Hugging Face&#8217;s inference endpoints and Replicate to host these models, mostly because it was straightforward enough to set up. However, it&#8217;s possible to just containerize them and expose them via an API endpoint on your own.</p><h3>Last Notes</h3><p>Any expert with experience in this field would probably take one look at this project and acknowledge that it&#8217;s not so simple. It&#8217;s not the NLPs that are difficult to create, it&#8217;s the entire data cleaning and data sorting that is the biggest hurdle. The creation of the algorithms and the API that calculates what is trending is another huge job.</p><p>The model for extracting tech terms, for example, will inevitably extract variations like &#8220;Google Gemini&#8221; or &#8220;Gemini Pro&#8221; or &#8220;Google&#8217;s Gemini.&#8221;</p><p>Cleaning this up is such work. My hat off to data engineers, honestly.</p><p>You can&#8217;t simply instruct these smaller models; they operate based on the data they&#8217;ve been trained on. You&#8217;ll need additional processing to be able to connect the words correctly, and even then some will fall off.</p><p>The categorization model that decides which label a keyword should be set as uses a pre-trained RoBERTA, which was last trained in 2019. It hasn&#8217;t seen many of these tools and companies that it is tasked to categorize. It will make assumptions for the correct category but you need a vast set of data to train it on for it to be able to perform.</p><p>Another prevalent issue was that these models can sometimes react quite strongly to data it hasn&#8217;t been trained on. For instance, my keyword model would crash the GPU whenever a Chinese character would slip in, simply because it hadn&#8217;t seen this kind of language before.</p><p>The keyword model also has issues in extracting words from comments, since it wasn&#8217;t trained on comments. This is something I will introduce in a later model, but if you&#8217;re new to building these models make sure you introduce the data you want processed early.</p><p>Building the model yourself means you understand the underlying data it was built on and can learn how to work with it effectively.</p><p>When you account for all this work, you also need to account for the work of creating the algorithms and the API that calculates what is trending.</p><p>Nevertheless, I hope this gave you some inspiration to build something of your own. I&#8217;m pretty keen to analyze the data that&#8217;s currently collected and try to find ways I can use it effectively.</p><p>I&#8217;ll keep building out the API which will hopefully be able to derive actual insights on the tech scene in time.</p><p>&#10084;</p>]]></content:encoded></item><item><title><![CDATA[Fine-Tune Smaller NLP Models for Specific Use Cases]]></title><description><![CDATA[With a custom dataset using the transformers library]]></description><link>https://howtouseai.substack.com/p/fine-tune-smaller-nlp-models-for</link><guid isPermaLink="false">https://howtouseai.substack.com/p/fine-tune-smaller-nlp-models-for</guid><dc:creator><![CDATA[Ida Silfverskiold]]></dc:creator><pubDate>Tue, 01 Sep 2026 08:20:30 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!PCzl!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb5b84a0-3884-4fc8-a4f8-50baebd6e259_1172x876.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!PCzl!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb5b84a0-3884-4fc8-a4f8-50baebd6e259_1172x876.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!PCzl!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb5b84a0-3884-4fc8-a4f8-50baebd6e259_1172x876.png 424w, /__u/substackcdn.com/image/fetch/$s_!PCzl!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb5b84a0-3884-4fc8-a4f8-50baebd6e259_1172x876.png 848w, /__u/substackcdn.com/image/fetch/$s_!PCzl!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb5b84a0-3884-4fc8-a4f8-50baebd6e259_1172x876.png 1272w, /__u/substackcdn.com/image/fetch/$s_!PCzl!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb5b84a0-3884-4fc8-a4f8-50baebd6e259_1172x876.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!PCzl!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb5b84a0-3884-4fc8-a4f8-50baebd6e259_1172x876.png" width="1172" height="876" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/eb5b84a0-3884-4fc8-a4f8-50baebd6e259_1172x876.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:876,&quot;width&quot;:1172,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!PCzl!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb5b84a0-3884-4fc8-a4f8-50baebd6e259_1172x876.png 424w, /__u/substackcdn.com/image/fetch/$s_!PCzl!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb5b84a0-3884-4fc8-a4f8-50baebd6e259_1172x876.png 848w, /__u/substackcdn.com/image/fetch/$s_!PCzl!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb5b84a0-3884-4fc8-a4f8-50baebd6e259_1172x876.png 1272w, /__u/substackcdn.com/image/fetch/$s_!PCzl!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb5b84a0-3884-4fc8-a4f8-50baebd6e259_1172x876.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"></figcaption></figure></div><p><em>This is an article dated December 2023 but has been re-published here.</em></p><p>This article will help you understand how to start working with smaller open source NLP models for specific use cases, why it can be more effective, as well as go through how to fine-tune a base model on your own.</p><p>I&#8217;ll get really nitty gritty and fine-tune a sequence to sequence model (BART) with a custom dataset for a specific use case &#8212; <strong>keyword extraction of texts for specific tech terms. The final model you&#8217;ll find</strong> <strong>here.</strong></p><p>I&#8217;ll keep fine-tuning this model, but for now it works decently<strong>.</strong> If you&#8217;re wondering what good the model will do, I&#8217;ll get to that later.</p><p><strong>I&#8217;ve also organized all material I&#8217;ve gathered in this Github repo if you&#8217;ll need it. </strong>This one has intel on the different base models, tasks and business cases as well as examples of fine-tuned models.</p><p>I&#8217;d also like to mention that what we&#8217;ll embark on here is completely <strong>free</strong>. Working with smaller open source models is a tad more complicated but a lot cheaper in the long run.</p><p><strong>Do you need more convincing?</strong> Then go through the introduction section<strong> </strong>otherwise skip it and go to the training section directly.</p><h2>Introduction</h2><h3>Large vs Small Models</h3><p>It&#8217;s easy to assume that using the largest and most advanced models out there &#8212; such as GPT-4 or GPT-3 &#8212; is the most efficient way to work with NLP tasks. I think we can all agree that <strong>GPT-4 </strong>&#8212; and event GPT-3 &#8212;<strong> excels in various areas</strong>. I use it quite often and could very well use it for anything such as sentiment analysis, categorizing documents, and translations.</p><p>Although amazing, <strong>GPT-4 </strong>&#8212; and even GPT-3&#8212; <strong>are quite large models.</strong></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!x4Nj!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63ce4dff-da43-422b-89cf-e31ec88fbb2a_1400x793.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!x4Nj!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63ce4dff-da43-422b-89cf-e31ec88fbb2a_1400x793.png 424w, /__u/substackcdn.com/image/fetch/$s_!x4Nj!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63ce4dff-da43-422b-89cf-e31ec88fbb2a_1400x793.png 848w, /__u/substackcdn.com/image/fetch/$s_!x4Nj!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63ce4dff-da43-422b-89cf-e31ec88fbb2a_1400x793.png 1272w, /__u/substackcdn.com/image/fetch/$s_!x4Nj!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63ce4dff-da43-422b-89cf-e31ec88fbb2a_1400x793.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!x4Nj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63ce4dff-da43-422b-89cf-e31ec88fbb2a_1400x793.png" width="1400" height="793" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/63ce4dff-da43-422b-89cf-e31ec88fbb2a_1400x793.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:793,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!x4Nj!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63ce4dff-da43-422b-89cf-e31ec88fbb2a_1400x793.png 424w, /__u/substackcdn.com/image/fetch/$s_!x4Nj!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63ce4dff-da43-422b-89cf-e31ec88fbb2a_1400x793.png 848w, /__u/substackcdn.com/image/fetch/$s_!x4Nj!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63ce4dff-da43-422b-89cf-e31ec88fbb2a_1400x793.png 1272w, /__u/substackcdn.com/image/fetch/$s_!x4Nj!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63ce4dff-da43-422b-89cf-e31ec88fbb2a_1400x793.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><strong>GPT, specifically, </strong>is a <strong>text generation model</strong> and is built primarily for content generation, which is amazing for creative writing or marketing content, developing chatbots and whatnot. So, although so advanced that it can do anything at this point, it was not technically made for all NLP tasks.</p><blockquote><p>As a side note, most larger language models out there &#8212; open source as well &#8212; are text generation, decoder, models.</p></blockquote><p>So what is the difference between a decoder and other models? See a table below for three examples of open source models, their model types and the specific tasks they were trained for.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!aMW5!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff259f582-5dd4-49f0-84fc-06094a595232_1400x492.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!aMW5!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff259f582-5dd4-49f0-84fc-06094a595232_1400x492.png 424w, /__u/substackcdn.com/image/fetch/$s_!aMW5!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff259f582-5dd4-49f0-84fc-06094a595232_1400x492.png 848w, /__u/substackcdn.com/image/fetch/$s_!aMW5!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff259f582-5dd4-49f0-84fc-06094a595232_1400x492.png 1272w, /__u/substackcdn.com/image/fetch/$s_!aMW5!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff259f582-5dd4-49f0-84fc-06094a595232_1400x492.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!aMW5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff259f582-5dd4-49f0-84fc-06094a595232_1400x492.png" width="1400" height="492" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f259f582-5dd4-49f0-84fc-06094a595232_1400x492.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:492,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!aMW5!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff259f582-5dd4-49f0-84fc-06094a595232_1400x492.png 424w, /__u/substackcdn.com/image/fetch/$s_!aMW5!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff259f582-5dd4-49f0-84fc-06094a595232_1400x492.png 848w, /__u/substackcdn.com/image/fetch/$s_!aMW5!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff259f582-5dd4-49f0-84fc-06094a595232_1400x492.png 1272w, /__u/substackcdn.com/image/fetch/$s_!aMW5!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff259f582-5dd4-49f0-84fc-06094a595232_1400x492.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Go <a href="https://github.com/ilsilfverskiold/transformers-nlp-docs/tree/main/docs/models">here</a> to check out all the base models for each model architecture.</p><p>GPT-2 is an open source model, but obviously the more advanced models by OpenAI are all closed source. BERT and BART is what is interesting to us here, and excel in other areas which we&#8217;ll talk about further.</p><p>If you&#8217;re wondering how these models tasks fits into a specific business case, go <a href="https://github.com/ilsilfverskiold/smaller-models-docs/tree/main/nlp/docs/business-cases">here</a> to brainstorm. You&#8217;ll see examples of cases based on tasks for encoder, decoder and seq-to-seq models.</p><p>If you&#8217;re confused, skip it for now. Just know that <strong>all of these models</strong>, along with GPT, <strong>were originally built for different NLP tasks.</strong></p><p>When choosing a model you should also consider more than simply performance but also computational resources and cost. When you get introduced to AI with GPT-3, it&#8217;s easy to disregard the other choices out there.</p><p>Sometimes, smaller, less complex models &#8212; fine-tuned for a specific use case &#8212; is more efficient in terms of resource usage and speed. It will also be significantly <strong>less expensive.</strong></p><p>Let&#8217;s illustrate this.</p><p>Say we&#8217;re doing something banal, like analyzing sentiment or summarizing content. We&#8217;re scraping certain social media sites or maybe customer service conversations and then using AI to analyze its content, summarizing and giving us a report.</p><p>Let&#8217;s look at the costs of using a larger closed source model like GPT-4 Turbo, GPT-3.5 Turbo, or Claude 2 if we were to analyze up to 3,000 texts <strong>per day</strong> with 400 tokens input and 200 tokens out.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!ue_8!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F441146e4-a8db-4978-980b-c706c73d5ec8_1300x884.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!ue_8!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F441146e4-a8db-4978-980b-c706c73d5ec8_1300x884.png 424w, /__u/substackcdn.com/image/fetch/$s_!ue_8!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F441146e4-a8db-4978-980b-c706c73d5ec8_1300x884.png 848w, /__u/substackcdn.com/image/fetch/$s_!ue_8!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F441146e4-a8db-4978-980b-c706c73d5ec8_1300x884.png 1272w, /__u/substackcdn.com/image/fetch/$s_!ue_8!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F441146e4-a8db-4978-980b-c706c73d5ec8_1300x884.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!ue_8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F441146e4-a8db-4978-980b-c706c73d5ec8_1300x884.png" width="1300" height="884" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/441146e4-a8db-4978-980b-c706c73d5ec8_1300x884.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:884,&quot;width&quot;:1300,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!ue_8!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F441146e4-a8db-4978-980b-c706c73d5ec8_1300x884.png 424w, /__u/substackcdn.com/image/fetch/$s_!ue_8!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F441146e4-a8db-4978-980b-c706c73d5ec8_1300x884.png 848w, /__u/substackcdn.com/image/fetch/$s_!ue_8!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F441146e4-a8db-4978-980b-c706c73d5ec8_1300x884.png 1272w, /__u/substackcdn.com/image/fetch/$s_!ue_8!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F441146e4-a8db-4978-980b-c706c73d5ec8_1300x884.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Costs to make up to 3,000 API calls per day for up to 31 days &#8212; keep in mind, that the larger your system template or prompt template is the more costly every API call will be.</p><p>We could obviously try to batch the texts in the same API calls but then our tokens would increase. The idea is though, that using these larger models for something simple is overkill.</p><p>I also need to stress, that when we build our own models we try to make them as small as possible because we also have to host them. Hosting can be very expensive.</p><p>Let&#8217;s look at a few smaller open source fine-tuned models in Hugging Face that does sentiment analysis <a href="https://huggingface.co/mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis">here</a>. These are fine-tuned models performing on a specific task.</p><p>Below is also another sentiment analysis model built on RoBERTa, an encoder model, that helps you classify the sentiment of texts as positive, negative or neutral.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!wQqp!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ef717d1-902d-4760-b739-28b0705eb887_2428x1390.gif" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!wQqp!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ef717d1-902d-4760-b739-28b0705eb887_2428x1390.gif 424w, /__u/substackcdn.com/image/fetch/$s_!wQqp!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ef717d1-902d-4760-b739-28b0705eb887_2428x1390.gif 848w, /__u/substackcdn.com/image/fetch/$s_!wQqp!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ef717d1-902d-4760-b739-28b0705eb887_2428x1390.gif 1272w, /__u/substackcdn.com/image/fetch/$s_!wQqp!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ef717d1-902d-4760-b739-28b0705eb887_2428x1390.gif 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!wQqp!,w_1456,c_limit,f_auto,q_auto:good,fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ef717d1-902d-4760-b739-28b0705eb887_2428x1390.gif" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8ef717d1-902d-4760-b739-28b0705eb887_2428x1390.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:null,&quot;width&quot;:null,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:5592026,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/gif&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://howtouseai.substack.com/i/213672217?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ef717d1-902d-4760-b739-28b0705eb887_2428x1390.gif&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!wQqp!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ef717d1-902d-4760-b739-28b0705eb887_2428x1390.gif 424w, /__u/substackcdn.com/image/fetch/$s_!wQqp!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ef717d1-902d-4760-b739-28b0705eb887_2428x1390.gif 848w, /__u/substackcdn.com/image/fetch/$s_!wQqp!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ef717d1-902d-4760-b739-28b0705eb887_2428x1390.gif 1272w, /__u/substackcdn.com/image/fetch/$s_!wQqp!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8ef717d1-902d-4760-b739-28b0705eb887_2428x1390.gif 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p></p><p>This model is finiteautomata/<a href="https://huggingface.co/finiteautomata/bertweet-base-sentiment-analysis">bertweet-base-sentiment-analysis</a> if you want to try it out on your own.</p><p>These are just examples. Go <a href="https://huggingface.co/models">browse</a> your own.</p><p>As a side note, when you test the models on the Hugging Face hub, you are using the Inference API which is for testing purposes only. If you want to go ahead connect to the models in Colab with Hugging Face&#8217;s pipeline use this script.</p><p>There are tons of more models available. Just to demonstrate, here is one to spot fake news, one to classify click bait articles and so on.</p><p><strong>This is still very new, </strong>so models are a bit here and there and it&#8217;s hard to understand the performance of each model. You can look at likes and downloads. You should obviously test them too. But you&#8217;ll see more and more fine-tuned models being shared, maybe they&#8217;ll be a bit more organized in the future as well.</p><p>Nevertheless, the point is that you&#8217;ll find a several models that are vastly smaller than GPT-4 or Claude that might do well enough for your specific use case, especially when you account for cost.</p><p>As for how to use a Hugging Face model in your application, you can deploy your model using Hugging Face&#8217;s Inference Endpoints and let the application scale to 0 when not in use. For a model with 400 million parameters you would pay from $0.5 per hour (when in use) for a small GPU instance. You can also use the model directly in Google Colab or locally with Hugging Face&#8217;s pipeline for free.</p><blockquote><p>There are also tools such as Replicate and Modal that offer cloud hosting with pricing per second of use. The other option is to containerize a model on your own and expose them via an API endpoint, using AWS for hosting.</p></blockquote><p>So, Hugging Face is a great hub for finding and sharing open source models and datasets. As for hosting them, the future looks really bright. But the key here is how to actually train these smaller base models for a specific use case.</p><p><strong>Hugging Face has simplified this for us too </strong>in various ways giving us an accessible training API, pre-built tokenizers and dynamic padding collators. What this means is that they will give us the correct tools to easily fine-tune these models without <strong>extensive </strong>machine learning knowledge.</p><p>Just as a side note about the transformers architecture,<em> </em>transformers have made it much easier for computers to understand and interact with human language.<strong> </strong>It has a unique structure that allows it to process words in relation to all other words in a sentence, rather than one at a time sequentially. This has made it possible to understand context better than previous models.</p><p>Transformers has therefore improved the output quality in general, which is something you&#8217;ve seen if you&#8217;ve tried ChatGPT, that offers fine-tuned versions of GPT-3 and GPT-4. I won&#8217;t go further than that as there is a lot of information out there you can scout. Check the Hugging Face <a href="https://huggingface.co/learn">learning hub</a>, they have some great resources.</p><p>Instead, I&#8217;ll get right into working with a specific use case.</p><h3>Use Case: Keyword Extraction</h3><p>I came to Hugging Face because I was keen to build a fine-tuned model that would extract specific tech terms (i.e. keywords) from texts. I think we forget how well these NLP models can do at things we used libraries such as NLTK for before.</p><p>See an example of me trying to extract keywords from a text using NLTK, spaCy and KeyBERT below.</p><p>None of these libraries did what I need it to do. If I would aggregate thousands of texts the words that would be at the top would be &#8216;want&#8217; &#8216;make&#8217; and &#8216;said.&#8217;</p><blockquote><p>You&#8217;ll see me testing KeyBERT above as well which is a library that is provided by Hugging Face and built on an enconder model (BERT). Unfortunately, it didn&#8217;t work well enough for my use case.</p></blockquote><p>Here the keywords I would be looking for.</p><pre><code><code>Bubble.io, Shopify, Analytics</code></code></pre><p>To be able to do this, I would need something very specific most libraries wouldn&#8217;t be able to provide.</p><p>I did find a <a href="https://huggingface.co/transformer3/H2-keywordextractor?text=Utilizing+the+Lease+Pattern+on+AWS+Using+DDB+and+DDB+Lock+Client+Library">model</a> that had already been fine-tuned in Hugging Face and that did a decent job. Check out the results from this model below.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!Wk9f!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec5ba4ca-8b19-4aae-9592-979a00f1e157_2092x1390.gif" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!Wk9f!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec5ba4ca-8b19-4aae-9592-979a00f1e157_2092x1390.gif 424w, /__u/substackcdn.com/image/fetch/$s_!Wk9f!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec5ba4ca-8b19-4aae-9592-979a00f1e157_2092x1390.gif 848w, /__u/substackcdn.com/image/fetch/$s_!Wk9f!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec5ba4ca-8b19-4aae-9592-979a00f1e157_2092x1390.gif 1272w, /__u/substackcdn.com/image/fetch/$s_!Wk9f!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec5ba4ca-8b19-4aae-9592-979a00f1e157_2092x1390.gif 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!Wk9f!,w_1456,c_limit,f_auto,q_auto:good,fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec5ba4ca-8b19-4aae-9592-979a00f1e157_2092x1390.gif" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ec5ba4ca-8b19-4aae-9592-979a00f1e157_2092x1390.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:null,&quot;width&quot;:null,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:6623959,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/gif&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://howtouseai.substack.com/i/213672217?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec5ba4ca-8b19-4aae-9592-979a00f1e157_2092x1390.gif&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!Wk9f!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec5ba4ca-8b19-4aae-9592-979a00f1e157_2092x1390.gif 424w, /__u/substackcdn.com/image/fetch/$s_!Wk9f!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec5ba4ca-8b19-4aae-9592-979a00f1e157_2092x1390.gif 848w, /__u/substackcdn.com/image/fetch/$s_!Wk9f!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec5ba4ca-8b19-4aae-9592-979a00f1e157_2092x1390.gif 1272w, /__u/substackcdn.com/image/fetch/$s_!Wk9f!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fec5ba4ca-8b19-4aae-9592-979a00f1e157_2092x1390.gif 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p></p><p>The Inference API endpoint will be slow to load for the first API call &#8212; be patient</p><p>Better but not good enough.</p><p>If I test a few more texts with the fine-tuned BART model I found above I would get these results.</p><p>I needed something cleaner than this. The words returned weren&#8217;t always isolated, correct nor relevant.</p><p>So let&#8217;s check out the fine-tuned model I built below. I&#8217;ll demonstrate it with the same texts I used above.</p><p>See how it returns just a<strong> few relevant keywords</strong> making sure to separate names such as Docker? This is what I needed. By being able to extract the correct keywords, I can do <strong>analysis on thousands of texts.</strong></p><p>The model I&#8217;ve built is not perfect as I&#8217;ve only fine-tuned it once. As for trying the model yourself, it is open-source so you may use it <a href="https://huggingface.co/ilsilfverskiold/tech-keywords-extractor?text=Utilizing+the+Lease+Pattern+on+AWS+Using+DDB+and+DDB+Lock+Client+Library">too</a>.</p><p>To build this model I had 50,000 titles of different sizes I&#8217;ve accessed from various social media platforms using their public API endpoints. I was too lazy to process all of it as a first go but <strong>I did a trial of 3,000 texts along with 8,500 texts.</strong> The first trial run did alright with 3,000 data points but it wasn&#8217;t good enough so I went back to process 5,500 more rows.</p><p>I did use GPT-4 as help to transform the text to keywords. I dished out $30 for 10,000 texts, batched by 10 for every API call. Here is the <a href="https://github.com/ilsilfverskiold/gpt-create-dataset">repository</a> for the script if you need to build your own dataset.</p><p>You need to make sure that the keywords are exactly the keywords you want generated so I had to manually go through 8,500 rows. As anyone will tell you, data is the biggest factor for fine-tuning.<strong> </strong>What you put in will come out.</p><p>It&#8217;s good to know, that quality trumps quantity always. You want quality data first, then if you can you add on to it.</p><p>I&#8217;ve shared my processed dataset of 8,500 rows <a href="https://huggingface.co/datasets/ilsilfverskiold/tech-keywords-topics-summary/viewer/default/train">here</a> so check it out or use it yourself.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!IqRq!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd956e466-87b8-4cb7-8014-0292a0819cb8_1400x728.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!IqRq!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd956e466-87b8-4cb7-8014-0292a0819cb8_1400x728.png 424w, /__u/substackcdn.com/image/fetch/$s_!IqRq!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd956e466-87b8-4cb7-8014-0292a0819cb8_1400x728.png 848w, /__u/substackcdn.com/image/fetch/$s_!IqRq!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd956e466-87b8-4cb7-8014-0292a0819cb8_1400x728.png 1272w, /__u/substackcdn.com/image/fetch/$s_!IqRq!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd956e466-87b8-4cb7-8014-0292a0819cb8_1400x728.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!IqRq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd956e466-87b8-4cb7-8014-0292a0819cb8_1400x728.png" width="1400" height="728" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d956e466-87b8-4cb7-8014-0292a0819cb8_1400x728.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:728,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!IqRq!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd956e466-87b8-4cb7-8014-0292a0819cb8_1400x728.png 424w, /__u/substackcdn.com/image/fetch/$s_!IqRq!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd956e466-87b8-4cb7-8014-0292a0819cb8_1400x728.png 848w, /__u/substackcdn.com/image/fetch/$s_!IqRq!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd956e466-87b8-4cb7-8014-0292a0819cb8_1400x728.png 1272w, /__u/substackcdn.com/image/fetch/$s_!IqRq!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd956e466-87b8-4cb7-8014-0292a0819cb8_1400x728.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The dataset I had created to build the tech-keyword-extractor using the fields &#8216;text&#8217; and &#8216;keywords&#8217;</p><p>This dataset has several fields but the ones I&#8217;ve added are keywords, topic and summary. Go nuts with that one if you want to build a different model, i.e. you can build a model that gives you the topic or a 3 word summary of the text rather than keywords.</p><p>To process my dataset from Hugging Face to Colab check out <a href="https://github.com/ilsilfverskiold/smaller-models-docs/blob/main/nlp/cook/datasets/process_huggingface_dataset.ipynb">this</a> script. We&#8217;ll go through processing this one later too though when fine-tuning the model.</p><h3>Process</h3><p>Let&#8217;s go through the process to to build this keyword model. If you have another use case in mind for a sequence to sequence model, this process should work the same.</p><p>The parts will be as follows.</p><ul><li><p><strong>Explore models</strong> &amp; tasks</p></li><li><p>Set up your <strong>dataset</strong></p></li><li><p><strong>Process</strong> the data</p></li><li><p><strong>Train</strong> the model &amp; <strong>test</strong> it</p></li><li><p><strong>Push</strong> the model to Hugging Face</p></li></ul><h3>Some Notes</h3><p>If you&#8217;re <strong>not comfortable with code</strong> what so ever, I would suggest giving Hugging Face&#8217;s <strong><a href="https://huggingface.co/autotrain">AutoTrain</a></strong> a go. I think the first process is free but otherwise you&#8217;ll have do dish out some cash to continue to use this service.</p><p>Remember to make sure your dataset is on-point regardless.</p><p>If you want to do this manually with their trainer API manually, read on. This will be completely free.</p><p>Also use this <a href="https://chat.openai.com/g/g-IlWD2J8i9-huggingface-helper">Hugging Face GPT</a> if you run into issues. Great for weird questions you don&#8217;t want anyone to see. I build one for anything I do nowadays.</p><h2>Training</h2><h3>Models &amp; Tasks</h3><p>First, it&#8217;s good to just explain a bit about encoder vs decoder NLP models, especially when working with smaller models. There should be a ton of information you can scout on this too, so I&#8217;ll keep it concise.</p><p>The fundamental difference is that an <strong>encoder model</strong> will generally generate condensed outputs from larger inputs whereas <strong>decoder models</strong> will generally expand or generate data from a smaller input.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!Eh17!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F31514b9c-e0c9-4676-8e67-e528c365db15_1400x809.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!Eh17!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F31514b9c-e0c9-4676-8e67-e528c365db15_1400x809.png 424w, /__u/substackcdn.com/image/fetch/$s_!Eh17!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F31514b9c-e0c9-4676-8e67-e528c365db15_1400x809.png 848w, /__u/substackcdn.com/image/fetch/$s_!Eh17!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F31514b9c-e0c9-4676-8e67-e528c365db15_1400x809.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Eh17!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F31514b9c-e0c9-4676-8e67-e528c365db15_1400x809.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!Eh17!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F31514b9c-e0c9-4676-8e67-e528c365db15_1400x809.png" width="1400" height="809" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/31514b9c-e0c9-4676-8e67-e528c365db15_1400x809.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:809,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!Eh17!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F31514b9c-e0c9-4676-8e67-e528c365db15_1400x809.png 424w, /__u/substackcdn.com/image/fetch/$s_!Eh17!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F31514b9c-e0c9-4676-8e67-e528c365db15_1400x809.png 848w, /__u/substackcdn.com/image/fetch/$s_!Eh17!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F31514b9c-e0c9-4676-8e67-e528c365db15_1400x809.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Eh17!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F31514b9c-e0c9-4676-8e67-e528c365db15_1400x809.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>A very simplified visual of decoder vs encoder inputs and outputs</p><p>The technical explanation is that a decoder will look at the next sequence, i.e. what word comes next, while an encoder model will look at the text in its entirety to understand the meaning of the entire input.</p><p>A decoder model is more concerned with generating the next sentence while an encoder model focuses on analyzing and derive meaning from the entire text.</p><p>A <strong>decoder model &#8212; like GPT &#8212; </strong>thus excels in tasks like <strong>text generation</strong>, where you start with a small amount of information and need to create a more extensive coherent output. An <strong>encoder model</strong>,<strong> </strong>on the other hand,<strong> </strong>is trained to understand the context or the big picture, which is great for tasks where you want to <strong>classify or extract</strong> <strong>content.</strong></p><p><strong>Sequence-to-sequence models</strong> is a mix of encoder and decoder, where the encoder processes the input and the decoder generates the output. The first transformer model that was introduced in the &#8220;Attention is All You Need&#8221; paper was a sequence to sequence model.</p><p>I think of sequence to sequence (Seq2Seq) models as models that will be able to provide condensed outputs that are a bit more coherent compared to encoder models. I.e. it will excel in summarization which provides a more condensed version of a text but it is still able to maintain context.</p><blockquote><p>A question you may have at this point, is why are the bigger models mostly decoder-only models? Could be because of computational efficiency. But I don&#8217;t have all the answers, so I would suggest you go and do some research on your own there.</p></blockquote><p>It&#8217;s also worth noting, that the architecture was split up into decoder and encoder later and today&#8217;s larger LLMs can obviously handle many tasks &#8212; i.e. classification, summarization and so on &#8212; because of their massive scale. However, these larger LLMs might still not provide the specialized performance of all models in all scenarios.</p><p>To understand how you can find pre-trained models for each model type, look at the table below. This demonstrates a few different smaller pre-trained base models.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!Tyui!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34b4771c-99c3-4de8-945f-abb215a4aa8c_1400x586.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!Tyui!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34b4771c-99c3-4de8-945f-abb215a4aa8c_1400x586.png 424w, /__u/substackcdn.com/image/fetch/$s_!Tyui!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34b4771c-99c3-4de8-945f-abb215a4aa8c_1400x586.png 848w, /__u/substackcdn.com/image/fetch/$s_!Tyui!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34b4771c-99c3-4de8-945f-abb215a4aa8c_1400x586.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Tyui!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34b4771c-99c3-4de8-945f-abb215a4aa8c_1400x586.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!Tyui!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34b4771c-99c3-4de8-945f-abb215a4aa8c_1400x586.png" width="1400" height="586" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/34b4771c-99c3-4de8-945f-abb215a4aa8c_1400x586.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:586,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!Tyui!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34b4771c-99c3-4de8-945f-abb215a4aa8c_1400x586.png 424w, /__u/substackcdn.com/image/fetch/$s_!Tyui!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34b4771c-99c3-4de8-945f-abb215a4aa8c_1400x586.png 848w, /__u/substackcdn.com/image/fetch/$s_!Tyui!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34b4771c-99c3-4de8-945f-abb215a4aa8c_1400x586.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Tyui!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34b4771c-99c3-4de8-945f-abb215a4aa8c_1400x586.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This list is not exhaustive of pretrained models you can use &#8212; go through this <a href="https://github.com/ilsilfverskiold/transformers-nlp-docs/tree/main/docs/models">repo</a> to see more.</p><p>The bigger models usually perform better for more complex tasks, so be a bit strategic on how you use them. With big, for the smaller models, I mean around 400&#8211;500M parameters.</p><p>If you are a bit lost, remember to look at the model types <a href="https://github.com/ilsilfverskiold/smaller-models-docs/tree/main/nlp/docs/models">here</a>. Also check different business cases for the different models <a href="https://github.com/ilsilfverskiold/smaller-models-docs/tree/main/nlp/docs/business-cases">here</a>.</p><p>For my case, I will try <strong>BART as a base model</strong> and will use <strong>Summarization as the task </strong>for a model that will extract tech terms and names from texts.</p><p>Maybe this is an odd choice as an encoder model seem to be the better choice for keyword extraction. With a seq-to-seq model I may end up with summary keywords, rather than just the keywords. I.e. it may create its own. But I&#8217;m ok with this.</p><p>I will also use a fairly large pre-trained model of 400 million parameters. I would suggest you build one with a smaller model to see how it does, as the hosting costs increase with the size of the model.</p><h3>Obtain the Dataset</h3><p>After you&#8217;ve decided on your model and your task, next is to obtain a dataset.</p><p>The dataset will look different based on the model you will be training. For me, as I&#8217;m training a Seq2Seq model (BART-large) as mentioned above, I will have a text and a target &#8212; the keywords &#8212; in my dataset.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!S4LE!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7233d0f-3c8b-4701-98f0-8760e154504f_1400x693.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!S4LE!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7233d0f-3c8b-4701-98f0-8760e154504f_1400x693.png 424w, /__u/substackcdn.com/image/fetch/$s_!S4LE!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7233d0f-3c8b-4701-98f0-8760e154504f_1400x693.png 848w, /__u/substackcdn.com/image/fetch/$s_!S4LE!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7233d0f-3c8b-4701-98f0-8760e154504f_1400x693.png 1272w, /__u/substackcdn.com/image/fetch/$s_!S4LE!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7233d0f-3c8b-4701-98f0-8760e154504f_1400x693.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!S4LE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7233d0f-3c8b-4701-98f0-8760e154504f_1400x693.png" width="1400" height="693" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b7233d0f-3c8b-4701-98f0-8760e154504f_1400x693.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:693,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!S4LE!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7233d0f-3c8b-4701-98f0-8760e154504f_1400x693.png 424w, /__u/substackcdn.com/image/fetch/$s_!S4LE!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7233d0f-3c8b-4701-98f0-8760e154504f_1400x693.png 848w, /__u/substackcdn.com/image/fetch/$s_!S4LE!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7233d0f-3c8b-4701-98f0-8760e154504f_1400x693.png 1272w, /__u/substackcdn.com/image/fetch/$s_!S4LE!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb7233d0f-3c8b-4701-98f0-8760e154504f_1400x693.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>As for the size of it, maybe 3000 texts will do well enough, but ideally you&#8217;ll need at least 8,000 examples. Either you create your own dataset or you use an already created dataset that has been shared by someone else.</p><p>My dataset is 8,500 texts. You&#8217;ll find it here so you can use the same data. It has several rows so you can pick another field such as summary or topic as your target instead.</p><p>Another option is to <strong>browse the Hugging Face <a href="https://huggingface.co/datasets">hub</a> for a dataset</strong> you can use. Here you&#8217;ll have large datasets as options which is great if you&#8217;re just getting started.</p><p>If you&#8217;re building this <strong>dataset from scratch</strong>, see this <a href="https://github.com/ilsilfverskiold/gpt-create-dataset">script</a> on generating your new dataset with GPT-4. This will be faster. But even with help, creating your own data is hard work. I manually checked my dataset and it took me two days to properly go through it.</p><p>Once you&#8217;ve decided on your dataset, you&#8217;ll have to make sure to split your data into a training, testing and validation set. The split usually is 80% of data for training, 10% validation and 10% testing depending on how small or large your dataset is.</p><p>To create a dataset dict with the appropriate datasets using either a custom CSV file or import a dataset from Hugging Face in Colab, see these <a href="https://github.com/ilsilfverskiold/smaller-models-docs/tree/main/nlp/cook/datasets">cook books.</a> Most datasets in Hugging Face are already processed with the appropriate sets, but if they are incomplete you can use these cook books as a guide.</p><p>My dataset already has the proper sets set up so no need to tweak it. You&#8217;ll see what this looks like when we import it into a Colab notebook.</p><h3>Shop Around for a Base Model</h3><p>I&#8217;ve seen many &#8216;shop&#8217; around for a base model before they decide which one to take. You can fine-tune a base model directly or a model that has already been fine-tuned and fine-tune it further. It needs to be a transformer model though.</p><p>I have never liked the results from fine-tuning an already fine-tuned model but it is possible. This is when it has been fine-tuned for a specific task not pre-trained.</p><p>You can either test a bit in Hugging Face directly using the Inference API or open up a Colab <a href="https://github.com/ilsilfverskiold/smaller-models-docs/blob/main/nlp/cook/using/pipeline_testing_models.ipynb">notebook</a> and use their pipeline.</p><h3>Process the Dataset</h3><p>Let&#8217;s start.</p><blockquote><p>If I am working with custom data, I can get it from my Google Drive. If you&#8217;re doing the same you would start a bit differently. Here you would first import the file and then create a dataset dict with the training, validating and test sets. Here is the <a href="https://github.com/ilsilfverskiold/transformers-nlp-docs/blob/main/cook/datasets/process_custom_dataset.ipynb">cook book</a> for this.</p></blockquote><p>What we&#8217;ll do here is just import my <a href="https://huggingface.co/datasets/ilsilfverskiold/tech-keywords-topics-summary">dataset</a> from Hugging Face so we can use it.</p><p>Open up a new Colab <a href="https://colab.research.google.com/">notebook</a>. The entire script for this process you&#8217;ll find <a href="https://github.com/ilsilfverskiold/smaller-models-docs/blob/main/nlp/cook/fine-tune/fine_tune_seqtoseq_tech_keywords_dataset.ipynb">here</a> if you want it generated for you.</p><p>If you want to go through the process, we&#8217;ll start by importing all the dependencies we need.</p><pre><code><code>!pip install -U datasets
!pip install -U accelerate
!pip install -U transformers
!pip install -U huggingface_hub</code></code></pre><p>Then we&#8217;ll import the dataset we&#8217;ll be using. Here it&#8217;s your choice what dataset you&#8217;ll import.</p><pre><code><code>from datasets import load_dataset

dataset = load_dataset(&#8221;ilsilfverskiold/tech-keywords-topics-summary&#8221;)
dataset</code></code></pre><p>This one will already have a training, validation and test set setup for you. It should log this if you run it.</p><pre><code><code>DatasetDict({
    train: Dataset({
        features: [&#8217;id&#8217;, &#8216;source&#8217;, &#8216;text&#8217;, &#8216;timestamp&#8217;, &#8216;reactions&#8217;, &#8216;engagement&#8217;, &#8216;url&#8217;, &#8216;text_length&#8217;, &#8216;keywords&#8217;, &#8216;topic&#8217;, &#8216;summary&#8217;, &#8216;__index_level_0__&#8217;],
        num_rows: 7196
    })
    validation: Dataset({
        features: [&#8217;id&#8217;, &#8216;source&#8217;, &#8216;text&#8217;, &#8216;timestamp&#8217;, &#8216;reactions&#8217;, &#8216;engagement&#8217;, &#8216;url&#8217;, &#8216;text_length&#8217;, &#8216;keywords&#8217;, &#8216;topic&#8217;, &#8216;summary&#8217;, &#8216;__index_level_0__&#8217;],
        num_rows: 635
    })
    test: Dataset({
        features: [&#8217;id&#8217;, &#8216;source&#8217;, &#8216;text&#8217;, &#8216;timestamp&#8217;, &#8216;reactions&#8217;, &#8216;engagement&#8217;, &#8216;url&#8217;, &#8216;text_length&#8217;, &#8216;keywords&#8217;, &#8216;topic&#8217;, &#8216;summary&#8217;, &#8216;__index_level_0__&#8217;],
        num_rows: 635
    })
})</code></code></pre><p>The validation and testing sets are really low. I found it did alright but ideally you&#8217;d want all around more data.</p><p>If you&#8217;re importing a dataset without a specific set such as validation and testing, see this <a href="https://github.com/ilsilfverskiold/smaller-models-docs/blob/main/nlp/cook/datasets/process_huggingface_dataset.ipynb">cook book</a>.</p><p>We can map out some examples from the dataset to see what it looks like.</p><pre><code><code>def show_samples(dataset, num_samples=3, seed=42):
    sample = dataset[&#8221;train&#8221;].shuffle(seed=seed).select(range(num_samples))
    for example in sample:
        print(f&#8221;\n&#8217;&gt;&gt; Text: {example[&#8217;text&#8217;]}&#8217;&#8221;)
        print(f&#8221;&#8217;&gt;&gt; Keywords: {example[&#8217;keywords&#8217;]}&#8217;&#8221;)


show_samples(dataset)</code></code></pre><p>The result is this.</p><pre><code><code>&#8216;&gt;&gt; Text: Driverless car users will not be prosecuted for fatal crashes in UK&#8217;
&#8216;&gt;&gt; Keywords: Driverless Cars, Legal Issues, UK&#8217;

&#8216;&gt;&gt; Text: Google is embedding inaudible watermarks right into its AI generated music -&#8217;
&#8216;&gt;&gt; Keywords: Google, AI Music, Watermarks, Audio Technology&#8217;

&#8216;&gt;&gt; Text: What are your thoughts on Nextjs performance? Do you agree with this chart? - ( by 10up where Nextjs appears lower than WordPress on core vitals. Couldn&#8217;t post the image here due to community rules. But appreciate any other studies and thought you have on this matter.&#8217;
&#8216;&gt;&gt; Keywords: Next.js, Performance, 10up, WordPress&#8217;</code></code></pre><p>Here I&#8217;m using text and keywords as fields. You may decide to use other fields though.</p><p>Next we&#8217;ll get the tokenizer for the base-model we&#8217;re using so we can check if our texts are too long to be processed as a single input. A tokenizer converts text into tokens, allowing models to understand language. BART has a 1024 token limit for each input.</p><pre><code><code>from transformers import AutoModelForSeq2SeqLM, AutoTokenizer

model_name = &#8216;facebook/bart-large&#8217; # go smaller if you can
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)

texts = dataset[&#8217;train&#8217;][&#8217;text&#8217;]

# Tokenize all texts and find the maximum length (max for BART is 1024 tokens)
max_token_length = max(len(tokenizer.encode(text, truncation=True)) for text in texts)
print(f&#8221;The longest text is {max_token_length} tokens long.&#8221;)</code></code></pre><p>I&#8217;m way below this token limit though, but I suppose it&#8217;s good practice to check.</p><p>Now we&#8217;ll preprocess this data and convert both the input text and the target (i.e. the keywords) into a format suitable for training a sequence-to-sequence model.</p><pre><code><code>def get_feature(batch):
  encodings = tokenizer(batch[&#8217;text&#8217;], text_target=batch[&#8217;keywords&#8217;],
                        max_length=1024, truncation=True)

  encodings = {&#8217;input_ids&#8217;: encodings[&#8217;input_ids&#8217;],
               &#8216;attention_mask&#8217;: encodings[&#8217;attention_mask&#8217;],
               &#8216;labels&#8217;: encodings[&#8217;labels&#8217;]}

  return encodings

dataset_pt = dataset.map(get_feature, batched=True)
dataset_pt</code></code></pre><p>Remember data preprocessing functions would look different if you are using a model with a different architecture, such as an encoder-only or decoder-only model. This one is designed to handle both the input text and the target outputs (in this case, keywords).</p><p>This dataset_pt will now log a few new fields, input_ids, attention_mask and labels.</p><pre><code><code>DatasetDict({
    train: Dataset({
        features: [&#8217;id&#8217;, &#8216;source&#8217;, &#8216;text&#8217;, &#8216;timestamp&#8217;, &#8216;reactions&#8217;, &#8216;engagement&#8217;, &#8216;url&#8217;, &#8216;text_length&#8217;, &#8216;keywords&#8217;, &#8216;topic&#8217;, &#8216;summary&#8217;, &#8216;__index_level_0__&#8217;, &#8216;input_ids&#8217;, &#8216;attention_mask&#8217;, &#8216;labels&#8217;],
        num_rows: 7196
    })
    validation: Dataset({
        features: [&#8217;id&#8217;, &#8216;source&#8217;, &#8216;text&#8217;, &#8216;timestamp&#8217;, &#8216;reactions&#8217;, &#8216;engagement&#8217;, &#8216;url&#8217;, &#8216;text_length&#8217;, &#8216;keywords&#8217;, &#8216;topic&#8217;, &#8216;summary&#8217;, &#8216;__index_level_0__&#8217;, &#8216;input_ids&#8217;, &#8216;attention_mask&#8217;, &#8216;labels&#8217;],
        num_rows: 635
    })
    test: Dataset({
        features: [&#8217;id&#8217;, &#8216;source&#8217;, &#8216;text&#8217;, &#8216;timestamp&#8217;, &#8216;reactions&#8217;, &#8216;engagement&#8217;, &#8216;url&#8217;, &#8216;text_length&#8217;, &#8216;keywords&#8217;, &#8216;topic&#8217;, &#8216;summary&#8217;, &#8216;__index_level_0__&#8217;, &#8216;input_ids&#8217;, &#8216;attention_mask&#8217;, &#8216;labels&#8217;],
        num_rows: 635
    })
})</code></code></pre><p>We will specify that these new fields are the ones that should be returned.</p><pre><code><code>columns = [&#8217;input_ids&#8217;, &#8216;labels&#8217;, &#8216;attention_mask&#8217;]
dataset_pt.set_format(type=&#8217;torch&#8217;, columns=columns)</code></code></pre><p>The last thing we&#8217;ll do before we start to train the model is get the data collator for a sequence to sequence model.</p><p>The data collator is responsible for dynamically padding the batches to the maximum length in each batch which is crucial for efficient training of transformer models like BART or T5.</p><pre><code><code>from transformers import DataCollatorForSeq2Seq

data_collator = DataCollatorForSeq2Seq(tokenizer, model=model)</code></code></pre><p>Padding will look different depending on the type of model you use.</p><p>When looking at the preprocessing function and data collator, see it as us trying to translate human readable text into something a computer will understand.</p><p>Models are designed differently and that&#8217;s why it may look different if you were to use an encoder-only or decoder-only model.</p><h3>Training</h3><p>We should be ready to start training the model. We&#8217;re using the <a href="https://huggingface.co/docs/transformers/main_classes/trainer">Trainer API</a> which abstracts a lot of complexity here and makes it easy for us to fine-tune transformer models.</p><p>Make sure you switch your runtime on Colab to use at least T4. If you&#8217;re on a pro plan you can use the V100 but then also increase the batch size to at least 8.</p><pre><code><code>from transformers import TrainingArguments, Trainer

training_args = TrainingArguments(
    output_dir = &#8216;bart_tech_keywords&#8217;, # rename to what you want it to be called
    num_train_epochs=3, # your choice
    warmup_steps = 500,
    per_device_train_batch_size=4, 
    per_device_eval_batch_size=4,
    weight_decay = 0.01,
    logging_steps = 10,
    evaluation_strategy = &#8216;steps&#8217;,
    eval_steps=50, 
    save_steps=1e6,
    gradient_accumulation_steps=16 
)

trainer = Trainer(model=model, args=training_args, tokenizer=tokenizer, data_collator=data_collator,
                  train_dataset = dataset_pt[&#8217;train&#8217;], eval_dataset = dataset_pt[&#8217;validation&#8217;])

trainer.train()</code></code></pre><p>If you want to read more to understand the parameters go to the Trainer API <a href="https://huggingface.co/docs/transformers/main_classes/trainer">docs</a>. It depends a bit on what gear you are training this on.</p><p>If you&#8217;re using a large dataset here it may take up to two hours, but if you&#8217;re using this small dataset of 8,500 rows then this will be a 10 minute ordeal.</p><p>Once you start the process, what we&#8217;re looking for here is overfitting. The training loss should <strong>consistently</strong> decrease, whereas the <strong>validation loss</strong> may fluctuate but <strong>should decrease at the end.</strong></p><p>Overfitting means that the model is fitting too well to our data which means it may not be able to generate unique answers. This will enable it to only generate output of data it has already been given which we do not want.</p><p>Ask the <a href="https://chat.openai.com/g/g-IlWD2J8i9-huggingface-helper">Hugging Face GPT</a> about your parameters and results, it can help you interpret them.</p><p>Once it is done, save the model. Make sure you set the name you want.</p><pre><code><code>trainer.save_model(&#8217;tech-keywords-extractor&#8217;)</code></code></pre><h3>Testing</h3><p>I did not set up any evaluation metrics in this run, which is not recommended, see my other <a href="https://medium.com/towards-data-science/fine-tune-smaller-transformer-models-text-classification-77cbbd3bf02b">article</a> for help in setting this up though and to understand what means what.</p><p>For this model, I&#8217;ll just test it manually to see how it does on our test set.</p><pre><code><code>from transformers import pipeline

pipe = pipeline(&#8217;summarization&#8217;, model=&#8217;tech-keywords-extractor&#8217;)

test_text=dataset[&#8217;test&#8217;][0][&#8217;text&#8217;]
keywords = dataset[&#8217;test&#8217;][0][&#8217;keywords&#8217;]
print(&#8221;the text: &#8220;, text_test)
print(&#8221;generated keywords: &#8220;, pipe(test_text))
print(&#8221;orginal keywords : &#8220;,keywords)</code></code></pre><p>You can also iterate over several examples so you can look them over one by one to see how they&#8217;re doing.</p><pre><code><code>for i in range(0, 50):
    text_test = dataset[&#8217;test&#8217;][i][&#8217;text&#8217;]
    keywords = dataset[&#8217;test&#8217;][i][&#8217;keywords&#8217;]
    print(&#8221;text: &#8220;, text_test)
    print(&#8221;generated keywords: &#8220;, pipe(text_test)[0][&#8217;summary_text&#8217;])
    print(&#8221;original keywords: &#8220;, keywords)</code></code></pre><p>Here it is good to do some more proper testing but I didn&#8217;t. I was decently satisfied.</p><h3>Push to Hugging Face</h3><p>So if you&#8217;re ready you can push it to the Hugging Face hub to save it for future use.</p><p>To log in you&#8217;ll need to navigate to Hugging Face and your account <strong>Settings</strong> to find <strong>Access Tokens.</strong></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!5Uub!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd0f57964-e762-4f86-b8ef-95d5ea1c04e5_1400x734.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!5Uub!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd0f57964-e762-4f86-b8ef-95d5ea1c04e5_1400x734.png 424w, /__u/substackcdn.com/image/fetch/$s_!5Uub!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd0f57964-e762-4f86-b8ef-95d5ea1c04e5_1400x734.png 848w, /__u/substackcdn.com/image/fetch/$s_!5Uub!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd0f57964-e762-4f86-b8ef-95d5ea1c04e5_1400x734.png 1272w, /__u/substackcdn.com/image/fetch/$s_!5Uub!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd0f57964-e762-4f86-b8ef-95d5ea1c04e5_1400x734.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!5Uub!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd0f57964-e762-4f86-b8ef-95d5ea1c04e5_1400x734.png" width="1400" height="734" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d0f57964-e762-4f86-b8ef-95d5ea1c04e5_1400x734.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:734,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!5Uub!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd0f57964-e762-4f86-b8ef-95d5ea1c04e5_1400x734.png 424w, /__u/substackcdn.com/image/fetch/$s_!5Uub!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd0f57964-e762-4f86-b8ef-95d5ea1c04e5_1400x734.png 848w, /__u/substackcdn.com/image/fetch/$s_!5Uub!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd0f57964-e762-4f86-b8ef-95d5ea1c04e5_1400x734.png 1272w, /__u/substackcdn.com/image/fetch/$s_!5Uub!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd0f57964-e762-4f86-b8ef-95d5ea1c04e5_1400x734.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Once you&#8217;ve found it, create a new <strong>write</strong> token and copy it. Use it when they ask you for your token.</p><pre><code><code>!huggingface-cli login</code></code></pre><p>After you&#8217;ve logged in you can simply set your username and the path you want your model to be pushed to. This will create a new model repository for you.</p><pre><code><code># you would replace your own name here
# you do not need to create a repository beforehand
trainer.push_to_hub(&#8221;your_hugging_face_username/tech-keywords-extractor&#8221;)</code></code></pre><p>We&#8217;re done.</p><p>If you&#8217;ve got stuck along the way, here is the full Colab <a href="https://github.com/ilsilfverskiold/smaller-models-docs/blob/main/nlp/cook/fine-tune/fine_tune_seqtoseq_tech_keywords_dataset.ipynb">notebook</a> for what we&#8217;ve done here. I didn&#8217;t run the whole thing because I already pushed my model once.</p><p>&#10084;</p>]]></content:encoded></item><item><title><![CDATA[Working with Embeddings: Closed versus Open Source]]></title><description><![CDATA[Using techniques to improve semantic search]]></description><link>https://howtouseai.substack.com/p/working-with-embeddings-closed-versus</link><guid isPermaLink="false">https://howtouseai.substack.com/p/working-with-embeddings-closed-versus</guid><dc:creator><![CDATA[Ida Silfverskiold]]></dc:creator><pubDate>Tue, 01 Sep 2026 08:01:33 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!gkHD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39a0277c-bb6c-41cd-a2c1-d86bd8b91ba2_1364x958.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!gkHD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39a0277c-bb6c-41cd-a2c1-d86bd8b91ba2_1364x958.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!gkHD!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39a0277c-bb6c-41cd-a2c1-d86bd8b91ba2_1364x958.png 424w, /__u/substackcdn.com/image/fetch/$s_!gkHD!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39a0277c-bb6c-41cd-a2c1-d86bd8b91ba2_1364x958.png 848w, /__u/substackcdn.com/image/fetch/$s_!gkHD!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39a0277c-bb6c-41cd-a2c1-d86bd8b91ba2_1364x958.png 1272w, /__u/substackcdn.com/image/fetch/$s_!gkHD!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39a0277c-bb6c-41cd-a2c1-d86bd8b91ba2_1364x958.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!gkHD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39a0277c-bb6c-41cd-a2c1-d86bd8b91ba2_1364x958.png" width="1364" height="958" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/39a0277c-bb6c-41cd-a2c1-d86bd8b91ba2_1364x958.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:958,&quot;width&quot;:1364,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!gkHD!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39a0277c-bb6c-41cd-a2c1-d86bd8b91ba2_1364x958.png 424w, /__u/substackcdn.com/image/fetch/$s_!gkHD!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39a0277c-bb6c-41cd-a2c1-d86bd8b91ba2_1364x958.png 848w, /__u/substackcdn.com/image/fetch/$s_!gkHD!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39a0277c-bb6c-41cd-a2c1-d86bd8b91ba2_1364x958.png 1272w, /__u/substackcdn.com/image/fetch/$s_!gkHD!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F39a0277c-bb6c-41cd-a2c1-d86bd8b91ba2_1364x958.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><em>This is an article dated September 2024 but has been re-published here.</em></p><p>Embeddings are a cornerstone of natural language processing. You can do quite a lot with embeddings, but one of the more popular uses is semantic search used in retrieval applications.</p><p>Although the entire tech community is abuzz with understanding how knowledge graph retrieval pipelines work, using standard vector retrieval isn&#8217;t out of style.</p><p>You&#8217;ll find multiple articles showing you how to filter out irrelevant results from semantic searches, something we&#8217;ll also be focusing on here using techniques such as clustering and re-ranking.</p><p>The <strong>main focus </strong>of this article,<strong> </strong>though, is to <strong>compare open source and closed source embedding models</strong> of various sizes.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!0wJW!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7bfa2e93-d917-4342-a054-f0cfea88cbcf_2000x670.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!0wJW!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7bfa2e93-d917-4342-a054-f0cfea88cbcf_2000x670.png 424w, /__u/substackcdn.com/image/fetch/$s_!0wJW!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7bfa2e93-d917-4342-a054-f0cfea88cbcf_2000x670.png 848w, /__u/substackcdn.com/image/fetch/$s_!0wJW!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7bfa2e93-d917-4342-a054-f0cfea88cbcf_2000x670.png 1272w, /__u/substackcdn.com/image/fetch/$s_!0wJW!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7bfa2e93-d917-4342-a054-f0cfea88cbcf_2000x670.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!0wJW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7bfa2e93-d917-4342-a054-f0cfea88cbcf_2000x670.png" width="1456" height="488" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7bfa2e93-d917-4342-a054-f0cfea88cbcf_2000x670.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:488,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!0wJW!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7bfa2e93-d917-4342-a054-f0cfea88cbcf_2000x670.png 424w, /__u/substackcdn.com/image/fetch/$s_!0wJW!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7bfa2e93-d917-4342-a054-f0cfea88cbcf_2000x670.png 848w, /__u/substackcdn.com/image/fetch/$s_!0wJW!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7bfa2e93-d917-4342-a054-f0cfea88cbcf_2000x670.png 1272w, /__u/substackcdn.com/image/fetch/$s_!0wJW!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7bfa2e93-d917-4342-a054-f0cfea88cbcf_2000x670.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">The models that will be in focus &#8212; there are many more available</figcaption></figure></div><p>We will compare up to 9 different embedding models that are high on the MTEB <a href="https://huggingface.co/spaces/mteb/leaderboard">leaderboard</a>. This will give you an idea of how a large versus a small model can perform and what the costs would be as you scale.</p><blockquote><p>If you&#8217;ve ever used OpenAI&#8217;s models to generate embeddings, you&#8217;ve probably been curious to see if they are competitive enough.</p></blockquote><p>Just as a quick recap on embeddings, if they&#8217;re new to you: when we create embeddings &#8212; an array of vectors &#8212; for each text, this translates into something a computer can understand.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!LR02!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63409b20-6812-40df-8ff9-14788d0c4433_1352x496.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!LR02!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63409b20-6812-40df-8ff9-14788d0c4433_1352x496.png 424w, /__u/substackcdn.com/image/fetch/$s_!LR02!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63409b20-6812-40df-8ff9-14788d0c4433_1352x496.png 848w, /__u/substackcdn.com/image/fetch/$s_!LR02!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63409b20-6812-40df-8ff9-14788d0c4433_1352x496.png 1272w, /__u/substackcdn.com/image/fetch/$s_!LR02!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63409b20-6812-40df-8ff9-14788d0c4433_1352x496.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!LR02!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63409b20-6812-40df-8ff9-14788d0c4433_1352x496.png" width="1352" height="496" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/63409b20-6812-40df-8ff9-14788d0c4433_1352x496.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:496,&quot;width&quot;:1352,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!LR02!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63409b20-6812-40df-8ff9-14788d0c4433_1352x496.png 424w, /__u/substackcdn.com/image/fetch/$s_!LR02!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63409b20-6812-40df-8ff9-14788d0c4433_1352x496.png 848w, /__u/substackcdn.com/image/fetch/$s_!LR02!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63409b20-6812-40df-8ff9-14788d0c4433_1352x496.png 1272w, /__u/substackcdn.com/image/fetch/$s_!LR02!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63409b20-6812-40df-8ff9-14788d0c4433_1352x496.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Text to embeddings</figcaption></figure></div><p>Specifically for semantic search, we compare the embeddings for different texts to see how much semantic similarity there is between them. This allows us to use a kind of fuzzy search with a query &#8212; i.e., searching for relationships &#8212; rather than an exact keyword match.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!yT3Z!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fe88e80-d65c-4a97-b8aa-e73c19a32b0e_1400x512.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!yT3Z!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fe88e80-d65c-4a97-b8aa-e73c19a32b0e_1400x512.png 424w, /__u/substackcdn.com/image/fetch/$s_!yT3Z!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fe88e80-d65c-4a97-b8aa-e73c19a32b0e_1400x512.png 848w, /__u/substackcdn.com/image/fetch/$s_!yT3Z!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fe88e80-d65c-4a97-b8aa-e73c19a32b0e_1400x512.png 1272w, /__u/substackcdn.com/image/fetch/$s_!yT3Z!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fe88e80-d65c-4a97-b8aa-e73c19a32b0e_1400x512.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!yT3Z!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fe88e80-d65c-4a97-b8aa-e73c19a32b0e_1400x512.png" width="1400" height="512" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3fe88e80-d65c-4a97-b8aa-e73c19a32b0e_1400x512.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:512,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!yT3Z!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fe88e80-d65c-4a97-b8aa-e73c19a32b0e_1400x512.png 424w, /__u/substackcdn.com/image/fetch/$s_!yT3Z!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fe88e80-d65c-4a97-b8aa-e73c19a32b0e_1400x512.png 848w, /__u/substackcdn.com/image/fetch/$s_!yT3Z!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fe88e80-d65c-4a97-b8aa-e73c19a32b0e_1400x512.png 1272w, /__u/substackcdn.com/image/fetch/$s_!yT3Z!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fe88e80-d65c-4a97-b8aa-e73c19a32b0e_1400x512.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Semantic similarity of the query embedding to the other embeddings</figcaption></figure></div><p>I will go through embeddings and how they work in the introduction section, especially focusing on how we calculate semantic similarity.</p><p>I always use a custom case when I write, and this time is no different. I got this idea from a consultancy owner who was asking if he could create an application that would match job descriptions to LinkedIn profiles.</p><p>If we were doing this for real, we would be using millions of user profiles, but for this piece, I have created 6,900 synthetic LinkedIn profiles.</p><p>You can find the dataset <a href="https://huggingface.co/datasets/ilsilfverskiold/linkedin_profiles_synthetic">here</a>.</p><p>This is generally an easy use case as we&#8217;re not chunking up documents in a large file; the profiles will fit into one chunk each. The domain is not a difficult one, as it is easy for us to understand if a model is finding the right relationships.</p><p>But it will give you an idea of how to think about solving a similar problem.</p><p>If you want to go straight to experimenting with the different models with the LinkedIn dataset, you can scroll past the introduction.</p><h2>Introduction</h2><p>For this article, as we have so few profiles, we can use clustering as a kind of unsupervised classification method for the entire dataset.</p><p>See an illustration of what clustering looks like below.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!GkmN!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4578bc1-68e5-40d7-baa3-23fdee3924e2_1400x904.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!GkmN!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4578bc1-68e5-40d7-baa3-23fdee3924e2_1400x904.png 424w, /__u/substackcdn.com/image/fetch/$s_!GkmN!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4578bc1-68e5-40d7-baa3-23fdee3924e2_1400x904.png 848w, /__u/substackcdn.com/image/fetch/$s_!GkmN!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4578bc1-68e5-40d7-baa3-23fdee3924e2_1400x904.png 1272w, /__u/substackcdn.com/image/fetch/$s_!GkmN!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4578bc1-68e5-40d7-baa3-23fdee3924e2_1400x904.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!GkmN!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4578bc1-68e5-40d7-baa3-23fdee3924e2_1400x904.png" width="1400" height="904" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d4578bc1-68e5-40d7-baa3-23fdee3924e2_1400x904.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:904,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!GkmN!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4578bc1-68e5-40d7-baa3-23fdee3924e2_1400x904.png 424w, /__u/substackcdn.com/image/fetch/$s_!GkmN!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4578bc1-68e5-40d7-baa3-23fdee3924e2_1400x904.png 848w, /__u/substackcdn.com/image/fetch/$s_!GkmN!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4578bc1-68e5-40d7-baa3-23fdee3924e2_1400x904.png 1272w, /__u/substackcdn.com/image/fetch/$s_!GkmN!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd4578bc1-68e5-40d7-baa3-23fdee3924e2_1400x904.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Simplified illustration of 4 clusters for the LinkedIn profiles</figcaption></figure></div><p>Clustering will also let us understand how the different models perceives connected relationships.</p><p>Depending on the model, we can then isolate the correct group before performing semantic search within the cluster.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!s3Le!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d3be501-79b2-4b3e-95f2-7daddf142094_1400x763.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!s3Le!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d3be501-79b2-4b3e-95f2-7daddf142094_1400x763.png 424w, /__u/substackcdn.com/image/fetch/$s_!s3Le!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d3be501-79b2-4b3e-95f2-7daddf142094_1400x763.png 848w, /__u/substackcdn.com/image/fetch/$s_!s3Le!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d3be501-79b2-4b3e-95f2-7daddf142094_1400x763.png 1272w, /__u/substackcdn.com/image/fetch/$s_!s3Le!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d3be501-79b2-4b3e-95f2-7daddf142094_1400x763.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!s3Le!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d3be501-79b2-4b3e-95f2-7daddf142094_1400x763.png" width="1400" height="763" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2d3be501-79b2-4b3e-95f2-7daddf142094_1400x763.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:763,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!s3Le!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d3be501-79b2-4b3e-95f2-7daddf142094_1400x763.png 424w, /__u/substackcdn.com/image/fetch/$s_!s3Le!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d3be501-79b2-4b3e-95f2-7daddf142094_1400x763.png 848w, /__u/substackcdn.com/image/fetch/$s_!s3Le!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d3be501-79b2-4b3e-95f2-7daddf142094_1400x763.png 1272w, /__u/substackcdn.com/image/fetch/$s_!s3Le!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d3be501-79b2-4b3e-95f2-7daddf142094_1400x763.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Simplified image of matching our query to the correct cluster</figcaption></figure></div><p>This should allow us to filter out irrelevant results, such as the model confusing product managers with product marketing managers.</p><p>You can add re-ranking with an LLM as a last step to make sure the top results wind up on top.</p><p>To make things very easy and less price-y, I have already added the embeddings for each model we&#8217;ll be evaluating in this <a href="https://huggingface.co/datasets/ilsilfverskiold/linkedin_profiles_synthetic">dataset</a>, I have also created <a href="https://huggingface.co/datasets/ilsilfverskiold/linkedin_recruitment_questions_embedded">embeddings</a> for our queries, i.e., our anonymous job descriptions.</p><p>Remember if you want to go straight to experimenting you can scroll down to the <strong>the use case</strong>, although don&#8217;t skip the economics part.</p><h3>Embeddings</h3><p>I mentioned that embeddings are numerical representations of texts that capture their meaning, allowing computers to process and understand natural language.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!LR02!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63409b20-6812-40df-8ff9-14788d0c4433_1352x496.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!LR02!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63409b20-6812-40df-8ff9-14788d0c4433_1352x496.png 424w, /__u/substackcdn.com/image/fetch/$s_!LR02!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63409b20-6812-40df-8ff9-14788d0c4433_1352x496.png 848w, /__u/substackcdn.com/image/fetch/$s_!LR02!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63409b20-6812-40df-8ff9-14788d0c4433_1352x496.png 1272w, /__u/substackcdn.com/image/fetch/$s_!LR02!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63409b20-6812-40df-8ff9-14788d0c4433_1352x496.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!LR02!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63409b20-6812-40df-8ff9-14788d0c4433_1352x496.png" width="1352" height="496" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/63409b20-6812-40df-8ff9-14788d0c4433_1352x496.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:496,&quot;width&quot;:1352,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!LR02!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63409b20-6812-40df-8ff9-14788d0c4433_1352x496.png 424w, /__u/substackcdn.com/image/fetch/$s_!LR02!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63409b20-6812-40df-8ff9-14788d0c4433_1352x496.png 848w, /__u/substackcdn.com/image/fetch/$s_!LR02!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63409b20-6812-40df-8ff9-14788d0c4433_1352x496.png 1272w, /__u/substackcdn.com/image/fetch/$s_!LR02!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63409b20-6812-40df-8ff9-14788d0c4433_1352x496.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Text to embeddings</figcaption></figure></div><p>With the more modern transformer models, these models can understand the entire context and thus understand several meanings of words and sentences &#8212; something that just wasn&#8217;t true a few years ago.</p><p>We can actually visualize embeddings on a graph by representing them as points in geometric space. Semantic relationships between embeddings thus translate into geometric closeness.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!GyHZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97dbdab6-d3fc-4110-84de-518b2b74b383_1366x962.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!GyHZ!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97dbdab6-d3fc-4110-84de-518b2b74b383_1366x962.png 424w, /__u/substackcdn.com/image/fetch/$s_!GyHZ!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97dbdab6-d3fc-4110-84de-518b2b74b383_1366x962.png 848w, /__u/substackcdn.com/image/fetch/$s_!GyHZ!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97dbdab6-d3fc-4110-84de-518b2b74b383_1366x962.png 1272w, /__u/substackcdn.com/image/fetch/$s_!GyHZ!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97dbdab6-d3fc-4110-84de-518b2b74b383_1366x962.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!GyHZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97dbdab6-d3fc-4110-84de-518b2b74b383_1366x962.png" width="1366" height="962" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/97dbdab6-d3fc-4110-84de-518b2b74b383_1366x962.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:962,&quot;width&quot;:1366,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!GyHZ!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97dbdab6-d3fc-4110-84de-518b2b74b383_1366x962.png 424w, /__u/substackcdn.com/image/fetch/$s_!GyHZ!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97dbdab6-d3fc-4110-84de-518b2b74b383_1366x962.png 848w, /__u/substackcdn.com/image/fetch/$s_!GyHZ!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97dbdab6-d3fc-4110-84de-518b2b74b383_1366x962.png 1272w, /__u/substackcdn.com/image/fetch/$s_!GyHZ!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97dbdab6-d3fc-4110-84de-518b2b74b383_1366x962.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Embeddings on a graph as points in geometric space</figcaption></figure></div><p>Different models are built for different tasks, but most of the larger ones are generalist enough to perform various tasks, such as<strong> retrieval</strong>, <strong>clustering</strong>, and <strong>classification</strong>.</p><p><strong>Semantic search</strong>, used in retrieval, uses this closeness on the graph to figure out where a query would match with the other embeddings, i.e., it computes the distance of the embeddings on the graph.</p><p>To calculate this similarity between embeddings in semantic search, several methods are used, but cosine similarity is the most popular.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!SvfK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9679e03c-fb12-4e4c-8265-500ba35cedbe_1400x471.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!SvfK!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9679e03c-fb12-4e4c-8265-500ba35cedbe_1400x471.png 424w, /__u/substackcdn.com/image/fetch/$s_!SvfK!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9679e03c-fb12-4e4c-8265-500ba35cedbe_1400x471.png 848w, /__u/substackcdn.com/image/fetch/$s_!SvfK!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9679e03c-fb12-4e4c-8265-500ba35cedbe_1400x471.png 1272w, /__u/substackcdn.com/image/fetch/$s_!SvfK!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9679e03c-fb12-4e4c-8265-500ba35cedbe_1400x471.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!SvfK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9679e03c-fb12-4e4c-8265-500ba35cedbe_1400x471.png" width="1400" height="471" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9679e03c-fb12-4e4c-8265-500ba35cedbe_1400x471.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:471,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!SvfK!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9679e03c-fb12-4e4c-8265-500ba35cedbe_1400x471.png 424w, /__u/substackcdn.com/image/fetch/$s_!SvfK!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9679e03c-fb12-4e4c-8265-500ba35cedbe_1400x471.png 848w, /__u/substackcdn.com/image/fetch/$s_!SvfK!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9679e03c-fb12-4e4c-8265-500ba35cedbe_1400x471.png 1272w, /__u/substackcdn.com/image/fetch/$s_!SvfK!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9679e03c-fb12-4e4c-8265-500ba35cedbe_1400x471.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Semantic similarity to find best match</figcaption></figure></div><p>The model we use will directly affect the results you get from performing semantic search and this is a result of how it has been trained.</p><p>It matters what datasets, objectives, and architectures models are trained with, as it will influence how well it understand and link various texts.</p><p><strong>Clustering</strong>, on the other hand, organizes data into groups (or clusters) where items are more similar to each other than to those in other groups. It is better at identifying and matching similarities between embeddings, allowing us to effectively isolate the group.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!CwPf!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71df7b15-4925-4935-8500-59f8ebe4864a_1300x922.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!CwPf!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71df7b15-4925-4935-8500-59f8ebe4864a_1300x922.png 424w, /__u/substackcdn.com/image/fetch/$s_!CwPf!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71df7b15-4925-4935-8500-59f8ebe4864a_1300x922.png 848w, /__u/substackcdn.com/image/fetch/$s_!CwPf!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71df7b15-4925-4935-8500-59f8ebe4864a_1300x922.png 1272w, /__u/substackcdn.com/image/fetch/$s_!CwPf!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71df7b15-4925-4935-8500-59f8ebe4864a_1300x922.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!CwPf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71df7b15-4925-4935-8500-59f8ebe4864a_1300x922.png" width="1300" height="922" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/71df7b15-4925-4935-8500-59f8ebe4864a_1300x922.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:922,&quot;width&quot;:1300,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!CwPf!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71df7b15-4925-4935-8500-59f8ebe4864a_1300x922.png 424w, /__u/substackcdn.com/image/fetch/$s_!CwPf!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71df7b15-4925-4935-8500-59f8ebe4864a_1300x922.png 848w, /__u/substackcdn.com/image/fetch/$s_!CwPf!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71df7b15-4925-4935-8500-59f8ebe4864a_1300x922.png 1272w, /__u/substackcdn.com/image/fetch/$s_!CwPf!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71df7b15-4925-4935-8500-59f8ebe4864a_1300x922.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Clustering embeddings to group similar profiles and to visualize the query</figcaption></figure></div><p>This process allows us to first filter out any irrelevant matches before performing semantic search and thus acts as a noise reduction tactic.</p><p>This is the idea, at least.</p><p>Not all models will be able to use clustering in the way we need them to; some will be better at it and some worse based on how they have been built.</p><h3>Embedding Models</h3><p>So, how do you know which model to pick? In comes the <a href="https://huggingface.co/spaces/mteb/leaderboard">MTEB</a> leaderboard that ranks embedding models based on their performance across various tasks.</p><p>I have picked out a few of these models that we&#8217;ll test for this, from the more popular models from OpenAI to compare with a fine-tuned Mistral-7B and smaller newer models such as <a href="https://huggingface.co/mixedbread-ai/mxbai-embed-large-v1">Mxbai</a> from Mixedbread AI.</p><p>They have all been released in the last two years, more or less.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!YN0j!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F154a7481-5077-4fad-aac3-6c8d87b7d2cb_2000x766.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!YN0j!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F154a7481-5077-4fad-aac3-6c8d87b7d2cb_2000x766.png 424w, /__u/substackcdn.com/image/fetch/$s_!YN0j!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F154a7481-5077-4fad-aac3-6c8d87b7d2cb_2000x766.png 848w, /__u/substackcdn.com/image/fetch/$s_!YN0j!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F154a7481-5077-4fad-aac3-6c8d87b7d2cb_2000x766.png 1272w, /__u/substackcdn.com/image/fetch/$s_!YN0j!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F154a7481-5077-4fad-aac3-6c8d87b7d2cb_2000x766.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!YN0j!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F154a7481-5077-4fad-aac3-6c8d87b7d2cb_2000x766.png" width="1456" height="558" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/154a7481-5077-4fad-aac3-6c8d87b7d2cb_2000x766.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:558,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!YN0j!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F154a7481-5077-4fad-aac3-6c8d87b7d2cb_2000x766.png 424w, /__u/substackcdn.com/image/fetch/$s_!YN0j!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F154a7481-5077-4fad-aac3-6c8d87b7d2cb_2000x766.png 848w, /__u/substackcdn.com/image/fetch/$s_!YN0j!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F154a7481-5077-4fad-aac3-6c8d87b7d2cb_2000x766.png 1272w, /__u/substackcdn.com/image/fetch/$s_!YN0j!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F154a7481-5077-4fad-aac3-6c8d87b7d2cb_2000x766.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Our selection of models we&#8217;ll use for this article &#8212; this list is not definite</figcaption></figure></div><p>If you&#8217;re new to open source models, you may be surprised to see that many open source models rank quite highly. If you&#8217;re not new to trying these models, it may still be interesting to see which one did best for this task.</p><p>Look at the table below to see the size, max tokens, and the ranking for retrieval and clustering for each model.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!Zd_G!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9b56250-3e8e-430d-917d-8ac144a6dc4c_1400x818.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!Zd_G!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9b56250-3e8e-430d-917d-8ac144a6dc4c_1400x818.png 424w, /__u/substackcdn.com/image/fetch/$s_!Zd_G!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9b56250-3e8e-430d-917d-8ac144a6dc4c_1400x818.png 848w, /__u/substackcdn.com/image/fetch/$s_!Zd_G!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9b56250-3e8e-430d-917d-8ac144a6dc4c_1400x818.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Zd_G!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9b56250-3e8e-430d-917d-8ac144a6dc4c_1400x818.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!Zd_G!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9b56250-3e8e-430d-917d-8ac144a6dc4c_1400x818.png" width="1400" height="818" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b9b56250-3e8e-430d-917d-8ac144a6dc4c_1400x818.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:818,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!Zd_G!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9b56250-3e8e-430d-917d-8ac144a6dc4c_1400x818.png 424w, /__u/substackcdn.com/image/fetch/$s_!Zd_G!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9b56250-3e8e-430d-917d-8ac144a6dc4c_1400x818.png 848w, /__u/substackcdn.com/image/fetch/$s_!Zd_G!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9b56250-3e8e-430d-917d-8ac144a6dc4c_1400x818.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Zd_G!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9b56250-3e8e-430d-917d-8ac144a6dc4c_1400x818.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">MTEB Leaderboard metrics for various models we&#8217;ll use</figcaption></figure></div><p>Many have used <a href="https://openai.com/index/new-and-improved-embedding-model/">Ada-002</a> from OpenAI, and which is at the bottom of our list with respect to all the other models. OpenAI have though released text-embed-3 in both small and large sizes that perform better and is cheaper as well.</p><p>So, you may ask yourself, why would someone use a commercial model when they can just use an open-source model that&#8217;s high on the leaderboard?</p><h3>Economics of Open Source Models</h3><p>Using an open source model certainly sounds good and is the preferred privacy choice. Many are high on the leaderboard, but you do need to consider the economics of hosting a model versus using an API.</p><p>I looked at the cost of hosting both smaller (around 350M) and larger (7B) open source models on a GPU versus paying per token for a few popular commercial models.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!Vt5Q!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff777b2aa-902d-4d5f-b448-5e4b17307edf_1078x802.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!Vt5Q!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff777b2aa-902d-4d5f-b448-5e4b17307edf_1078x802.png 424w, /__u/substackcdn.com/image/fetch/$s_!Vt5Q!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff777b2aa-902d-4d5f-b448-5e4b17307edf_1078x802.png 848w, /__u/substackcdn.com/image/fetch/$s_!Vt5Q!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff777b2aa-902d-4d5f-b448-5e4b17307edf_1078x802.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Vt5Q!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff777b2aa-902d-4d5f-b448-5e4b17307edf_1078x802.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!Vt5Q!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff777b2aa-902d-4d5f-b448-5e4b17307edf_1078x802.png" width="1078" height="802" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f777b2aa-902d-4d5f-b448-5e4b17307edf_1078x802.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:802,&quot;width&quot;:1078,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!Vt5Q!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff777b2aa-902d-4d5f-b448-5e4b17307edf_1078x802.png 424w, /__u/substackcdn.com/image/fetch/$s_!Vt5Q!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff777b2aa-902d-4d5f-b448-5e4b17307edf_1078x802.png 848w, /__u/substackcdn.com/image/fetch/$s_!Vt5Q!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff777b2aa-902d-4d5f-b448-5e4b17307edf_1078x802.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Vt5Q!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff777b2aa-902d-4d5f-b448-5e4b17307edf_1078x802.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Computational costs per 10k, 100k, 1m and 2.5m texts &#8212; not including storage</figcaption></figure></div><p><em>The assumption here is that each text is 400 tokens, thus a 334M model will be able to process up to 75&#8211;90 texts per second on a single L4 GPU, and a 7B model will process around 30-40 texts per second with a single A100 but maybe more.</em></p><p>As you&#8217;ll observe, using a model like text-embed-3-large or ada-002 will really add up once you start to embed millions of texts. This is not including storage.</p><p>If you&#8217;re an enterprise client and you&#8217;re looking into Nvidia&#8217;s embedding models, such as nv-embed-v1, they offer quite a good <a href="https://build.nvidia.com/nvidia/nv-embed-v1">API</a> that you can tap into. I&#8217;ve used it to test a few of these models.</p><p>Using a small model though, less than 500 parameters, is certainly the most sound choice. If you can go with a smaller open source model, you should do so as you can slash your compute costs by up to 90%.</p><p>I also went ahead and calculated the processing times for smaller and larger models, if you were to host them on a single GPU.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!vJ-R!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8010e35-bfa2-48fa-95f1-8a9ee2cd6261_1400x721.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!vJ-R!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8010e35-bfa2-48fa-95f1-8a9ee2cd6261_1400x721.png 424w, /__u/substackcdn.com/image/fetch/$s_!vJ-R!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8010e35-bfa2-48fa-95f1-8a9ee2cd6261_1400x721.png 848w, /__u/substackcdn.com/image/fetch/$s_!vJ-R!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8010e35-bfa2-48fa-95f1-8a9ee2cd6261_1400x721.png 1272w, /__u/substackcdn.com/image/fetch/$s_!vJ-R!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8010e35-bfa2-48fa-95f1-8a9ee2cd6261_1400x721.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!vJ-R!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8010e35-bfa2-48fa-95f1-8a9ee2cd6261_1400x721.png" width="1400" height="721" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e8010e35-bfa2-48fa-95f1-8a9ee2cd6261_1400x721.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:721,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!vJ-R!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8010e35-bfa2-48fa-95f1-8a9ee2cd6261_1400x721.png 424w, /__u/substackcdn.com/image/fetch/$s_!vJ-R!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8010e35-bfa2-48fa-95f1-8a9ee2cd6261_1400x721.png 848w, /__u/substackcdn.com/image/fetch/$s_!vJ-R!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8010e35-bfa2-48fa-95f1-8a9ee2cd6261_1400x721.png 1272w, /__u/substackcdn.com/image/fetch/$s_!vJ-R!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8010e35-bfa2-48fa-95f1-8a9ee2cd6261_1400x721.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Estimated processing times with one GPU</figcaption></figure></div><p>Calling an API will also take time, and they have inference limits, so regardless of what choice you make, you&#8217;ll have to consider the amount of time it takes to fully embed an entire dataset.</p><p>For the open source models, you can always use more GPUs to process, but it gives you an idea of how using something smaller may be more energy-efficient.</p><p>It&#8217;s always good to test a few to see which model performs well for your task though as well, which is what we will do in a bit.</p><h3>Quantization</h3><p>As you saw above, using larger models, such as those around 7B, is still quite expensive and energy-intensive. We only calculated costs for 2.5 million embeddings, but once you start to scale further, it may be worthwhile to look into <a href="https://huggingface.co/blog/embedding-quantization">quantization</a>.</p><p>Quantization compresses a model by using fewer bits to represent its data, which decreases the model&#8217;s size. The idea is that using quantization techniques like 4-bit and 8-bit quantization will help to run larger models on hardware that would normally not be able to handle such large models.</p><p>There have been a few people who have tried to measure the performance decrease of various metrics on quantized models; I think the last one I saw argued for a 12% overall drop in performance.</p><p>I will have to write a bit about this in the future, I love to look at the economics and performance cuts of these things.</p><h2>The Use Case</h2><p>I don&#8217;t know about you, but I like to test different models rather than just look at metrics. This gives me a sense of how smaller and larger models can interpret relationships between texts.</p><p>The dataset with the synthetic LinkedIn profiles you can find <a href="https://huggingface.co/datasets/ilsilfverskiold/linkedin_profiles_synthetic">here</a>, along with the <a href="https://huggingface.co/datasets/ilsilfverskiold/linkedin_recruitment_questions_embedded">dataset</a> with our job descriptions that should be matched.</p><p>The Colab notebook we will work in you can find <a href="https://colab.research.google.com/gist/ilsilfverskiold/4214061d3ffd64479067e6e0dded3ffc/visualize_different_embedding_models.ipynb">here</a>.</p><h3>Importing the Data</h3><p>You need to open the <a href="https://colab.research.google.com/gist/ilsilfverskiold/4214061d3ffd64479067e6e0dded3ffc/visualize_different_embedding_models.ipynb">notebook</a> to follow along, but once you have done so, you should see that we&#8217;re importing two datasets from Hugging Face.</p><pre><code># Synthetic LinkedIn profiles with the embeddings
dataset = load_dataset(&#8221;ilsilfverskiold/linkedin_profiles_synthetic&#8221;)
profiles = dataset[&#8217;train&#8217;]

# Anonymous job descriptions with embeddings
dataset = load_dataset(&#8221;ilsilfverskiold/linkedin_recruitment_questions_embedded&#8221;)
applications = dataset[&#8217;train&#8217;]</code></pre><p>These two datasets will allow us to compare the different embedding models for the 6,900 LinkedIn profiles that have been generated.</p><p>The synthetic data is, well, synthetic, so take it with a grain of salt. It was created with Llama 3.1 and it does suffer from some great alignment where it describes profiles using words such as &#8216;results-driven,&#8217; &#8216;seasoned,&#8217; and &#8216;dedicated.&#8217;</p><p>The embeddings have already been added, which you&#8217;ll see if you look into the &#8216;profiles.&#8217;</p><pre><code># profiles dataset
Dataset({
    features: [...,&#8217;embeddings_nv-embed-v1&#8217;, &#8216;embeddings_nv-embedqa-e5-v5&#8217;, &#8216;embeddings_bge-m3&#8217;, &#8216;embeddings_arctic-embed-l&#8217;, &#8216;embeddings_mistral-7b-v2&#8217;, &#8216;embeddings_gte-large-en-v1.5&#8217;, &#8216;embeddings_text-embedding-ada-002&#8217;, &#8216;embeddings_text-embedding-3-small&#8217;, &#8216;embeddings_voyage-3&#8217;, &#8216;embeddings_mxbai-embed-large-v1 &#8216;],
    num_rows: 6904
})</code></pre><p><em>Ps. embeddings_gte-large-en-v1.5 does not work. I tried to host it but failed to set all the embeddings for it so do not use it.</em></p><p>From here, you need to decide on the job description you are interested in matching to the profiles.</p><p>Look at the code below; I have picked the second application, but you can set another number.</p><pre><code>application = applications[1] # deciding on the second application - a product marketing manager position 
application_text = application[&#8217;natural_language&#8217;]
print(&#8221;application we&#8217;re looking for: &#8220;,application_text)</code></pre><p>Check the <a href="https://huggingface.co/datasets/ilsilfverskiold/linkedin_recruitment_questions_embedded">dataset</a> directly on Hugging Face if that is easier.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!y06j!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe7002b73-5c3a-4394-be51-23150a94e87a_1400x897.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!y06j!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe7002b73-5c3a-4394-be51-23150a94e87a_1400x897.png 424w, /__u/substackcdn.com/image/fetch/$s_!y06j!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe7002b73-5c3a-4394-be51-23150a94e87a_1400x897.png 848w, /__u/substackcdn.com/image/fetch/$s_!y06j!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe7002b73-5c3a-4394-be51-23150a94e87a_1400x897.png 1272w, /__u/substackcdn.com/image/fetch/$s_!y06j!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe7002b73-5c3a-4394-be51-23150a94e87a_1400x897.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!y06j!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe7002b73-5c3a-4394-be51-23150a94e87a_1400x897.png" width="1400" height="897" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e7002b73-5c3a-4394-be51-23150a94e87a_1400x897.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:897,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!y06j!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe7002b73-5c3a-4394-be51-23150a94e87a_1400x897.png 424w, /__u/substackcdn.com/image/fetch/$s_!y06j!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe7002b73-5c3a-4394-be51-23150a94e87a_1400x897.png 848w, /__u/substackcdn.com/image/fetch/$s_!y06j!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe7002b73-5c3a-4394-be51-23150a94e87a_1400x897.png 1272w, /__u/substackcdn.com/image/fetch/$s_!y06j!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe7002b73-5c3a-4394-be51-23150a94e87a_1400x897.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">HuggingFace dataset viewer for the job applications dataset</figcaption></figure></div><p>From here, you can decide which embedding model you&#8217;d like to work with. I have already tested most of them, so I&#8217;ll use <code>embeddings_mxbai-embed-large-v1</code> for this run.</p><p>This is the 334M open-source model that ranked quite high on the leaderboard for both retrieval and clustering if you scroll up to the table I used earlier.</p><p>If you want to try a different model, you simply set another one. Look into the <a href="https://huggingface.co/datasets/ilsilfverskiold/linkedin_profiles_synthetic">dataset</a> mentioned above to see which ones you have access to.</p><pre><code># Get the query embeddings for an embedding model - in here we&#8217;re picking mxbai-embed-large-v1
query_embedding_vector = np.array(application[&#8217;embeddings_mxbai-embed-large-v1&#8217;])

embeddings_list = [np.array(emb) for emb in profiles[&#8217;embeddings_mxbai-embed-large-v1 &#8216;]] # note the extra space
texts = profiles[&#8217;text&#8217;]</code></pre><h3>Semantic Search</h3><p>We can try to perform semantic search before we try to cluster; this allows us to see how it can do before adding in anything else.</p><p>To calculate the semantic similarity between the profiles and our query &#8212; the job application &#8212; we run the code below.</p><pre><code># Let&#8217;s first try to calculate the cosine similarity (without clustering)
def cosine_similarity(a, b):
    a = np.array(a)
    b = np.array(b)
    return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))

similarities = []
for idx, emb in enumerate(embeddings_list):
    sim = cosine_similarity(query_embedding_vector, emb)
    similarities.append(sim)</code></pre><p>Then, we can display the similarity score by sorting the highest on top, limiting the display to the first 30 results.</p><pre><code>results = list(zip(range(1, len(texts) + 1), similarities, texts))
sorted_results = sorted(results, key=lambda x: x[1], reverse=True)

# Let&#8217;s display the results as well
print(&#8221;\nSimilarity Results (sorted from highest to lowest):&#8221;)
for idx, sim, text in sorted_results[:30]:  # adjust if you want to show more
    percentage = (sim + 1) / 2 * 100
    text_preview = &#8216; &#8216;.join(text.split()[:10])
    print(f&#8221;Text {idx} similarity: {percentage:.2f}% - Preview: {text_preview}...&#8221;)</code></pre><p>The results will look like something below, but it depends on the application you chose.</p><pre><code>Similarity Results (sorted from highest to lowest):
Text 3615 similarity: 89.59% - Preview: Product Marketing Manager | Building Go-to-Market Strategies for Growth Results-driven...
Text 6299 similarity: 89.56% - Preview: Product Marketing Manager | Driving Growth &amp; Customer Engagement Results-driven...
Text 3232 similarity: 89.09% - Preview: Product Marketing Manager | Driving Product Growth through Data-Driven Strategies...
Text 5959 similarity: 88.90% - Preview: Product Marketing Manager | Data-Driven Growth Expert Results-driven Product Marketing...
Text 5635 similarity: 88.84% - Preview: Product Marketing Manager | Driving Growth through Data-Driven Marketing Strategies...
Text 5835 similarity: 88.74% - Preview: Product Marketing Manager | Cloud-Based SaaS Results-driven Product Marketing Manager...
Text 139 similarity: 88.66% - Preview: Product Marketing Manager | Scaling Growth through Data-Driven Strategies Experienced...
Text 6688 similarity: 88.48% - Preview: Product Marketing Manager | Driving Business Growth through Data-Driven Insights...
Text 6405 similarity: 88.27% - Preview: Product Marketing Manager | Scaling SaaS Products for Global Markets...
Text 3439 similarity: 88.11% - Preview: Product Manager | Focused on delivering innovative products that drive...
Text 5958 similarity: 88.00% - Preview: Product Manager Office | Growth Driven by Customer Centricity Highly...
Text 5183 similarity: 87.86% - Preview: Product Marketing Manager | B2B SaaS Experienced Product Marketing Manager...
Text 1329 similarity: 87.81% - Preview: Product Marketing Manager | Scaling Growth for Emerging Tech Startups...
Text 130 similarity: 87.81% - Preview: Product Marketing Manager | Growth Strategies &amp; Launches Results-driven Product...
Text 3423 similarity: 87.78% - Preview: Product Marketing Manager | Scaling B2B SaaS Solutions Experienced Product...
Text 4234 similarity: 87.72% - Preview: Product Manager | Leading Cross-Functional Teams to Drive Business Growth...</code></pre><p>Using the mxbai embedding model, along with many others, we can clearly see that the results will return Product Marketing Managers with Product Managers, which is something that we do not want.</p><p><em>Look at the 88.11% &#8212; Preview: Product Manager and 88.00% &#8212; Preview: Product Manager Office above.</em></p><p>Let&#8217;s introduce clustering to see if it can help.</p><h3>Clustering</h3><p>First, we set up the clusters from the profile embeddings; here, we need to decide the amount of clusters.</p><p>I picked 10.</p><pre><code>embeddings_array = np.array(embeddings_list)

num_clusters = 10 # you can pick another number here

kmeans = KMeans(n_clusters=num_clusters, random_state=42)
kmeans.fit(embeddings_array)
cluster_labels = kmeans.labels_

pca = PCA(n_components=2)
reduced_embeddings = pca.fit_transform(embeddings_array)</code></pre><p>Then, we need to understand what cluster the query &#8212; or job application &#8212; will fit into.</p><pre><code># Let&#8217;s now see how query fits into the clustering
query_embedding_array = np.array(query_embedding_vector).reshape(1, -1)
reduced_query_embedding = pca.transform(query_embedding_array)

# Let&#8217;s also predict which cluster the query would belong to
query_cluster_label = kmeans.predict(query_embedding_array)[0]
print(f&#8221;The query belongs to cluster {query_cluster_label}&#8221;)</code></pre><p>After this, we can visualize the clusters on a 2-dimensional graph &#8212; remember that the clusters have been flattened, so they may sit on top of each other.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!G6aW!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fedcd5060-8dd8-48c4-8dc0-63290610ce03_1400x520.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!G6aW!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fedcd5060-8dd8-48c4-8dc0-63290610ce03_1400x520.png 424w, /__u/substackcdn.com/image/fetch/$s_!G6aW!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fedcd5060-8dd8-48c4-8dc0-63290610ce03_1400x520.png 848w, /__u/substackcdn.com/image/fetch/$s_!G6aW!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fedcd5060-8dd8-48c4-8dc0-63290610ce03_1400x520.png 1272w, /__u/substackcdn.com/image/fetch/$s_!G6aW!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fedcd5060-8dd8-48c4-8dc0-63290610ce03_1400x520.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!G6aW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fedcd5060-8dd8-48c4-8dc0-63290610ce03_1400x520.png" width="1400" height="520" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/edcd5060-8dd8-48c4-8dc0-63290610ce03_1400x520.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:520,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!G6aW!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fedcd5060-8dd8-48c4-8dc0-63290610ce03_1400x520.png 424w, /__u/substackcdn.com/image/fetch/$s_!G6aW!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fedcd5060-8dd8-48c4-8dc0-63290610ce03_1400x520.png 848w, /__u/substackcdn.com/image/fetch/$s_!G6aW!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fedcd5060-8dd8-48c4-8dc0-63290610ce03_1400x520.png 1272w, /__u/substackcdn.com/image/fetch/$s_!G6aW!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fedcd5060-8dd8-48c4-8dc0-63290610ce03_1400x520.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Clustering the LinkedIn profiles &#8212; Image from Colab notebook</figcaption></figure></div><p>You can hover over the different embeddings to see the profiles.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!UnZo!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8a91a051-d296-491d-ab97-e3cdfd8f55ad_1400x509.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!UnZo!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8a91a051-d296-491d-ab97-e3cdfd8f55ad_1400x509.png 424w, /__u/substackcdn.com/image/fetch/$s_!UnZo!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8a91a051-d296-491d-ab97-e3cdfd8f55ad_1400x509.png 848w, /__u/substackcdn.com/image/fetch/$s_!UnZo!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8a91a051-d296-491d-ab97-e3cdfd8f55ad_1400x509.png 1272w, /__u/substackcdn.com/image/fetch/$s_!UnZo!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8a91a051-d296-491d-ab97-e3cdfd8f55ad_1400x509.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!UnZo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8a91a051-d296-491d-ab97-e3cdfd8f55ad_1400x509.png" width="1400" height="509" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8a91a051-d296-491d-ab97-e3cdfd8f55ad_1400x509.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:509,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!UnZo!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8a91a051-d296-491d-ab97-e3cdfd8f55ad_1400x509.png 424w, /__u/substackcdn.com/image/fetch/$s_!UnZo!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8a91a051-d296-491d-ab97-e3cdfd8f55ad_1400x509.png 848w, /__u/substackcdn.com/image/fetch/$s_!UnZo!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8a91a051-d296-491d-ab97-e3cdfd8f55ad_1400x509.png 1272w, /__u/substackcdn.com/image/fetch/$s_!UnZo!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8a91a051-d296-491d-ab97-e3cdfd8f55ad_1400x509.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Looking at the 5th cluster&#8217;s embeddings &#8212; Image from Colab notebook</figcaption></figure></div><p>We can also isolate the query, our X, on the graph to see the cluster the model thinks it belongs to.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!rOj_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F984e8856-b9e8-4fcc-8c2a-3902da4168ac_1400x519.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!rOj_!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F984e8856-b9e8-4fcc-8c2a-3902da4168ac_1400x519.png 424w, /__u/substackcdn.com/image/fetch/$s_!rOj_!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F984e8856-b9e8-4fcc-8c2a-3902da4168ac_1400x519.png 848w, /__u/substackcdn.com/image/fetch/$s_!rOj_!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F984e8856-b9e8-4fcc-8c2a-3902da4168ac_1400x519.png 1272w, /__u/substackcdn.com/image/fetch/$s_!rOj_!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F984e8856-b9e8-4fcc-8c2a-3902da4168ac_1400x519.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!rOj_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F984e8856-b9e8-4fcc-8c2a-3902da4168ac_1400x519.png" width="1400" height="519" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/984e8856-b9e8-4fcc-8c2a-3902da4168ac_1400x519.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:519,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!rOj_!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F984e8856-b9e8-4fcc-8c2a-3902da4168ac_1400x519.png 424w, /__u/substackcdn.com/image/fetch/$s_!rOj_!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F984e8856-b9e8-4fcc-8c2a-3902da4168ac_1400x519.png 848w, /__u/substackcdn.com/image/fetch/$s_!rOj_!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F984e8856-b9e8-4fcc-8c2a-3902da4168ac_1400x519.png 1272w, /__u/substackcdn.com/image/fetch/$s_!rOj_!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F984e8856-b9e8-4fcc-8c2a-3902da4168ac_1400x519.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Isolating the 5th cluster with the query , X &#8212; Image from Colab notebook</figcaption></figure></div><p>We can clearly see that the model is correctly interpreting the marketing people into one cluster, including SEO specialists and growth hackers in the same cluster, while not including Office Product Manager nor Product Managers.</p><p>This is great.</p><p>From here, we can now combine both clustering and semantic search to get better results.</p><p>Remember to check out the different models and try to see which does better; you&#8217;ll see that the bigger models are naturally better at being able to group similar profiles but some smaller models do quite well.</p><h3>Clustering &amp; Semantic Search</h3><p>Now that we see that it is able to group the query into the right cluster, we can combine our approach.</p><pre><code># Let&#8217;s now do semantic search but only in the correct cluster
cluster_indices = np.where(cluster_labels == query_cluster_label)[0]

cluster_embeddings = embeddings_array[cluster_indices]
cluster_texts = [texts[i] for i in cluster_indices]

similarities_in_cluster = []
for idx, emb in zip(cluster_indices, cluster_embeddings):
    sim = cosine_similarity(query_embedding_vector, emb)
    similarities_in_cluster.append((idx, sim))

similarities_in_cluster.sort(key=lambda x: x[1], reverse=True)

top_n = 40  # adjust this number if you want to display more matches
top_matches = similarities_in_cluster[:top_n]

print(f&#8221;\nTop {top_n} similar texts in the same cluster as the query:&#8221;)
for idx, sim in top_matches:
    percentage = (sim + 1) / 2 * 100
    text_preview = &#8216; &#8216;.join(texts[idx].split()[:10])
    print(f&#8221;Text {idx+1} similarity: {percentage:.2f}% - Preview: {text_preview}...&#8221;)</code></pre><p>As we can see if we run the code above, the results are now giving back results without Product Manager in them, instead it gives us back Marketing Managers which is a better fit in general.</p><pre><code>Top 40 similar texts in the same cluster as the query:
Text 3615 similarity: 89.59% - Preview: Product Marketing Manager | Building Go-to-Market Strategies for Growth Results-driven...
Text 3232 similarity: 89.09% - Preview: Product Marketing Manager | Driving Product Growth through Data-Driven Strategies...
Text 5959 similarity: 88.90% - Preview: Product Marketing Manager | Data-Driven Growth Expert Results-driven Product Marketing...
Text 5635 similarity: 88.84% - Preview: Product Marketing Manager | Driving Growth through Data-Driven Marketing Strategies...
Text 5835 similarity: 88.74% - Preview: Product Marketing Manager | Cloud-Based SaaS Results-driven Product Marketing Manager...
Text 139 similarity: 88.66% - Preview: Product Marketing Manager | Scaling Growth through Data-Driven Strategies Experienced...
Text 6688 similarity: 88.48% - Preview: Product Marketing Manager | Driving Business Growth through Data-Driven Insights...
Text 6405 similarity: 88.27% - Preview: Product Marketing Manager | Scaling SaaS Products for Global Markets...
Text 5183 similarity: 87.86% - Preview: Product Marketing Manager | B2B SaaS Experienced Product Marketing Manager...
Text 1329 similarity: 87.81% - Preview: Product Marketing Manager | Scaling Growth for Emerging Tech Startups...
Text 130 similarity: 87.81% - Preview: Product Marketing Manager | Growth Strategies &amp; Launches Results-driven Product...
Text 3423 similarity: 87.78% - Preview: Product Marketing Manager | Scaling B2B SaaS Solutions Experienced Product...
Text 5945 similarity: 87.63% - Preview: Marketing Manager | Driving Growth through Data-Driven Strategies Results-driven marketing...
Text 2664 similarity: 87.59% - Preview: Product Marketing Manager | Driving Growth &amp; Innovation Results-driven Product...
Text 3368 similarity: 87.54% - Preview: Product Marketing Manager | Scaling Growth through Data-Driven Strategies Highly...
Text 5794 similarity: 87.48% - Preview: Product Marketing Manager | Driving Growth through Data-Driven Insights Results-driven...
Text 5685 similarity: 86.71% - Preview: Performance Marketing Manager | Driving Business Growth through Data-Driven Strategies...
Text 5818 similarity: 86.37% - Preview: Digital Marketing Manager | Driving Business Growth through Data-Driven Strategies...</code></pre><p>For a real case, you&#8217;d ideally want to filter and do classification on this dataset before performing semantic search.</p><p>The idea here is for you to compare the different models, especially smaller ones to the bigger ones, to see how much quality you are willing to sacrifice for faster and cheaper inference.</p><p>Don&#8217;t go for a bigger model just because, unless you really need it.</p><p>If you want to continue to evaluate the models, you can use <a href="https://docs.ragas.io/en/stable/">RAGAs</a> to evaluate how the retrieval application would do based on the different models.</p><h3>Notes on Model Performance</h3><p>I needed to pick something here to evaluate performance, so I chose to look at how good the models did at being able to separate product managers and product marketing managers.</p><p>The bigger models have more of an ability to get you the correct results before clustering, but all of them had issues at first to separate the two.</p><p>Ada-002, possibly being a lot bigger, did well at performing before clustering, whereas OpenAI&#8217;s smaller and newer model, text-embed-3-small, did worse.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!3zoY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3554b3c9-6af5-4720-9186-b3a8c442a856_1400x1115.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!3zoY!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3554b3c9-6af5-4720-9186-b3a8c442a856_1400x1115.png 424w, /__u/substackcdn.com/image/fetch/$s_!3zoY!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3554b3c9-6af5-4720-9186-b3a8c442a856_1400x1115.png 848w, /__u/substackcdn.com/image/fetch/$s_!3zoY!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3554b3c9-6af5-4720-9186-b3a8c442a856_1400x1115.png 1272w, /__u/substackcdn.com/image/fetch/$s_!3zoY!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3554b3c9-6af5-4720-9186-b3a8c442a856_1400x1115.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!3zoY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3554b3c9-6af5-4720-9186-b3a8c442a856_1400x1115.png" width="1400" height="1115" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3554b3c9-6af5-4720-9186-b3a8c442a856_1400x1115.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1115,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!3zoY!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3554b3c9-6af5-4720-9186-b3a8c442a856_1400x1115.png 424w, /__u/substackcdn.com/image/fetch/$s_!3zoY!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3554b3c9-6af5-4720-9186-b3a8c442a856_1400x1115.png 848w, /__u/substackcdn.com/image/fetch/$s_!3zoY!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3554b3c9-6af5-4720-9186-b3a8c442a856_1400x1115.png 1272w, /__u/substackcdn.com/image/fetch/$s_!3zoY!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3554b3c9-6af5-4720-9186-b3a8c442a856_1400x1115.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Setting up our own performance metrics for a job profile</figcaption></figure></div><p>However, some of the models were also struggling to cluster the profiles correctly as well. Specifically, the fine-tuned 7B Mistral model and E5 did not do well here. This could be a natural consequence of how they were built.</p><p>The rest did about the same, for this specific job profile.</p><p>I was surprised at how well <a href="https://huggingface.co/mixedbread-ai/mxbai-embed-large-v1">mxbai</a> performed, being only 335M in size; this goes to show that the bigger models may be overkill for simpler tasks.</p><p>This is only an evaluation for this small thing; I suggest you look at other things to evaluate performance for your task.</p><p>Nevertheless, we can continue from here and also add on strategies such as re-ranking to give the best results to an LLM to evaluate.</p><h3>Re-Ranking</h3><p>There are many strategies to correct for irrelevant results with RAG pipelines; re-ranking is one.</p><p>Re-ranking basically means to re-rank the results so the more relevant ones will be on top. Strategies to achieve this can be to use <a href="https://arxiv.org/pdf/2306.17563">Pairwise Ranking</a>.</p><p>To do this, you give a pair to a model, could be an LLM, and ask it to rank the usefulness of two profiles based on the job description.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!Yn1O!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffa501470-99ce-4163-99e8-2e71d45bf515_1272x586.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!Yn1O!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffa501470-99ce-4163-99e8-2e71d45bf515_1272x586.png 424w, /__u/substackcdn.com/image/fetch/$s_!Yn1O!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffa501470-99ce-4163-99e8-2e71d45bf515_1272x586.png 848w, /__u/substackcdn.com/image/fetch/$s_!Yn1O!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffa501470-99ce-4163-99e8-2e71d45bf515_1272x586.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Yn1O!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffa501470-99ce-4163-99e8-2e71d45bf515_1272x586.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!Yn1O!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffa501470-99ce-4163-99e8-2e71d45bf515_1272x586.png" width="1272" height="586" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fa501470-99ce-4163-99e8-2e71d45bf515_1272x586.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:586,&quot;width&quot;:1272,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!Yn1O!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffa501470-99ce-4163-99e8-2e71d45bf515_1272x586.png 424w, /__u/substackcdn.com/image/fetch/$s_!Yn1O!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffa501470-99ce-4163-99e8-2e71d45bf515_1272x586.png 848w, /__u/substackcdn.com/image/fetch/$s_!Yn1O!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffa501470-99ce-4163-99e8-2e71d45bf515_1272x586.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Yn1O!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffa501470-99ce-4163-99e8-2e71d45bf515_1272x586.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Pairwise Ranking with an LLM &#8212; simplified</figcaption></figure></div><p>You&#8217;ll have to combine methods for your use case to enable it to perform well.</p><p>If you are new to embeddings, I hope you learned something, and if it&#8217;s not new, then I hope you got a bit of intel about the economics of using smaller versus larger embedding models, be they open source or commercial.</p><p>For the larger LLMs, many closed-source models are taking the lead, this is not true when it comes to embedding models.</p><p>Something to take with you is to give a smaller, more computationally efficient model a chance.</p><p>&#10084;</p>]]></content:encoded></item><item><title><![CDATA[Agentic AI: Single vs Multi-Agent Systems]]></title><description><![CDATA[Building with a structured data source in LangGraph]]></description><link>https://howtouseai.substack.com/p/agentic-ai-single-vs-multi-agent</link><guid isPermaLink="false">https://howtouseai.substack.com/p/agentic-ai-single-vs-multi-agent</guid><dc:creator><![CDATA[Ida Silfverskiold]]></dc:creator><pubDate>Tue, 01 Sep 2026 07:51:48 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!mYw_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F95fe8311-6822-4022-ba07-17ebc80aa5e8_1400x756.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!mYw_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F95fe8311-6822-4022-ba07-17ebc80aa5e8_1400x756.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!mYw_!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F95fe8311-6822-4022-ba07-17ebc80aa5e8_1400x756.png 424w, /__u/substackcdn.com/image/fetch/$s_!mYw_!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F95fe8311-6822-4022-ba07-17ebc80aa5e8_1400x756.png 848w, /__u/substackcdn.com/image/fetch/$s_!mYw_!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F95fe8311-6822-4022-ba07-17ebc80aa5e8_1400x756.png 1272w, /__u/substackcdn.com/image/fetch/$s_!mYw_!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F95fe8311-6822-4022-ba07-17ebc80aa5e8_1400x756.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!mYw_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F95fe8311-6822-4022-ba07-17ebc80aa5e8_1400x756.png" width="1400" height="756" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/95fe8311-6822-4022-ba07-17ebc80aa5e8_1400x756.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:756,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!mYw_!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F95fe8311-6822-4022-ba07-17ebc80aa5e8_1400x756.png 424w, /__u/substackcdn.com/image/fetch/$s_!mYw_!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F95fe8311-6822-4022-ba07-17ebc80aa5e8_1400x756.png 848w, /__u/substackcdn.com/image/fetch/$s_!mYw_!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F95fe8311-6822-4022-ba07-17ebc80aa5e8_1400x756.png 1272w, /__u/substackcdn.com/image/fetch/$s_!mYw_!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F95fe8311-6822-4022-ba07-17ebc80aa5e8_1400x756.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Building different agent systems in LangGraph | Image by author</figcaption></figure></div><p><em>This is an article dated October 2025 but has been re-published here.</em></p><p>If you&#8217;re just starting to build different agentic systems, one of the interesting areas is the difference between <strong>building a single versus multi-agent workflow</strong>, or perhaps the difference between working with more flexible vs controlled systems.</p><p>This article will help you understand what agentic AI is and how to build agentic systems with LangGraph &amp; LangSmith Studio.</p><p>We&#8217;ll build a researcher with two different architectures to be able to compare the results to understand which one can do better.</p><p>You&#8217;ll find the resources we&#8217;ll be working with <a href="https://github.com/ilsilfverskiold/Awesome-LLM-Resources-List/tree/main/guides/langgraph">here</a>. Running the systems will mostly be free except for some OpenAI tokens.</p><p><em>As a note, if you want to get an overview of the different open source frameworks out there, check out <a href="https://medium.com/data-science-collective/agentic-ai-comparing-new-open-source-frameworks-21ec676732df">this</a> article.</em></p><h3>The use case</h3><p>To build something concrete, we&#8217;ll be building a research agent for tech that can find what is trending yesterday, or the last week, and then figure out what is news worthy.</p><p>Working with <strong>summarizing and gathering research</strong> is one of those areas that <strong>agentic AI can really shine</strong>.</p><p>This article will be using an <a href="https://docs.safron.io">API</a> that gathers what people are speaking and sharing in tech, and the agentic system will be tasked with deciding what is important based on our user persona, and then summarizing for us.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!lbvy!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff99f0b0f-3013-44b8-a8c2-abf3a7ed51f7_1400x612.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!lbvy!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff99f0b0f-3013-44b8-a8c2-abf3a7ed51f7_1400x612.png 424w, /__u/substackcdn.com/image/fetch/$s_!lbvy!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff99f0b0f-3013-44b8-a8c2-abf3a7ed51f7_1400x612.png 848w, /__u/substackcdn.com/image/fetch/$s_!lbvy!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff99f0b0f-3013-44b8-a8c2-abf3a7ed51f7_1400x612.png 1272w, /__u/substackcdn.com/image/fetch/$s_!lbvy!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff99f0b0f-3013-44b8-a8c2-abf3a7ed51f7_1400x612.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!lbvy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff99f0b0f-3013-44b8-a8c2-abf3a7ed51f7_1400x612.png" width="1400" height="612" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f99f0b0f-3013-44b8-a8c2-abf3a7ed51f7_1400x612.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:612,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!lbvy!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff99f0b0f-3013-44b8-a8c2-abf3a7ed51f7_1400x612.png 424w, /__u/substackcdn.com/image/fetch/$s_!lbvy!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff99f0b0f-3013-44b8-a8c2-abf3a7ed51f7_1400x612.png 848w, /__u/substackcdn.com/image/fetch/$s_!lbvy!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff99f0b0f-3013-44b8-a8c2-abf3a7ed51f7_1400x612.png 1272w, /__u/substackcdn.com/image/fetch/$s_!lbvy!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff99f0b0f-3013-44b8-a8c2-abf3a7ed51f7_1400x612.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">The data source is giving us structured data the agentic system can work with</figcaption></figure></div><p>The agent won&#8217;t be adding any citations in the text, we are looking at how much it covers when we only set up a single agent versus what it covers when we are setting up multiple agents working in sync.</p><p>The focus thus is less on the data source and more on the agentic part.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!o5a-!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc5a6c887-3fd5-406c-aa0e-f9610760fb99_1400x748.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!o5a-!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc5a6c887-3fd5-406c-aa0e-f9610760fb99_1400x748.png 424w, /__u/substackcdn.com/image/fetch/$s_!o5a-!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc5a6c887-3fd5-406c-aa0e-f9610760fb99_1400x748.png 848w, /__u/substackcdn.com/image/fetch/$s_!o5a-!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc5a6c887-3fd5-406c-aa0e-f9610760fb99_1400x748.png 1272w, /__u/substackcdn.com/image/fetch/$s_!o5a-!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc5a6c887-3fd5-406c-aa0e-f9610760fb99_1400x748.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!o5a-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc5a6c887-3fd5-406c-aa0e-f9610760fb99_1400x748.png" width="1400" height="748" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c5a6c887-3fd5-406c-aa0e-f9610760fb99_1400x748.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:748,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!o5a-!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc5a6c887-3fd5-406c-aa0e-f9610760fb99_1400x748.png 424w, /__u/substackcdn.com/image/fetch/$s_!o5a-!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc5a6c887-3fd5-406c-aa0e-f9610760fb99_1400x748.png 848w, /__u/substackcdn.com/image/fetch/$s_!o5a-!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc5a6c887-3fd5-406c-aa0e-f9610760fb99_1400x748.png 1272w, /__u/substackcdn.com/image/fetch/$s_!o5a-!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc5a6c887-3fd5-406c-aa0e-f9610760fb99_1400x748.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Example of what our multi-agent system will look like in LangSmith Studio taking several minutes to complete</figcaption></figure></div><p>We will build this system first with a simple agent that has access to the different API endpoints, and then we will build out the system to use several teams and more comprehensive tools to see the difference in quality.</p><p><em>Before we start, I always do a review for beginners. If you&#8217;re well versed in agentic systems, you can scroll past some of the first sections.</em></p><h3>Agentic AI &amp; LLMs</h3><p>Agentic AI is about programming with natural language. Instead of using rigid, explicit code, you&#8217;re instructing large language models (LLMs) to route data and perform actions through plain language to automate tasks.</p><p>Using natural language in workflows isn&#8217;t new, we&#8217;ve used NLP for years to extract and process data.</p><p>What&#8217;s new is the amount of freedom we can now give language models, allowing them to handle ambiguity and make decisions dynamically.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!FIga!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8863031e-7fb4-46ee-8f0d-668733897f2a_1400x441.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!FIga!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8863031e-7fb4-46ee-8f0d-668733897f2a_1400x441.png 424w, /__u/substackcdn.com/image/fetch/$s_!FIga!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8863031e-7fb4-46ee-8f0d-668733897f2a_1400x441.png 848w, /__u/substackcdn.com/image/fetch/$s_!FIga!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8863031e-7fb4-46ee-8f0d-668733897f2a_1400x441.png 1272w, /__u/substackcdn.com/image/fetch/$s_!FIga!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8863031e-7fb4-46ee-8f0d-668733897f2a_1400x441.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!FIga!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8863031e-7fb4-46ee-8f0d-668733897f2a_1400x441.png" width="1400" height="441" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8863031e-7fb4-46ee-8f0d-668733897f2a_1400x441.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:441,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!FIga!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8863031e-7fb4-46ee-8f0d-668733897f2a_1400x441.png 424w, /__u/substackcdn.com/image/fetch/$s_!FIga!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8863031e-7fb4-46ee-8f0d-668733897f2a_1400x441.png 848w, /__u/substackcdn.com/image/fetch/$s_!FIga!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8863031e-7fb4-46ee-8f0d-668733897f2a_1400x441.png 1272w, /__u/substackcdn.com/image/fetch/$s_!FIga!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8863031e-7fb4-46ee-8f0d-668733897f2a_1400x441.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>But just because LLMs can understand nuanced language doesn&#8217;t mean they inherently validate facts or maintain data integrity.</p><p>I see them primarily as a communication layer (at least right now) that sits on top of structured systems and existing data sources.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!K9tB!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58364d6a-0dfd-4aed-8dd3-6b03dfb336fd_1400x647.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!K9tB!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58364d6a-0dfd-4aed-8dd3-6b03dfb336fd_1400x647.png 424w, /__u/substackcdn.com/image/fetch/$s_!K9tB!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58364d6a-0dfd-4aed-8dd3-6b03dfb336fd_1400x647.png 848w, /__u/substackcdn.com/image/fetch/$s_!K9tB!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58364d6a-0dfd-4aed-8dd3-6b03dfb336fd_1400x647.png 1272w, /__u/substackcdn.com/image/fetch/$s_!K9tB!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58364d6a-0dfd-4aed-8dd3-6b03dfb336fd_1400x647.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!K9tB!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58364d6a-0dfd-4aed-8dd3-6b03dfb336fd_1400x647.png" width="1400" height="647" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/58364d6a-0dfd-4aed-8dd3-6b03dfb336fd_1400x647.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:647,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!K9tB!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58364d6a-0dfd-4aed-8dd3-6b03dfb336fd_1400x647.png 424w, /__u/substackcdn.com/image/fetch/$s_!K9tB!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58364d6a-0dfd-4aed-8dd3-6b03dfb336fd_1400x647.png 848w, /__u/substackcdn.com/image/fetch/$s_!K9tB!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58364d6a-0dfd-4aed-8dd3-6b03dfb336fd_1400x647.png 1272w, /__u/substackcdn.com/image/fetch/$s_!K9tB!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F58364d6a-0dfd-4aed-8dd3-6b03dfb336fd_1400x647.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>I usually explain it like this to non-technical people: they work a bit like we do. If we don&#8217;t have access to clean, structured data, we start making things up. Same with LLMs.</p><p>So just like us, they do their best with what they&#8217;ve got. If we want better output, we need to build systems that give them reliable data or systems to work with.</p><p>So, with Agentic systems we integrate ways for them to interact with different data sources, tools and systems.</p><p>Now, just because we <em>can</em> use these larger models in more places, doesn&#8217;t mean we always<em> should</em>. LLMs shine when interpreting nuanced natural language, think customer service, research, or human-in-the-loop collaboration.</p><p>But for structured tasks (like extracting numbers and sending them somewhere) you can to use traditional approaches and automation.</p><p>LLMs aren&#8217;t inherently better at math than a calculator. So, instead of having an LLM do calculations, you give an LLM access to a calculator.</p><p>So whenever you <em>can</em> build parts of a workflow programmatically, that will still be the better option.</p><p>Nevertheless, LLMs are great at adapting to messy real-world input and interpreting vague instructions so combining the two can be a great way to build systems.</p><p><em>If you are very new to this, and you&#8217;re still confused, go browse some of my other stuff that explain this in more detail. It may make more sense when we build later.</em></p><h3>Agentic frameworks and LangGraph</h3><p>I know a lot of people jump straight to CrewAI or AutoGen for their first agent, but we have a ton of options. For this article I will introduce you to LangGraph.</p><p>LangGraph is a graph-based framework built on top of LangChain. I would say it&#8217;s more technical and can be complex compared to other frameworks. However, it&#8217;s the preferred choice for many developers which is why it is worth building at least something with it.</p><p>LangGraph has a lot of abstractions though, where you may want to rebuild some of it just to be able to control and understand it better.</p><p>I will not go into detail on LangGraph here, so I decided to build a quick <a href="/__u/howtouseai.substack.com/p/agentic-ai-frameworks-learning-langgraph">guide</a> for those that need to get a review.</p><p>If you want to get an overview of the different frameworks out there, I have done some writing on this <a href="https://medium.com/data-science-collective/agentic-ai-comparing-new-open-source-frameworks-21ec676732df">here</a> which generated north of 100k readers.</p><p><em>I should note that I prefer no framework at all when I build nowadays but I copy a lot from what I have picked up from different frameworks so it&#8217;s still worth it to learn how to work with them.</em></p><p>As for this use case, you&#8217;ll be able to run the workflow without coding anything, but if you&#8217;re here to learn you may also want to understand how LangGraph works.</p><h3>Single vs. multi-agent systems</h3><p>Before we dig into building, let&#8217;s just first cover the differences with single vs multi-agent systems.</p><p>If you build a system around <strong>one LLM and give it a bunch of tools</strong> you want it to use, you&#8217;re working with a <strong>single-agent workflow</strong>. It&#8217;s fast, and if you&#8217;re new to agentic AI, it might seem like the model should just figure things out on its own.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!Yfe2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e33a767-b087-4738-bfdb-996bbecb3670_1400x685.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!Yfe2!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e33a767-b087-4738-bfdb-996bbecb3670_1400x685.png 424w, /__u/substackcdn.com/image/fetch/$s_!Yfe2!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e33a767-b087-4738-bfdb-996bbecb3670_1400x685.png 848w, /__u/substackcdn.com/image/fetch/$s_!Yfe2!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e33a767-b087-4738-bfdb-996bbecb3670_1400x685.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Yfe2!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e33a767-b087-4738-bfdb-996bbecb3670_1400x685.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!Yfe2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e33a767-b087-4738-bfdb-996bbecb3670_1400x685.png" width="1400" height="685" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8e33a767-b087-4738-bfdb-996bbecb3670_1400x685.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:685,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!Yfe2!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e33a767-b087-4738-bfdb-996bbecb3670_1400x685.png 424w, /__u/substackcdn.com/image/fetch/$s_!Yfe2!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e33a767-b087-4738-bfdb-996bbecb3670_1400x685.png 848w, /__u/substackcdn.com/image/fetch/$s_!Yfe2!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e33a767-b087-4738-bfdb-996bbecb3670_1400x685.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Yfe2!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8e33a767-b087-4738-bfdb-996bbecb3670_1400x685.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>But the thing is these workflows are just another form of system design.</p><p>Like any software project, you need to plan the process, define the steps, structure the logic, and decide how each part should behave.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!pkPO!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc4c5beaa-acac-4e3d-8f11-f5d66ffcc070_1400x764.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!pkPO!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc4c5beaa-acac-4e3d-8f11-f5d66ffcc070_1400x764.png 424w, /__u/substackcdn.com/image/fetch/$s_!pkPO!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc4c5beaa-acac-4e3d-8f11-f5d66ffcc070_1400x764.png 848w, /__u/substackcdn.com/image/fetch/$s_!pkPO!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc4c5beaa-acac-4e3d-8f11-f5d66ffcc070_1400x764.png 1272w, /__u/substackcdn.com/image/fetch/$s_!pkPO!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc4c5beaa-acac-4e3d-8f11-f5d66ffcc070_1400x764.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!pkPO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc4c5beaa-acac-4e3d-8f11-f5d66ffcc070_1400x764.png" width="1400" height="764" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c4c5beaa-acac-4e3d-8f11-f5d66ffcc070_1400x764.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:764,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!pkPO!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc4c5beaa-acac-4e3d-8f11-f5d66ffcc070_1400x764.png 424w, /__u/substackcdn.com/image/fetch/$s_!pkPO!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc4c5beaa-acac-4e3d-8f11-f5d66ffcc070_1400x764.png 848w, /__u/substackcdn.com/image/fetch/$s_!pkPO!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc4c5beaa-acac-4e3d-8f11-f5d66ffcc070_1400x764.png 1272w, /__u/substackcdn.com/image/fetch/$s_!pkPO!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc4c5beaa-acac-4e3d-8f11-f5d66ffcc070_1400x764.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This is where multi-agent workflows come in.</p><p>Not all of them are hierarchical or linear though, some are collaborative. Collaborative workflows would then also fall into the more flexible approach that I find more difficult to work with, at least as it is now with the capabilities that exist.</p><p>However, collaborative workflows do also break apart different functions into their own modules.</p><p>Collaborative workflows are great to start with when you&#8217;re just playing around, but they don&#8217;t always give you the precision needed for actual tasks.</p><p>For the workflow I will build here, I already know how the APIs should be used so it&#8217;s my job to guide the system to use it the right way.</p><p>We&#8217;ll go through comparing a single-agent setup with a hierarchical multi-agent system, where a lead agent delegates tasks across a small team so you can see how they behave in practice.</p><h3>Building a single agent</h3><p>To build a single agent we work with one LLM and one system prompt for it, and then we give it access to several tools.</p><p>It&#8217;s up to the agent to decide which tool to use and when, based on the user&#8217;s question.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!dr6d!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F304ade2e-e090-474f-87ae-67e3d13e8768_1400x693.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!dr6d!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F304ade2e-e090-474f-87ae-67e3d13e8768_1400x693.png 424w, /__u/substackcdn.com/image/fetch/$s_!dr6d!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F304ade2e-e090-474f-87ae-67e3d13e8768_1400x693.png 848w, /__u/substackcdn.com/image/fetch/$s_!dr6d!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F304ade2e-e090-474f-87ae-67e3d13e8768_1400x693.png 1272w, /__u/substackcdn.com/image/fetch/$s_!dr6d!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F304ade2e-e090-474f-87ae-67e3d13e8768_1400x693.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!dr6d!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F304ade2e-e090-474f-87ae-67e3d13e8768_1400x693.png" width="1400" height="693" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/304ade2e-e090-474f-87ae-67e3d13e8768_1400x693.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:693,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!dr6d!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F304ade2e-e090-474f-87ae-67e3d13e8768_1400x693.png 424w, /__u/substackcdn.com/image/fetch/$s_!dr6d!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F304ade2e-e090-474f-87ae-67e3d13e8768_1400x693.png 848w, /__u/substackcdn.com/image/fetch/$s_!dr6d!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F304ade2e-e090-474f-87ae-67e3d13e8768_1400x693.png 1272w, /__u/substackcdn.com/image/fetch/$s_!dr6d!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F304ade2e-e090-474f-87ae-67e3d13e8768_1400x693.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The challenge with a single agent is control.</p><p>No matter how detailed the system prompt is, the model may not follow our requests (this can happen in more controlled environments too). If we give it too many tools or options, there&#8217;s a good chance it won&#8217;t use all of them or even use the right ones.</p><p>There is only so much we can set in the instructions for it to act like we want it to.</p><p>To illustrate this, we&#8217;ll build the tech news agent that has access to several API endpoints with custom data with several options as parameters in the tools.</p><p>It&#8217;s up to the agent to decide how many to use and how to setup the final summary.</p><p><em>Remember, I build these workflows using LangGraph. I won&#8217;t go into LangGraph in depth here, so if you want to learn the basics to be able to tweak the code, <a href="/__u/howtouseai.substack.com/p/agentic-ai-frameworks-learning-langgraph">go here</a> (the guide is from April 2025).</em></p><p>You can find the single-agent workflow <a href="https://github.com/ilsilfverskiold/Awesome-LLM-Resources-List/tree/main/guides/langgraph/single-agent-workflow">here</a>. To run it, you&#8217;ll need to have a login for LangSmith, where you get access to LangSmith Studio, and python 3 installed.</p><p>Once you have a login, you can clone the single agent workflow to your computer.</p><p>It will have this structure.</p><pre><code>single-agent-workflow/
&#9500;&#9472; my_agent/
&#9474;  &#9500;&#9472; agent.py
&#9474;  &#9500;&#9472; requirements.txt
&#9474;  &#9500;&#9472; utils/
&#9474;     &#9500;&#9472; nodes.py
&#9474;     &#9500;&#9472; state.py
&#9474;     &#9492;&#9472; tools.py
&#9500;&#9472; README.md  
&#9500;&#9472; langgraph.json  </code></pre><p>Create an .env file and add a Google API key. We&#8217;ll be using Gemini for this single agent.</p><pre><code>GOOGLE_API_KEY=KEY_HERE</code></pre><p>Then set up an environment.</p><pre><code>python3.11 -m venv venv_py311</code></pre><pre><code>source venv_py311/bin/activate</code></pre><p>Install the LangGraph cli.</p><pre><code>pip install -U &#8220;langgraph-cli[inmem]&#8221;</code></pre><p>You&#8217;ll find the requirements in the my_agent folder and you&#8217;ll install these too.</p><pre><code>pip install -r my_agent/requirements.txt</code></pre><p>Once these have downloaded you can open up the single agent workflow in LangSmith studio.</p><pre><code>langgraph dev</code></pre><p>This will automatically open up LangSmith Studio (before it was LangGraph Studio).</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!gAz_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f194355-e52d-4c62-8000-3802a5b7abba_1400x753.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!gAz_!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f194355-e52d-4c62-8000-3802a5b7abba_1400x753.png 424w, /__u/substackcdn.com/image/fetch/$s_!gAz_!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f194355-e52d-4c62-8000-3802a5b7abba_1400x753.png 848w, /__u/substackcdn.com/image/fetch/$s_!gAz_!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f194355-e52d-4c62-8000-3802a5b7abba_1400x753.png 1272w, /__u/substackcdn.com/image/fetch/$s_!gAz_!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f194355-e52d-4c62-8000-3802a5b7abba_1400x753.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!gAz_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f194355-e52d-4c62-8000-3802a5b7abba_1400x753.png" width="1400" height="753" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1f194355-e52d-4c62-8000-3802a5b7abba_1400x753.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:753,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!gAz_!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f194355-e52d-4c62-8000-3802a5b7abba_1400x753.png 424w, /__u/substackcdn.com/image/fetch/$s_!gAz_!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f194355-e52d-4c62-8000-3802a5b7abba_1400x753.png 848w, /__u/substackcdn.com/image/fetch/$s_!gAz_!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f194355-e52d-4c62-8000-3802a5b7abba_1400x753.png 1272w, /__u/substackcdn.com/image/fetch/$s_!gAz_!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1f194355-e52d-4c62-8000-3802a5b7abba_1400x753.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>It&#8217;s important that you set up an assistant here as well, as the default will be set to Anthropic and we&#8217;re using Gemini for this agent.</p><p>To manage assistants, click on the button in the bottom left corner.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!Y-Hg!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1d0274f5-ca68-4fe3-b528-d3b9acdeb025_1400x739.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!Y-Hg!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1d0274f5-ca68-4fe3-b528-d3b9acdeb025_1400x739.png 424w, /__u/substackcdn.com/image/fetch/$s_!Y-Hg!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1d0274f5-ca68-4fe3-b528-d3b9acdeb025_1400x739.png 848w, /__u/substackcdn.com/image/fetch/$s_!Y-Hg!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1d0274f5-ca68-4fe3-b528-d3b9acdeb025_1400x739.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Y-Hg!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1d0274f5-ca68-4fe3-b528-d3b9acdeb025_1400x739.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!Y-Hg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1d0274f5-ca68-4fe3-b528-d3b9acdeb025_1400x739.png" width="1400" height="739" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1d0274f5-ca68-4fe3-b528-d3b9acdeb025_1400x739.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:739,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!Y-Hg!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1d0274f5-ca68-4fe3-b528-d3b9acdeb025_1400x739.png 424w, /__u/substackcdn.com/image/fetch/$s_!Y-Hg!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1d0274f5-ca68-4fe3-b528-d3b9acdeb025_1400x739.png 848w, /__u/substackcdn.com/image/fetch/$s_!Y-Hg!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1d0274f5-ca68-4fe3-b528-d3b9acdeb025_1400x739.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Y-Hg!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1d0274f5-ca68-4fe3-b528-d3b9acdeb025_1400x739.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Make sure you create a new assistant with &#8216;gemini&#8217; and click &#8216;create new assistant&#8217; | Image by author</p><p>You&#8217;ll see a modal like the one above. Set the model to &#8216;gemini&#8217; and then click &#8216;Create New Assistant.&#8217;</p><p>Once you&#8217;re back to the main screen, you can set a start message.</p><p>For this message you can tell it what you want, such as what you work with and if you want information for daily, weekly or monthly (see an example below).</p><pre><code>{&#8221;messages&#8221;: [&#8221;I&#8217;m a tech investor, give me what&#8217;s up in tech for the last week&#8221;]}</code></pre><p>It will look like this in LangSmith Studio.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!esGR!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0b044d05-660e-46e5-a582-8f0b42ce56fc_1400x745.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!esGR!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0b044d05-660e-46e5-a582-8f0b42ce56fc_1400x745.png 424w, /__u/substackcdn.com/image/fetch/$s_!esGR!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0b044d05-660e-46e5-a582-8f0b42ce56fc_1400x745.png 848w, /__u/substackcdn.com/image/fetch/$s_!esGR!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0b044d05-660e-46e5-a582-8f0b42ce56fc_1400x745.png 1272w, /__u/substackcdn.com/image/fetch/$s_!esGR!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0b044d05-660e-46e5-a582-8f0b42ce56fc_1400x745.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!esGR!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0b044d05-660e-46e5-a582-8f0b42ce56fc_1400x745.png" width="1400" height="745" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0b044d05-660e-46e5-a582-8f0b42ce56fc_1400x745.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:745,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!esGR!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0b044d05-660e-46e5-a582-8f0b42ce56fc_1400x745.png 424w, /__u/substackcdn.com/image/fetch/$s_!esGR!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0b044d05-660e-46e5-a582-8f0b42ce56fc_1400x745.png 848w, /__u/substackcdn.com/image/fetch/$s_!esGR!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0b044d05-660e-46e5-a582-8f0b42ce56fc_1400x745.png 1272w, /__u/substackcdn.com/image/fetch/$s_!esGR!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0b044d05-660e-46e5-a582-8f0b42ce56fc_1400x745.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Once you click submit, the result will be fast, as we&#8217;re working with a single agent.</p><p>It decides to first check a few trending keywords in 3 different categories and then it checks what people are saying for a few of the trending keywords.</p><p><em>If you want to understand how these tools are created, be sure to check inside the code but it simply uses an API.</em></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!wvlw!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7ab9f248-80db-42d9-abd9-2d6c36d51f40_1400x741.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!wvlw!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7ab9f248-80db-42d9-abd9-2d6c36d51f40_1400x741.png 424w, /__u/substackcdn.com/image/fetch/$s_!wvlw!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7ab9f248-80db-42d9-abd9-2d6c36d51f40_1400x741.png 848w, /__u/substackcdn.com/image/fetch/$s_!wvlw!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7ab9f248-80db-42d9-abd9-2d6c36d51f40_1400x741.png 1272w, /__u/substackcdn.com/image/fetch/$s_!wvlw!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7ab9f248-80db-42d9-abd9-2d6c36d51f40_1400x741.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!wvlw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7ab9f248-80db-42d9-abd9-2d6c36d51f40_1400x741.png" width="1400" height="741" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7ab9f248-80db-42d9-abd9-2d6c36d51f40_1400x741.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:741,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!wvlw!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7ab9f248-80db-42d9-abd9-2d6c36d51f40_1400x741.png 424w, /__u/substackcdn.com/image/fetch/$s_!wvlw!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7ab9f248-80db-42d9-abd9-2d6c36d51f40_1400x741.png 848w, /__u/substackcdn.com/image/fetch/$s_!wvlw!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7ab9f248-80db-42d9-abd9-2d6c36d51f40_1400x741.png 1272w, /__u/substackcdn.com/image/fetch/$s_!wvlw!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7ab9f248-80db-42d9-abd9-2d6c36d51f40_1400x741.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The end result looks like this (although it depends when you&#8217;ve run it, the information will obviously differ day by day).</p><pre><code>Here&#8217;s a summary of the trending tech topics from the past week:

Companies:
Apple: Apple is facing scrutiny over its App Store practices, including a UK monopoly case and concerns about attention to detail. There are also reports of reduced iPhone Air production and its potential move to include ads in the Maps app.
Meta: Meta is undergoing layoffs in its AI division, which has generated negative sentiment. There is also discussion around a mod that disables the recording light on Meta&#8217;s Ray-Ban glasses, raising privacy concerns.
GM: GM&#8217;s decision to ditch Apple CarPlay and Android Auto in future vehicles has sparked controversy and negative reactions from users.
Samsung: Samsung is planning to introduce ads on its smart fridges, drawing criticism. There&#8217;s also news about the upcoming Galaxy XR event and a new chief design officer.
[...]</code></pre><p>If you want to read the whole thing without booting the agent up yourself, see it <a href="https://github.com/ilsilfverskiold/Awesome-LLM-Resources-List/blob/main/guides/langgraph/single-agent-workflow/results.md">here</a>. The result is fine, but as you can see it&#8217;s not digging all that deep.</p><p>We can of course probe it and keep asking it questions but as you can imagine if we need something more complex it would start to make shortcuts in the workflow.</p><p>The key thing is, an agent system isn&#8217;t just gonna think the way we expect, we have to actually orchestrate it to do what we want.</p><p>This is fine as long as we have a human in the loop, for Q&amp;A, that sort of thing.</p><p>For this, if a human asks for one thing and the agent fetches that information, it would work great.</p><p>For deep research though we need to build a bit more complex of a system, we can do this with a workflow like system (one part does one thing), or we can try to build a hierarchical system where one agent (or team) is responsible for one thing.</p><h3>Testing a multi-agent workflow</h3><p>Building multiagent system is a lot more difficult than building a single agent with access to some tools.</p><p>To do this, you need to carefully think about the architecture beforehand and how data should flow between the agents.</p><p>The multi-agent <a href="https://github.com/ilsilfverskiold/Awesome-LLM-Resources-List/tree/main/guides/langgraph/tech_news_bot_multiagent">workflow</a> I&#8217;ll set up here uses two different teams (a research team and an editing team) with several agents under each.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!b3-V!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe090bb5f-1dc4-46b7-9dfa-50246556d740_1400x633.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!b3-V!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe090bb5f-1dc4-46b7-9dfa-50246556d740_1400x633.png 424w, /__u/substackcdn.com/image/fetch/$s_!b3-V!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe090bb5f-1dc4-46b7-9dfa-50246556d740_1400x633.png 848w, /__u/substackcdn.com/image/fetch/$s_!b3-V!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe090bb5f-1dc4-46b7-9dfa-50246556d740_1400x633.png 1272w, /__u/substackcdn.com/image/fetch/$s_!b3-V!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe090bb5f-1dc4-46b7-9dfa-50246556d740_1400x633.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!b3-V!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe090bb5f-1dc4-46b7-9dfa-50246556d740_1400x633.png" width="1400" height="633" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e090bb5f-1dc4-46b7-9dfa-50246556d740_1400x633.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:633,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!b3-V!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe090bb5f-1dc4-46b7-9dfa-50246556d740_1400x633.png 424w, /__u/substackcdn.com/image/fetch/$s_!b3-V!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe090bb5f-1dc4-46b7-9dfa-50246556d740_1400x633.png 848w, /__u/substackcdn.com/image/fetch/$s_!b3-V!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe090bb5f-1dc4-46b7-9dfa-50246556d740_1400x633.png 1272w, /__u/substackcdn.com/image/fetch/$s_!b3-V!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe090bb5f-1dc4-46b7-9dfa-50246556d740_1400x633.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Example architecture of teams/agents in our multi-agent system | Image by author</p><p>Every agent has access to a specific set of tools (not too many) and clear instructions.</p><p>This limited scope for each agent is great when working with lower level LLMs (i.e. Gemini Flash 2.0), although I always like to use a more advanced LLM for summarizing (in this case we&#8217;re using GPT-5 as the summerizer agent).</p><p>We&#8217;re introducing some new tools, like a research pad that acts as a shared space (one team writes their findings, the other reads from it). The last LLM will read everything that has been researched and edited to make a summary.</p><p>An alternative to using a research pad is to store data in a scratchpad in state, isolating short-term memory for each team or agent. But that also means thinking carefully about what each agent&#8217;s memory should include.</p><p>I also decided to build out the tools a bit more to provide richer data upfront, so the agents don&#8217;t have to fetch sources for each keyword individually. Here I&#8217;m using normal programmatic logic because I can.</p><p><em>A key thing to remember: if you can use normal programming logic, do it.</em></p><p>The workflow is set up for you <a href="https://github.com/ilsilfverskiold/Awesome-LLM-Resources-List/tree/main/guides/langgraph/tech_news_bot_multiagent">here</a>. Before loading it, make sure to add both your OpenAI and Google API keys in an <code>.env</code> file.</p><pre><code>GOOGLE_API_KEY=KEY_HERE
OPENAI_API_KEY=KEY_HERE
ANTHROPIC_API_KEY=KEY_HERE</code></pre><p>You only need to set the Anthropic key if you will be playing around with changing the agents (but it may give you an error if you don&#8217;t set it, in that case be sure to set up an assistant with only Gemini).</p><p>You&#8217;ll need to do the same as you did with the single agent workflow from here, set up an environment, install the requirements and open up the workflow.</p><pre><code>langgraph dev</code></pre><p>Once you&#8217;ve opened it up, you&#8217;ll see that it looks a lot more evolved than our single agent.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!KD1V!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07f76616-23bf-492a-a926-dbb76705a70c_1400x741.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!KD1V!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07f76616-23bf-492a-a926-dbb76705a70c_1400x741.png 424w, /__u/substackcdn.com/image/fetch/$s_!KD1V!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07f76616-23bf-492a-a926-dbb76705a70c_1400x741.png 848w, /__u/substackcdn.com/image/fetch/$s_!KD1V!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07f76616-23bf-492a-a926-dbb76705a70c_1400x741.png 1272w, /__u/substackcdn.com/image/fetch/$s_!KD1V!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07f76616-23bf-492a-a926-dbb76705a70c_1400x741.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!KD1V!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07f76616-23bf-492a-a926-dbb76705a70c_1400x741.png" width="1400" height="741" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/07f76616-23bf-492a-a926-dbb76705a70c_1400x741.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:741,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!KD1V!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07f76616-23bf-492a-a926-dbb76705a70c_1400x741.png 424w, /__u/substackcdn.com/image/fetch/$s_!KD1V!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07f76616-23bf-492a-a926-dbb76705a70c_1400x741.png 848w, /__u/substackcdn.com/image/fetch/$s_!KD1V!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07f76616-23bf-492a-a926-dbb76705a70c_1400x741.png 1272w, /__u/substackcdn.com/image/fetch/$s_!KD1V!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07f76616-23bf-492a-a926-dbb76705a70c_1400x741.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>In this workflow, the routes (edges) are setup dynamically instead of manually like we did with the single agent. It&#8217;ll look more complex if you peek into the code.</p><p>To run it you need to give it a message like last time.</p><p>I&#8217;ve decided to prompt it a bit more here which is a bit of cheating, you can try something simpler.</p><pre><code>{&#8221;messages&#8221;: [&#8221;{&#8221;messages&#8220;: [&#8221;I&#8217;m an investor and I&#8217;m interested in getting an update for what has happened within the week in tech, and what people are talking about (this means categories like companies, people, websites and subjects are interesting). Please also track these specific keywords: AI, Google, Microsoft, and Large Language Models&#8221;]}&#8221;]}</code></pre><p>It&#8217;s better to set what you set before so you have something to compare it with but you do you.</p><p>Once it starts it will take quite some time, so you can walk away from it and then return in a few minutes.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!_-aD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34f9e709-7570-4e08-87cf-20429e28daf6_1400x736.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!_-aD!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34f9e709-7570-4e08-87cf-20429e28daf6_1400x736.png 424w, /__u/substackcdn.com/image/fetch/$s_!_-aD!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34f9e709-7570-4e08-87cf-20429e28daf6_1400x736.png 848w, /__u/substackcdn.com/image/fetch/$s_!_-aD!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34f9e709-7570-4e08-87cf-20429e28daf6_1400x736.png 1272w, /__u/substackcdn.com/image/fetch/$s_!_-aD!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34f9e709-7570-4e08-87cf-20429e28daf6_1400x736.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!_-aD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34f9e709-7570-4e08-87cf-20429e28daf6_1400x736.png" width="1400" height="736" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/34f9e709-7570-4e08-87cf-20429e28daf6_1400x736.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:736,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!_-aD!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34f9e709-7570-4e08-87cf-20429e28daf6_1400x736.png 424w, /__u/substackcdn.com/image/fetch/$s_!_-aD!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34f9e709-7570-4e08-87cf-20429e28daf6_1400x736.png 848w, /__u/substackcdn.com/image/fetch/$s_!_-aD!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34f9e709-7570-4e08-87cf-20429e28daf6_1400x736.png 1272w, /__u/substackcdn.com/image/fetch/$s_!_-aD!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F34f9e709-7570-4e08-87cf-20429e28daf6_1400x736.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The tools that the trending keywords agent has access to are more complex from the single agent so they take longer to return data.</p><p>In general, these systems take time to gather and process information and that&#8217;s just something we need to get used to.</p><p>You&#8217;ll be able to see the notes it gathers under a folder called &#8216;notes&#8217; in root later. You&#8217;ll find an example <a href="https://github.com/ilsilfverskiold/Awesome-LLM-Resources-List/tree/main/guides/langgraph/tech_news_bot_multiagent/notes">here</a>.</p><p>The final summary will look something like this:</p><pre><code>FINAL RESEARCH SUMMARY
Tech Research Summary

Weekly Tech Investor Brief (week ending Oct 28, 2025)

Key Happenings
Oct 27: ICE signed a $5.7M contract for AI-powered social media surveillance (Reddit: r/technology)
Oct 27: &#8220;Windows 10 deadline boosts Mac sales&#8221; thread trended, highlighting OS/device churn (Hacker News)
Oct 26: Microsoft 365 Copilot arbitrary data exfiltration via Mermaid diagrams disclosed (Hacker News)
Oct 26: &#8220;It&#8217;s insulting to read AI-generated blog posts&#8221; topped HN, reflecting AI content fatigue (Hacker News)
[...]

Why It Matters

AI demand is moving from hype to operational scrutiny. Government adoption (e.g., ICE&#8217;s Oct 27 contract) signals durable budgets for AI monitoring and analytics, but also heightens regulatory and civil liberties overhang&#8212;an opening for compliant AI, privacy-tech, and auditing vendors. Enterprise posts on Microsoft 365 Copilot exfiltration and Teams attendance monitoring underscore a near-term buyer focus on security, governance, and employee trust, not just raw AI features.
Platform competition intensified. Google&#8217;s Oct 23 Earth AI updates and Google AI Studio &#8220;vibe coding&#8221; push indicate a bid to reduce time-to-production for AI apps, a likely driver of cloud/TPSU demand and developer lock-in. Meanwhile, community gravitation to open and self-hosted stacks (ComfyUI momentum; S3-compatible storage chatter) reflects a cost-control and control-residency theme&#8212;relevant for hybrid vendors and open-core plays.
Consumer backlash is shaping product roadmaps. GM&#8217;s removal of CarPlay/Android Auto is trending because it challenges perceived table-stakes features, risking brand equity and sales. Apple&#8217;s reported ads in Maps and YouTube&#8217;s deepfake measures reflect a broader tension between monetization, safety, and user experience&#8212;areas where differentiated policy and design can become competitive moats.
[...]</code></pre><p>You can read the whole thing <a href="https://github.com/ilsilfverskiold/Awesome-LLM-Resources-List/blob/main/guides/langgraph/tech_news_bot_multiagent/example-summary.md">here</a> instead if you want to check it out without running this yourself.</p><p><em>The news will obviously vary depending on when you run the workflow.</em> <em>I ran it the 28th of October so the example report will be for this date.</em></p><p>As for the results, I&#8217;ll let you decide for yourself the difference between using a more complex system versus a simple one, and how it gives us more control over the process.</p><h3>Some ending notes</h3><p>I&#8217;m working with a good data source here. Without that, you&#8217;d need to add a lot more error handling, which would slow everything down even more.</p><p>Clean and structured data is key. Without it, the LLM won&#8217;t perform at its best. Even with solid data, it&#8217;s not perfect. You still need to work on the agents to make sure they do what they&#8217;re supposed to.</p><p>You&#8217;ve probably already noticed the system works but it&#8217;s not quite there yet. There are still several things that need improvement: parsing the user&#8217;s query into a more structured format and adding guardrails so agents always use their tools. We may also want to make sure the system will summarize more effectively to keep the research doc concise.</p><p>We need to introduce better error handling, and perhaps &#8220;long-term&#8221; memory to better understand what the user actually needs. State (short-term memory) is especially important if you want to optimize for performance and cost.</p><p>Right now, we&#8217;re just pushing every message into state and giving all agents access to it, which isn&#8217;t ideal. We really want to separate state between the teams. In this case, it&#8217;s something I haven&#8217;t done, but you can try it by introducing a scratchpad in the state schema to isolate what each team knows.</p><p>Regardless, I hope it was a fun experience to understand the results we can get by building different agentic systems. If you liked it, be sure to like, comment or share it.</p><p>&#10084;</p>]]></content:encoded></item><item><title><![CDATA[Fine-Tune Smaller Transformer Models: Text Classification]]></title><description><![CDATA[Using Microsoft&#8217;s Phi-3 to generate synthetic data]]></description><link>https://howtouseai.substack.com/p/fine-tune-smaller-transformer-models</link><guid isPermaLink="false">https://howtouseai.substack.com/p/fine-tune-smaller-transformer-models</guid><dc:creator><![CDATA[Ida Silfverskiold]]></dc:creator><pubDate>Tue, 01 Sep 2026 07:45:35 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!oCl5!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff82c3857-3bd2-4550-bd12-88fcf43929b6_1400x678.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!oCl5!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff82c3857-3bd2-4550-bd12-88fcf43929b6_1400x678.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!oCl5!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff82c3857-3bd2-4550-bd12-88fcf43929b6_1400x678.png 424w, /__u/substackcdn.com/image/fetch/$s_!oCl5!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff82c3857-3bd2-4550-bd12-88fcf43929b6_1400x678.png 848w, /__u/substackcdn.com/image/fetch/$s_!oCl5!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff82c3857-3bd2-4550-bd12-88fcf43929b6_1400x678.png 1272w, /__u/substackcdn.com/image/fetch/$s_!oCl5!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff82c3857-3bd2-4550-bd12-88fcf43929b6_1400x678.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!oCl5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff82c3857-3bd2-4550-bd12-88fcf43929b6_1400x678.png" width="1400" height="678" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f82c3857-3bd2-4550-bd12-88fcf43929b6_1400x678.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:678,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!oCl5!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff82c3857-3bd2-4550-bd12-88fcf43929b6_1400x678.png 424w, /__u/substackcdn.com/image/fetch/$s_!oCl5!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff82c3857-3bd2-4550-bd12-88fcf43929b6_1400x678.png 848w, /__u/substackcdn.com/image/fetch/$s_!oCl5!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff82c3857-3bd2-4550-bd12-88fcf43929b6_1400x678.png 1272w, /__u/substackcdn.com/image/fetch/$s_!oCl5!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff82c3857-3bd2-4550-bd12-88fcf43929b6_1400x678.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Build a smaller model from a bigger model to perform on a use case</figcaption></figure></div><p><em>This is an article dated May 2024 but has been re-published here.</em></p><p>Text classification models aren&#8217;t new, but the bar for how quickly they can be built and how well they perform has improved.</p><p>The transformer-based model I will fine-tune here is more than 1000 times smaller than GPT-3.5 Turbo. It will perform consistently better for this use case because it will be specifically trained for it.</p><p>The idea is to optimize AI workflows where smaller models excel, particularly in handling redundant tasks where larger models are simply overkill.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!YKjQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1db058af-e7d4-483d-a95b-5d98600b5d05_1400x654.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!YKjQ!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1db058af-e7d4-483d-a95b-5d98600b5d05_1400x654.png 424w, /__u/substackcdn.com/image/fetch/$s_!YKjQ!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1db058af-e7d4-483d-a95b-5d98600b5d05_1400x654.png 848w, /__u/substackcdn.com/image/fetch/$s_!YKjQ!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1db058af-e7d4-483d-a95b-5d98600b5d05_1400x654.png 1272w, /__u/substackcdn.com/image/fetch/$s_!YKjQ!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1db058af-e7d4-483d-a95b-5d98600b5d05_1400x654.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!YKjQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1db058af-e7d4-483d-a95b-5d98600b5d05_1400x654.png" width="1400" height="654" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1db058af-e7d4-483d-a95b-5d98600b5d05_1400x654.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:654,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!YKjQ!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1db058af-e7d4-483d-a95b-5d98600b5d05_1400x654.png 424w, /__u/substackcdn.com/image/fetch/$s_!YKjQ!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1db058af-e7d4-483d-a95b-5d98600b5d05_1400x654.png 848w, /__u/substackcdn.com/image/fetch/$s_!YKjQ!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1db058af-e7d4-483d-a95b-5d98600b5d05_1400x654.png 1272w, /__u/substackcdn.com/image/fetch/$s_!YKjQ!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1db058af-e7d4-483d-a95b-5d98600b5d05_1400x654.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Simplified demonstration of model sizes for fun</figcaption></figure></div><p>I&#8217;ve previously talked about <a href="https://medium.com/gitconnected/fine-tune-smaller-nlp-models-with-hugging-face-for-specific-use-cases-1745813471dc">this</a>, where I built a slightly larger <a href="https://huggingface.co/ilsilfverskiold/tech-keywords-extractor">keyword extractor</a> for tech-focused content using a sequence-to-sequence transformer model. I also went through the different <a href="https://github.com/ilsilfverskiold/smaller-models-docs/tree/main/nlp/docs">models</a> and what they excelled at.</p><p>For this piece, I&#8217;m diving into text classification with transformers, where encoder models do well. I&#8217;ll train a pre-trained encoder model with binary classes to identify clickbait versus factual articles. However, you may train it for a different use case.</p><p>You&#8217;ll find the finished model <a href="https://huggingface.co/ilsilfverskiold/classify-clickbait-titles">here</a>.</p><p>Most organizations use open-source LLMs such as Mistral and Llama to transform their datasets for training, but what I&#8217;ll do here is create the training data altogether using Phi-3 via Ollama.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!pfTp!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffccca88d-5ef5-46c5-9f17-a148ad97dab9_1400x681.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!pfTp!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffccca88d-5ef5-46c5-9f17-a148ad97dab9_1400x681.png 424w, /__u/substackcdn.com/image/fetch/$s_!pfTp!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffccca88d-5ef5-46c5-9f17-a148ad97dab9_1400x681.png 848w, /__u/substackcdn.com/image/fetch/$s_!pfTp!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffccca88d-5ef5-46c5-9f17-a148ad97dab9_1400x681.png 1272w, /__u/substackcdn.com/image/fetch/$s_!pfTp!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffccca88d-5ef5-46c5-9f17-a148ad97dab9_1400x681.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!pfTp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffccca88d-5ef5-46c5-9f17-a148ad97dab9_1400x681.png" width="1400" height="681" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fccca88d-5ef5-46c5-9f17-a148ad97dab9_1400x681.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:681,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!pfTp!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffccca88d-5ef5-46c5-9f17-a148ad97dab9_1400x681.png 424w, /__u/substackcdn.com/image/fetch/$s_!pfTp!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffccca88d-5ef5-46c5-9f17-a148ad97dab9_1400x681.png 848w, /__u/substackcdn.com/image/fetch/$s_!pfTp!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffccca88d-5ef5-46c5-9f17-a148ad97dab9_1400x681.png 1272w, /__u/substackcdn.com/image/fetch/$s_!pfTp!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffccca88d-5ef5-46c5-9f17-a148ad97dab9_1400x681.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Ideally you want more balanced data for training</figcaption></figure></div><p>There is always the risk that the model will overfit when using data from a large language model, but in this case, it performed fine, so I&#8217;m getting on the artificial data train. <em>However, you will have to be careful and look at the metrics once it is in training.</em></p><p>As for building a text classifier to identify clickbait titles, I think we can agree that some clickbait can be good as it keeps things interesting. I tried the finished model on various titles I made up, and found that having only factual content can be a bit dull.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!bp43!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba4f34c1-621d-4bb0-841f-a24975590cac_1400x595.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!bp43!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba4f34c1-621d-4bb0-841f-a24975590cac_1400x595.png 424w, /__u/substackcdn.com/image/fetch/$s_!bp43!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba4f34c1-621d-4bb0-841f-a24975590cac_1400x595.png 848w, /__u/substackcdn.com/image/fetch/$s_!bp43!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba4f34c1-621d-4bb0-841f-a24975590cac_1400x595.png 1272w, /__u/substackcdn.com/image/fetch/$s_!bp43!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba4f34c1-621d-4bb0-841f-a24975590cac_1400x595.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!bp43!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba4f34c1-621d-4bb0-841f-a24975590cac_1400x595.png" width="1400" height="595" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ba4f34c1-621d-4bb0-841f-a24975590cac_1400x595.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:595,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!bp43!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba4f34c1-621d-4bb0-841f-a24975590cac_1400x595.png 424w, /__u/substackcdn.com/image/fetch/$s_!bp43!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba4f34c1-621d-4bb0-841f-a24975590cac_1400x595.png 848w, /__u/substackcdn.com/image/fetch/$s_!bp43!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba4f34c1-621d-4bb0-841f-a24975590cac_1400x595.png 1272w, /__u/substackcdn.com/image/fetch/$s_!bp43!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba4f34c1-621d-4bb0-841f-a24975590cac_1400x595.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Making up a few titles to test the model</figcaption></figure></div><p>These issues always seem clear-cut, then you dive into them, and they are more nuanced than you considered. The question that popped into my head was, &#8216;What&#8217;s good clickbait content versus bad clickbait content?&#8217; A platform will probably need a bit of both to keep people reading.</p><blockquote><p>I used the new model on all my own content, and none of my titles were identified as clickbait. I&#8217;m not sure if that&#8217;s something good or not.</p></blockquote><p>If you&#8217;re new to transformer encoder models like BERT, this is a good learning experience. If you are not new to building text classification models with transformers, you might find it interesting to see if synthetic data worked well and to look at my performance metrics for this model.</p><p>As we all know, it&#8217;s easier to use fake data than to access the real thing.</p><h2>Introduction</h2><p>I got inspiration for this piece from <a href="https://medium.com/u/107d29e0e405?source=post_page---user_mention--77cbbd3bf02b---------------------------------------">Fabian Ridder</a> as he was using ChatGPT to identify clickbait and factual articles to train a model using <a href="https://fasttext.cc/">FastText</a>. I thought this case would be great for a smaller transformer model.</p><p>The model we&#8217;re building will use synthetic data rather than the real thing, though. The process will be quick, as it will only take about an hour or so to generate data with Phi-3 and a few minutes to train it. The model will be very small, with only 11M parameters.</p><p>As we&#8217;re using binary classes, i.e., clickbait or factual, we will be able to achieve 99% accuracy. The model will have the ability to interpret nuanced texts much better than FastText though.</p><p>The cost of training will be zero, and I have already prepared the <a href="https://huggingface.co/datasets/ilsilfverskiold/clickbait_titles_synthetic_data">dataset</a> that we&#8217;ll use for this. However, you may generate your own data for another use case.</p><p>If you want to dive into training the model, you can skip the introduction where I provide some information on encoder models and the tasks they excel in.</p><h3>Encoder Models &amp; What They Excel In</h3><p>While transformers have introduced amazing capabilities in text generation, they have also offered improvements within other NLP tasks, such as text classification and extraction.</p><p>The distinction between model architectures is a bit blurry but it&#8217;s useful to understand that different transformer models were originally built for different tasks.</p><p>A decoder model takes in a smaller input and outputs a larger text. GPT, which introduced impressive text generation back when, is a <strong>decoder model</strong>. A decoder primarily focuses on generating the next sentence rather than look at the text as a whole.</p><p>While larger language models offer more nuanced capabilities today, decoders were not built for tasks that involve extraction and labeling. For these tasks, we can use <strong>encoder models</strong>, which take in more input and provide a condensed output.</p><p>Encoders excel at extracting information by looking at the entire input at once to create a representation and thus are great at analyzing input data in its entirety.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!JPPL!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26399cb0-fa15-46ad-b1ec-23ad174d6a69_1400x745.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!JPPL!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26399cb0-fa15-46ad-b1ec-23ad174d6a69_1400x745.png 424w, /__u/substackcdn.com/image/fetch/$s_!JPPL!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26399cb0-fa15-46ad-b1ec-23ad174d6a69_1400x745.png 848w, /__u/substackcdn.com/image/fetch/$s_!JPPL!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26399cb0-fa15-46ad-b1ec-23ad174d6a69_1400x745.png 1272w, /__u/substackcdn.com/image/fetch/$s_!JPPL!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26399cb0-fa15-46ad-b1ec-23ad174d6a69_1400x745.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!JPPL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26399cb0-fa15-46ad-b1ec-23ad174d6a69_1400x745.png" width="1400" height="745" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/26399cb0-fa15-46ad-b1ec-23ad174d6a69_1400x745.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:745,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!JPPL!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26399cb0-fa15-46ad-b1ec-23ad174d6a69_1400x745.png 424w, /__u/substackcdn.com/image/fetch/$s_!JPPL!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26399cb0-fa15-46ad-b1ec-23ad174d6a69_1400x745.png 848w, /__u/substackcdn.com/image/fetch/$s_!JPPL!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26399cb0-fa15-46ad-b1ec-23ad174d6a69_1400x745.png 1272w, /__u/substackcdn.com/image/fetch/$s_!JPPL!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26399cb0-fa15-46ad-b1ec-23ad174d6a69_1400x745.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Smaller Transformer Models &#8212; Encoders vs Decoders</figcaption></figure></div><p>I won&#8217;t go into it any more than this, but there should be a lot of information you can scout on the topic, albeit it can be a bit technical. I do go into it a bit more in this <a href="https://medium.com/gitconnected/fine-tune-smaller-nlp-models-with-hugging-face-for-specific-use-cases-1745813471dc">article</a>.</p><p>So, what tasks are common with encoders? Some examples are classification &#8212; such as sentiment analysis and categorization &#8212; named entity recognition (NER), and keyword extraction, among others.</p><p>You can try a model that classifies text into twelve different emotions <a href="https://huggingface.co/SamLowe/roberta-base-go_emotions">here</a>. You can also look into a model that classifies hate speech as toxic <a href="https://huggingface.co/s-nlp/roberta_toxicity_classifier?text=he+is+a+nice+person">here</a>. Both of these were built with an encoder-only model, in this case, RoBERTa.</p><p>There are many base models you can work with; RoBERTa is a newer model that used more data for training and improved on BERT by optimizing its training techniques.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!Bmfz!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F311fcedd-c120-4a55-b335-9ea1b8733135_1400x776.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!Bmfz!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F311fcedd-c120-4a55-b335-9ea1b8733135_1400x776.png 424w, /__u/substackcdn.com/image/fetch/$s_!Bmfz!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F311fcedd-c120-4a55-b335-9ea1b8733135_1400x776.png 848w, /__u/substackcdn.com/image/fetch/$s_!Bmfz!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F311fcedd-c120-4a55-b335-9ea1b8733135_1400x776.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Bmfz!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F311fcedd-c120-4a55-b335-9ea1b8733135_1400x776.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!Bmfz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F311fcedd-c120-4a55-b335-9ea1b8733135_1400x776.png" width="1400" height="776" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/311fcedd-c120-4a55-b335-9ea1b8733135_1400x776.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:776,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!Bmfz!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F311fcedd-c120-4a55-b335-9ea1b8733135_1400x776.png 424w, /__u/substackcdn.com/image/fetch/$s_!Bmfz!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F311fcedd-c120-4a55-b335-9ea1b8733135_1400x776.png 848w, /__u/substackcdn.com/image/fetch/$s_!Bmfz!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F311fcedd-c120-4a55-b335-9ea1b8733135_1400x776.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Bmfz!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F311fcedd-c120-4a55-b335-9ea1b8733135_1400x776.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">The more known encoder transformer models &#8212;they come in different sizes</figcaption></figure></div><p>BERT was the first encoder-only transformer model, this one started it all by understanding language context much better than previous models. DistillBERT is a compressed version of BERT.</p><p>ALBERT uses some tricks to reduce the number of parameters, making it smaller without significantly losing performance. This is the one I&#8217;ll use for this case, as I think it will do well.</p><p>DeBERTA is an improved model that better understands word relationships and context. Generally, the bigger models will perform better on complex NLP tasks. However, they can more easily overfit if the training data is not diverse enough.</p><p>For this piece, I&#8217;m focusing on one task: text classification.</p><p>As for how hard is it to build a text classification model, it really depends on what you are asking it to do. When working with binary classes, you can achieve a high accuracy score in most cases. However, it also depends on how complex the use case is.</p><p>There are certain benchmarks you can look at to understand how BERT has performed with different open-source datasets.</p><p>I reviewed the paper &#8220;<a href="https://arxiv.org/abs/1905.05583">How to Fine-Tune BERT for Text Classification?</a>&#8221; to look at these benchmarks and graphed their accuracy score with the amount of labels they were trained with below.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!bcuy!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fc3805b-e400-4f25-979e-ac441ea12e84_1400x748.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!bcuy!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fc3805b-e400-4f25-979e-ac441ea12e84_1400x748.png 424w, /__u/substackcdn.com/image/fetch/$s_!bcuy!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fc3805b-e400-4f25-979e-ac441ea12e84_1400x748.png 848w, /__u/substackcdn.com/image/fetch/$s_!bcuy!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fc3805b-e400-4f25-979e-ac441ea12e84_1400x748.png 1272w, /__u/substackcdn.com/image/fetch/$s_!bcuy!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fc3805b-e400-4f25-979e-ac441ea12e84_1400x748.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!bcuy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fc3805b-e400-4f25-979e-ac441ea12e84_1400x748.png" width="1400" height="748" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4fc3805b-e400-4f25-979e-ac441ea12e84_1400x748.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:748,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!bcuy!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fc3805b-e400-4f25-979e-ac441ea12e84_1400x748.png 424w, /__u/substackcdn.com/image/fetch/$s_!bcuy!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fc3805b-e400-4f25-979e-ac441ea12e84_1400x748.png 848w, /__u/substackcdn.com/image/fetch/$s_!bcuy!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fc3805b-e400-4f25-979e-ac441ea12e84_1400x748.png 1272w, /__u/substackcdn.com/image/fetch/$s_!bcuy!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fc3805b-e400-4f25-979e-ac441ea12e84_1400x748.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Benchmarks datasets from the paper &#8220;<a href="https://arxiv.org/abs/1905.05583">How to Fine-Tune BERT for Text Classification?</a>&#8221;</figcaption></figure></div><p>We see datasets with only two labels do quite well. This is what we call binary labels. What might stand out is the <a href="https://huggingface.co/datasets/fancyzhx/dbpedia_14">DBpedia</a> dataset, which has 14 classes, yet achieved 98% accuracy as a benchmark, whereas the <a href="https://huggingface.co/datasets/yelp_review_full">Yelp Review Full</a> dataset, with only 5 classes, achieved only 70%.</p><p>Here&#8217;s where complexity comes in: Yelp reviews are very difficult to label, especially when rating stars between 1 and 5. Think about how difficult it is for a human to classify someone else&#8217;s text into a specific star rating; it really depends on how the person classifies their own reviews.</p><p>If you were to build a text classifier with the Yelp reviews dataset, you would find that 1-star and 5-star reviews are labeled correctly most of the time, but the model would struggle with 2, 3, and 4-star reviews. This is because what one person may classify as a 2-star review, the AI model might interpret as a 3-star review.</p><p>The DBpedia<strong> </strong>dataset on the other hand has texts that are easier to interpret for the model.</p><p>When we train a model, we can look at the metrics per label rather than as a whole to understand which labels are underperforming. Nevertheless, if you are working with a complex task, don&#8217;t feel discouraged if your metrics aren&#8217;t perfect.</p><p>Always try it afterwards on new data to see if it works well enough on your use case and keep working on the dataset, or switch the underlying model.</p><h3>The Economics of Smaller Models</h3><p>I always have a section on the cost of building and running a model. In any project, you&#8217;ll have to weigh resources and efficiency to get an outcome.</p><p>If you are just trying things out, then a<strong> bigger model</strong> with an API endpoint makes sense even though it will be <strong>computationally inefficient</strong>.</p><p>I have been running Claude Haiku to do natural language processing for a project now for a month, extracting category, topics and location from texts. This is for demonstration purposes only, but it makes sense when you want to prototype something for an organization.</p><p>However, doing zero-shot with these bigger models, will result in a lot of inconsistency, and some texts have to be disregarded altogether. Sometimes the bigger models will output absolute gibberish, but at the same time, it&#8217;s cheaper to run them for such a small project.</p><p>With your own models you will also have to host them, that&#8217;s why we spend so much time trying to make them smaller. You can naturally run them locally, but you&#8217;ll probably want to be able to use them for a development project so you&#8217;ll need to keep hosting costs in consideration.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!IrGC!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b0ad6b5-6ccb-4026-baea-a7739d91cee2_1400x851.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!IrGC!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b0ad6b5-6ccb-4026-baea-a7739d91cee2_1400x851.png 424w, /__u/substackcdn.com/image/fetch/$s_!IrGC!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b0ad6b5-6ccb-4026-baea-a7739d91cee2_1400x851.png 848w, /__u/substackcdn.com/image/fetch/$s_!IrGC!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b0ad6b5-6ccb-4026-baea-a7739d91cee2_1400x851.png 1272w, /__u/substackcdn.com/image/fetch/$s_!IrGC!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b0ad6b5-6ccb-4026-baea-a7739d91cee2_1400x851.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!IrGC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b0ad6b5-6ccb-4026-baea-a7739d91cee2_1400x851.png" width="1400" height="851" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7b0ad6b5-6ccb-4026-baea-a7739d91cee2_1400x851.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:851,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!IrGC!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b0ad6b5-6ccb-4026-baea-a7739d91cee2_1400x851.png 424w, /__u/substackcdn.com/image/fetch/$s_!IrGC!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b0ad6b5-6ccb-4026-baea-a7739d91cee2_1400x851.png 848w, /__u/substackcdn.com/image/fetch/$s_!IrGC!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b0ad6b5-6ccb-4026-baea-a7739d91cee2_1400x851.png 1272w, /__u/substackcdn.com/image/fetch/$s_!IrGC!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b0ad6b5-6ccb-4026-baea-a7739d91cee2_1400x851.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">We compare the amount of titles that can be processed per hour of hosting with API calls</figcaption></figure></div><p>Looking at the picture up top, I have calculated the amount of titles we can process for each instance and compared the same costs for GPT-3.5. I&#8217;m aware that it may look a bit messy, but alas it is hard to vizualise.</p><p>We can at least deduce that if we are sporadically using GPT-3.5 throughout the day for a small project, it makes sense to use it even though the costs to host the smaller model is quite low.</p><p>The breakpoint is when you are consistently processing so much data that surpasses a certain threshold. For this case, this would be when the titles to be processed exceeds 32,000 per day as the cost to keep the instance running 24/7 would equal the same price.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!LCa2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbedc4bcb-eeba-44ff-9070-aa87edd88f77_1400x505.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!LCa2!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbedc4bcb-eeba-44ff-9070-aa87edd88f77_1400x505.png 424w, /__u/substackcdn.com/image/fetch/$s_!LCa2!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbedc4bcb-eeba-44ff-9070-aa87edd88f77_1400x505.png 848w, /__u/substackcdn.com/image/fetch/$s_!LCa2!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbedc4bcb-eeba-44ff-9070-aa87edd88f77_1400x505.png 1272w, /__u/substackcdn.com/image/fetch/$s_!LCa2!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbedc4bcb-eeba-44ff-9070-aa87edd88f77_1400x505.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!LCa2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbedc4bcb-eeba-44ff-9070-aa87edd88f77_1400x505.png" width="1400" height="505" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/bedc4bcb-eeba-44ff-9070-aa87edd88f77_1400x505.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:505,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!LCa2!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbedc4bcb-eeba-44ff-9070-aa87edd88f77_1400x505.png 424w, /__u/substackcdn.com/image/fetch/$s_!LCa2!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbedc4bcb-eeba-44ff-9070-aa87edd88f77_1400x505.png 848w, /__u/substackcdn.com/image/fetch/$s_!LCa2!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbedc4bcb-eeba-44ff-9070-aa87edd88f77_1400x505.png 1272w, /__u/substackcdn.com/image/fetch/$s_!LCa2!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbedc4bcb-eeba-44ff-9070-aa87edd88f77_1400x505.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Comparison of costs using 1 vCPU for hosting vs API calls with GPT-3.5 for this case </figcaption></figure></div><p>This calculates as if you are keeping the instance running throughout the day, if you are only processing data at certain hours of the day, it makes sense to host and then scale down to zero when it is not in use. Since it&#8217;s so small, we can also just containerize it and then host it on ECS or even Lambda for serverless inference.</p><p>When using the closed sourced LLMs for zero-shot inference, we would also need to take into account that the model hasn&#8217;t been trained for this specific case so we may get inconsistent results. So for redundant tasks where you need consistency, building your own model is a better choice.</p><p>It is also worth noting that sometimes you need models that perform on more complex tasks. Here, the cost difference might be steeper for the larger LLMs as you&#8217;ll need a better model and a longer prompt template.</p><h3>Working with Synthetic Data</h3><p>Transforming data with the use of LLMs isn&#8217;t new, if you&#8217;re not doing it you should. This is much faster than manually transforming thousands of data points.</p><p>I looked at what Orange, the telecom giant, had done via their AI/NLP task force &#8212; NEPAL &#8212; and they had grabbed data from various places and transformed the raw texts into instruction-like formats using GPT-3.5 and Mixtral to create data that could be used for training.</p><p>If you&#8217;re keen to read more on this you can look at the session that is provided via Nvidia&#8217;s GTC <a href="https://www.nvidia.com/en-us/on-demand/session/gtc24-s62692/">here</a>.</p><p>But people are going further than this, using the larger language models to build the entire dataset; this is called<strong> synthetic data.</strong> It&#8217;s a smart way to build smaller specialized models with data that comes from the larger language models but that are cheaper and more efficient to host.</p><p>There are concerns at this though, where the quality of synthetic data can be questioned. Relying only on generated data might lead to models that miss nuances or biases inherent in real world data causing it to malfunction when it actually sees it.</p><p>However, it is much easier to generate synthetic data than to access the real thing.</p><h2>Building the Model</h2><p>I will embark on creating a very simple model here, the model is simply to identify titles as either clickbait or factual. You may build a different text classifier with more labels.</p><p>The process is straightforward and I&#8217;ll go through the entire process, the cook book we&#8217;ll work with is <a href="https://github.com/ilsilfverskiold/smaller-models-docs/blob/main/nlp/cook/fine-tune/albert_text_classification.ipynb">this</a> one.</p><p>This tutorial will use this <a href="https://huggingface.co/datasets/ilsilfverskiold/clickbait_titles_synthetic_data">dataset</a>, if you want to build your own dataset be sure to read the first section.</p><h3>The Dataset</h3><p>To create a synthetic dataset, we can boot up <a href="https://ollama.com/">Ollama</a> locally and run a model we want to use to build the training data. Make sure it is a commercially available model. I chose Phi-3, because it is small and it is very good.</p><p>I quite like Javascript, so I used the <a href="https://github.com/ollama/ollama-js">Ollama JS</a> framework to build a script that could run in the background to produce a CSV file.</p><p>This script creates clickbait titles and stores it in a new CSV in your root folder. You need to change the prompt template later to produce an equal amount of titles that are <strong>factual</strong>.</p><p>As I&#8217;m using a generative text model, Phi-3, some outputs won&#8217;t be usable, but that is to be expected. It will take some time for this to run, so go do something else with your time.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!Quax!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07932690-1039-4221-abc5-0b0116122258_1400x950.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!Quax!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07932690-1039-4221-abc5-0b0116122258_1400x950.png 424w, /__u/substackcdn.com/image/fetch/$s_!Quax!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07932690-1039-4221-abc5-0b0116122258_1400x950.png 848w, /__u/substackcdn.com/image/fetch/$s_!Quax!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07932690-1039-4221-abc5-0b0116122258_1400x950.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Quax!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07932690-1039-4221-abc5-0b0116122258_1400x950.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!Quax!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07932690-1039-4221-abc5-0b0116122258_1400x950.png" width="1400" height="950" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/07932690-1039-4221-abc5-0b0116122258_1400x950.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:950,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!Quax!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07932690-1039-4221-abc5-0b0116122258_1400x950.png 424w, /__u/substackcdn.com/image/fetch/$s_!Quax!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07932690-1039-4221-abc5-0b0116122258_1400x950.png 848w, /__u/substackcdn.com/image/fetch/$s_!Quax!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07932690-1039-4221-abc5-0b0116122258_1400x950.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Quax!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F07932690-1039-4221-abc5-0b0116122258_1400x950.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">My terminal for testing to generate data to a CSV</figcaption></figure></div><p>Once you&#8217;re finished you can store your finished CSV file with the clickbait and factual tiles in your Google Drive. Remember to set the text and label as fields, where the text is the title and the label is whether it is clickbait or factual.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!sjN_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc80bf96-adf8-449b-bbee-978581d7c479_1400x951.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!sjN_!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc80bf96-adf8-449b-bbee-978581d7c479_1400x951.png 424w, /__u/substackcdn.com/image/fetch/$s_!sjN_!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc80bf96-adf8-449b-bbee-978581d7c479_1400x951.png 848w, /__u/substackcdn.com/image/fetch/$s_!sjN_!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc80bf96-adf8-449b-bbee-978581d7c479_1400x951.png 1272w, /__u/substackcdn.com/image/fetch/$s_!sjN_!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc80bf96-adf8-449b-bbee-978581d7c479_1400x951.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!sjN_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc80bf96-adf8-449b-bbee-978581d7c479_1400x951.png" width="1400" height="951" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/dc80bf96-adf8-449b-bbee-978581d7c479_1400x951.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:951,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!sjN_!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc80bf96-adf8-449b-bbee-978581d7c479_1400x951.png 424w, /__u/substackcdn.com/image/fetch/$s_!sjN_!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc80bf96-adf8-449b-bbee-978581d7c479_1400x951.png 848w, /__u/substackcdn.com/image/fetch/$s_!sjN_!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc80bf96-adf8-449b-bbee-978581d7c479_1400x951.png 1272w, /__u/substackcdn.com/image/fetch/$s_!sjN_!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc80bf96-adf8-449b-bbee-978581d7c479_1400x951.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">How your dataset should be structured</figcaption></figure></div><p>Since I&#8217;ve already prepared the <a href="https://huggingface.co/datasets/ilsilfverskiold/clickbait_titles_synthetic_data">dataset</a> we&#8217;ll use, please see <a href="https://github.com/ilsilfverskiold/smaller-models-docs/blob/main/nlp/cook/datasets/push_custom_dataset_huggingface.ipynb">this</a> script to upload your custom dataset to HuggingFace.</p><blockquote><p>Looking through the dataset, you&#8217;ll see that most clickbait articles that have been generated by Phi-3 has an exclamation mark at the end of it. This is something you want to make sure doesn&#8217;t happen, so it&#8217;s important to check the work of the LLM generating the data.</p></blockquote><p>Remember that the script I provided you with splits your data into a training, test and validation set. I would recommend to have at least a training and test set for training the model.</p><p>If you&#8217;ve got your dataset sorted, we can go ahead and fine-tune the model.</p><h3>Dataset &amp; Model</h3><p>If you haven&#8217;t opened up the cook book, do so <a href="https://github.com/ilsilfverskiold/smaller-models-docs/blob/main/nlp/cook/fine-tune/albert_text_classification.ipynb">here</a>. The first part of this is deciding on your dataset and then your pre-trained model.</p><pre><code>from datasets import load_dataset, DatasetDict

dataset = load_dataset(&#8221;ilsilfverskiold/clickbait_titles_synthetic_data&#8221;)
dataset</code></pre><pre><code>model_name = &#8220;albert/albert-base-v2&#8221;
your_path = &#8220;classify-clickbait&#8221;</code></pre><p>I wen&#8217;t through the different models under the introduction section, where <a href="https://huggingface.co/albert/albert-base-v2">ALBERT</a> and DistillBERT are smaller models and BERT and RoBERTa are larger.</p><p>For this case, as it&#8217;s not overly complex, I will go for <a href="https://huggingface.co/albert/albert-base-v2">ALBERT</a>. I&#8217;m sure BERT can do better, but ALBERT is ten times smaller. RoBERTa is too big and may produce some overfitting with this dataset.</p><p>Remember, if you&#8217;re working with a different language then look for a base model that has been trained on a corpus from at least a similar language.</p><p>If you&#8217;re working with nordic languages I can recommend <a href="https://huggingface.co/KB/bert-base-swedish-cased">KB/bert-base-swedish-cased</a> that I used to create a model for the IPTC newscodes categories.</p><h3>Prepare The Dataset</h3><p>Now we need to do a few things for this to work well.</p><p>We first convert our labels to a standardized numerical format that the trainer will understand.</p><pre><code>from sklearn.preprocessing import LabelEncoder

label_encoder = LabelEncoder()

label_encoder.fit(dataset[&#8217;train&#8217;][&#8217;label&#8217;])
def encode_labels(example):
    return {&#8217;encoded_label&#8217;: label_encoder.transform([example[&#8217;label&#8217;]])[0]}
for split in dataset:
    dataset[split] = dataset[split].map(encode_labels, batched=False)</code></pre><p>Then we need to map the numerical representations back to the actual label names. This is so we can get the actual label names rather than the numerical reps when we do inference with the model.</p><pre><code>from transformers import AutoConfig

unique_labels = sorted(list(set(dataset[&#8217;train&#8217;][&#8217;label&#8217;])))
id2label = {i: label for i, label in enumerate(unique_labels)}
label2id = {label: i for i, label in enumerate(unique_labels)}

config = AutoConfig.from_pretrained(model_name)
config.id2label = id2label
config.label2id = label2id

# Verify the correct labels
print(&#8221;ID to Label Mapping:&#8221;, config.id2label)
print(&#8221;Label to ID Mapping:&#8221;, config.label2id)</code></pre><p>After this we&#8217;re ready to fetch the pre-trained model and it&#8217;s tokenizer. We use the config we set up with the labels when we import the model.</p><pre><code>from transformers import AlbertForSequenceClassification, AlbertTokenizer

tokenizer = AlbertTokenizer.from_pretrained(model_name)
model = AlbertForSequenceClassification.from_pretrained(model_name, config=config)</code></pre><p>If you&#8217;re using a different model such as BERT or RoBERTa, you can use AutoTokenizer and AutoModelForSequenceClassification which will automatically select the correct classes for your specified model.</p><p>This next function filters for invalid content and then makes sure the text data is properly tokenized and labeled, preparing the dataset for training.</p><pre><code>def filter_invalid_content(example):
    return isinstance(example[&#8217;text&#8217;], str)

dataset = dataset.filter(filter_invalid_content, batched=False)

def encode_data(batch):
    tokenized_inputs = tokenizer(batch[&#8221;text&#8221;], padding=True, truncation=True, max_length=256)
    tokenized_inputs[&#8221;labels&#8221;] = batch[&#8221;encoded_label&#8221;]
    return tokenized_inputs

dataset_encoded = dataset.map(encode_data, batched=True)
dataset_encoded</code></pre><pre><code>dataset_encoded.set_format(type=&#8217;torch&#8217;, columns=[&#8217;input_ids&#8217;, &#8216;attention_mask&#8217;, &#8216;labels&#8217;])</code></pre><p>We also need to fetch a data collator to handle padding for our inputs.</p><pre><code>from transformers import DataCollatorWithPadding

data_collator = DataCollatorWithPadding(tokenizer)</code></pre><h3>Evaluation Metrics</h3><p>It&#8217;s not required for you to set up any evaluation metrics, such as accuracy, precision, recall or f1. However, you do need at least accuracy to understand how the model is performing.</p><p><strong>Accuracy </strong>measures the amount of predictions the model got right across all categories. <strong>Precision </strong>measures how often predictions for a specific category are correct. <strong>Recall</strong> tells us how well the model can identify all instances within a specific category. The <strong>F1 Score</strong> is the weighted average of <strong>Precision</strong> and <strong>Recall</strong>.</p><p>I won&#8217;t go into detail on these metrics, but there are many others that <a href="https://towardsdatascience.com/accuracy-precision-recall-or-f1-331fb37c5cb9">write</a> about this. For this case, I&#8217;m more interested in how it performs on new real data rather than synthetic data. So, what I look out for are metrics that are too good, indicating that it has overfitted.</p><p>We do though set up a function that let us look at the accuracy for each label rather than as an average. This is much more relevant when you have many labels, rather than just two.</p><pre><code>from sklearn.preprocessing import LabelEncoderfrom sklearn.metrics import accuracy_score, confusion_matriximport numpy as np

label_encoder = LabelEncoder()
label_encoder.fit(unique_labels)
def per_label_accuracy(y_true, y_pred, labels):
    cm = confusion_matrix(y_true, y_pred, labels=labels)
    correct_predictions = cm.diagonal()
    label_totals = cm.sum(axis=1)
    per_label_acc = np.divide(correct_predictions, label_totals, out=np.zeros_like(correct_predictions, dtype=float), where=label_totals != 0)
    return dict(zip(labels, per_label_acc))</code></pre><p>We also set up the general compute metrics function. I am using all of these metrics here because this is general template I have for any text classifier, but you may decide which ones you want.</p><pre><code>from sklearn.metrics import accuracy_score, recall_score, precision_score, f1_score

def compute_metrics(pred):
    labels = pred.label_ids
    preds = pred.predictions.argmax(-1)

    decoded_labels = label_encoder.inverse_transform(labels)
    decoded_preds = label_encoder.inverse_transform(preds)

    precision = precision_score(decoded_labels, decoded_preds, average=&#8217;weighted&#8217;)
    recall = recall_score(decoded_labels, decoded_preds, average=&#8217;weighted&#8217;)
    f1 = f1_score(decoded_labels, decoded_preds, average=&#8217;weighted&#8217;)
    acc = accuracy_score(decoded_labels, decoded_preds)

    labels_list = list(label_encoder.classes_)
    per_label_acc = per_label_accuracy(decoded_labels, decoded_preds, labels_list)

    per_label_acc_metrics = {}
    for label, accuracy in per_label_acc.items():
        label_key = f&#8221;accuracy_label_{label}&#8221;
        per_label_acc_metrics[label_key] = accuracy

    return {
        &#8216;accuracy&#8217;: acc,
        &#8216;f1&#8217;: f1,
        &#8216;precision&#8217;: precision,
        &#8216;recall&#8217;: recall,
        **per_label_acc_metrics
    }</code></pre><p>Once you&#8217;re decently satisfied, we can move on to setting up the training arguments and the trainer.</p><h3>Training the Model</h3><p>Next up we set up our training arguments. Here you can tweak the epochs, batch size and learning rate.</p><pre><code>from transformers import Trainer, TrainingArguments

training_args = TrainingArguments(
    output_dir=your_path,
    num_train_epochs=3,
    warmup_steps=500,
    per_device_train_batch_size=16,
    per_device_eval_batch_size=16,
    weight_decay=0.01,
    logging_steps=10,
    evaluation_strategy=&#8217;steps&#8217;,
    eval_steps=100,
    learning_rate=2e-5,
    save_steps=1000,
    gradient_accumulation_steps=2
)</code></pre><p>I chose to go with a learning rate and epochs based on the paper &#8220;<a href="https://arxiv.org/abs/1905.05583">How to Fine-Tune BERT for Text Classification?</a>&#8221; but decreased the batch size.</p><p>Now we can go ahead and set up the trainer, with everything we&#8217;ve prepared, and run it.</p><pre><code>trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=dataset_encoded[&#8217;train&#8217;],
    eval_dataset=dataset_encoded[&#8217;test&#8217;],
    compute_metrics=compute_metrics,
    tokenizer=tokenizer,
    data_collator=data_collator,
)

trainer.train()</code></pre><p>When in training, you need to look out for overfitting. As both the training and evaluation datasets are synthetic, the typical signs of overfitting might be unclear.</p><p>Keep an eye on the accuracy and loss for both the training and evaluation datasets. I.e. very low training and validation loss, along with too stellar evaluation metrics could be a sign of overfitting.</p><p>But remember binary classes with less complex tasks usually perform well.</p><p>You&#8217;ll see my results for one run I made below.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!uh99!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F525d784a-1d3c-4cb1-9987-ece222eb935e_1400x405.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!uh99!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F525d784a-1d3c-4cb1-9987-ece222eb935e_1400x405.png 424w, /__u/substackcdn.com/image/fetch/$s_!uh99!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F525d784a-1d3c-4cb1-9987-ece222eb935e_1400x405.png 848w, /__u/substackcdn.com/image/fetch/$s_!uh99!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F525d784a-1d3c-4cb1-9987-ece222eb935e_1400x405.png 1272w, /__u/substackcdn.com/image/fetch/$s_!uh99!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F525d784a-1d3c-4cb1-9987-ece222eb935e_1400x405.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!uh99!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F525d784a-1d3c-4cb1-9987-ece222eb935e_1400x405.png" width="1400" height="405" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/525d784a-1d3c-4cb1-9987-ece222eb935e_1400x405.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:405,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!uh99!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F525d784a-1d3c-4cb1-9987-ece222eb935e_1400x405.png 424w, /__u/substackcdn.com/image/fetch/$s_!uh99!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F525d784a-1d3c-4cb1-9987-ece222eb935e_1400x405.png 848w, /__u/substackcdn.com/image/fetch/$s_!uh99!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F525d784a-1d3c-4cb1-9987-ece222eb935e_1400x405.png 1272w, /__u/substackcdn.com/image/fetch/$s_!uh99!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F525d784a-1d3c-4cb1-9987-ece222eb935e_1400x405.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Training metrics may look stellar but be careful with synthetic data</figcaption></figure></div><p>As you can see from the training metrics, they are a bit too good. Validation loss is fluctuating as well. This could be a very bad sign so you have to make sure to test the model on real data once it has finished training.</p><p>If you&#8217;re training a model with several classes, perhaps even with a skewed dataset, don&#8217;t worry if the average evaluation metrics aren&#8217;t great. Look at the metrics per label.</p><h3>Evaluating the Model</h3><p>Once it&#8217;s done training, you can run the final evaluation metrics, save the model and then save the state. This will build the metrics for when you push it to the hub for your model page.</p><pre><code>trainer.evaluate()
trainer.save_model(your_path)
trainer.save_state()</code></pre><p>Now you can run the HuggingFace pipeline in your notebook to test it.</p><pre><code>from transformers import pipeline
pipe = pipeline(&#8217;text-classification&#8217;, model=your_path)</code></pre><pre><code>example_titles = [
    &#8220;grab an example title&#8221;,
    &#8220;grab another example title&#8221;,
    &#8220;and another xample title&#8221;
]

for title in example_titles:
    result = pipe(title)
    print(f&#8221;Title: {title}&#8221;)
    print(f&#8221;Output: {result[0][&#8217;label&#8217;]}&#8221;)</code></pre><p>Mine did fine on test data, however it missed a few clickbait articles that I personally found to be clickbait. For a production use case, it&#8217;s better to build a more diverse dataset (especially with synthetic data) so it can perform well on new real data.</p><p>Nevertheless, if you&#8217;re not satisfied, then you go back to the dataset, redo it or try with a different model.</p><p>If you are wondering, I have indeed gotten stellar results on some runs and less-than-stellar results on other runs with the same data, the same training parameters, and the same seed.</p><h3>Testing the Model</h3><p>Before you push the model, you can also test the model against other alternatives.</p><p>I asked GPT-3.5 to tell me which titles it thought was clickbait and factual, and it did really well which is to be expected, it is more than 1000x larger than Albert.</p><p>We can also compare some titles to what a fine-tuned FastText says versus the fine-tuned transformer encoder model.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!COHG!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26b31a45-0945-493e-8af4-465252ae0468_1400x658.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!COHG!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26b31a45-0945-493e-8af4-465252ae0468_1400x658.png 424w, /__u/substackcdn.com/image/fetch/$s_!COHG!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26b31a45-0945-493e-8af4-465252ae0468_1400x658.png 848w, /__u/substackcdn.com/image/fetch/$s_!COHG!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26b31a45-0945-493e-8af4-465252ae0468_1400x658.png 1272w, /__u/substackcdn.com/image/fetch/$s_!COHG!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26b31a45-0945-493e-8af4-465252ae0468_1400x658.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!COHG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26b31a45-0945-493e-8af4-465252ae0468_1400x658.png" width="1400" height="658" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/26b31a45-0945-493e-8af4-465252ae0468_1400x658.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:658,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!COHG!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26b31a45-0945-493e-8af4-465252ae0468_1400x658.png 424w, /__u/substackcdn.com/image/fetch/$s_!COHG!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26b31a45-0945-493e-8af4-465252ae0468_1400x658.png 848w, /__u/substackcdn.com/image/fetch/$s_!COHG!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26b31a45-0945-493e-8af4-465252ae0468_1400x658.png 1272w, /__u/substackcdn.com/image/fetch/$s_!COHG!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F26b31a45-0945-493e-8af4-465252ae0468_1400x658.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Testing a few titles with the fine-tuned Albert model and FastText</figcaption></figure></div><p>Using <strong>FastText</strong> is very simple and computationally efficient, but it treats words in isolation and lacks deep contextual understanding.</p><p>Therefore, <strong>FastText</strong> doesn&#8217;t capture the context and nuances of language as well as a model that is transformer based.</p><h3>Push to the Hub</h3><p>If you&#8217;re satisfied with your model, you can push it to the HuggingFace hub to store it there.</p><p>You simply login with a <strong>write</strong> token you can find in your HuggingFace account under <strong>Settings</strong>.</p><pre><code>!huggingface-cli login</code></pre><p>And then push it.</p><pre><code>tokenizer.push_to_hub(&#8221;username/classify-clickbait&#8221;)
trainer.push_to_hub(&#8221;username/classify-clickbait&#8221;)</code></pre><p>Push the tokenizer just in case, especially if you are working with a version of Albert.</p><p>Now you can use it directly from there, mine you&#8217;ll find <a href="https://huggingface.co/ilsilfverskiold/classify-clickbait-titles">there</a>.</p><h3>Optimization Techniques</h3><p>If you want to use a larger model like BERT, you can apply different techniques so you can distill it further after fine-tuning. I didn&#8217;t find it that much more successful than just using ALBERT, at least for this case.</p><p>BERT on its own though performed much better in general. Although I really like RoBERTa for most cases, it was prone to overfit on this specific dataset either because it was too small, not good enough or too artificial.</p><p>For every case you&#8217;ll have to estimate how much performance you can sacrifice for efficiency and eventually you learn which models do well in what situation.</p><h3>Ending Notes</h3><p>Would the model have performed better if we had used real data? It&#8217;s possible, but the accuracy may be lower unless the dataset is meticulously sorted.</p><p>This is hard work.</p><p>Using synthetic data can get the job done very quickly so you get up a prototype to work with. Synthetic data is much cleaner to work with.</p><p>You are also free to work with the larger open source LLMs, so it doesn&#8217;t break any rules for people that can&#8217;t access high quality data without breaching protocol.</p><p>I did not put down time and effort into building this dataset, but in all cases you should make sure you have varied data the model can learn from.</p><p>Hopefully this piece was useful and gave you some inspiration and ideas on how to work with the smaller models.</p><p>&#10084;</p>]]></content:encoded></item><item><title><![CDATA[How to build an Over-Engineered Retrieval System]]></title><description><![CDATA[Which is actually how some people do it]]></description><link>https://howtouseai.substack.com/p/how-to-build-an-over-engineered-retrieval</link><guid isPermaLink="false">https://howtouseai.substack.com/p/how-to-build-an-over-engineered-retrieval</guid><dc:creator><![CDATA[Ida Silfverskiold]]></dc:creator><pubDate>Tue, 01 Sep 2026 07:36:37 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!c_sW!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff980fed3-782e-4004-954b-047636dba32d_1400x680.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!c_sW!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff980fed3-782e-4004-954b-047636dba32d_1400x680.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!c_sW!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff980fed3-782e-4004-954b-047636dba32d_1400x680.png 424w, /__u/substackcdn.com/image/fetch/$s_!c_sW!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff980fed3-782e-4004-954b-047636dba32d_1400x680.png 848w, /__u/substackcdn.com/image/fetch/$s_!c_sW!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff980fed3-782e-4004-954b-047636dba32d_1400x680.png 1272w, /__u/substackcdn.com/image/fetch/$s_!c_sW!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff980fed3-782e-4004-954b-047636dba32d_1400x680.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!c_sW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff980fed3-782e-4004-954b-047636dba32d_1400x680.png" width="1400" height="680" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f980fed3-782e-4004-954b-047636dba32d_1400x680.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:680,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!c_sW!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff980fed3-782e-4004-954b-047636dba32d_1400x680.png 424w, /__u/substackcdn.com/image/fetch/$s_!c_sW!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff980fed3-782e-4004-954b-047636dba32d_1400x680.png 848w, /__u/substackcdn.com/image/fetch/$s_!c_sW!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff980fed3-782e-4004-954b-047636dba32d_1400x680.png 1272w, /__u/substackcdn.com/image/fetch/$s_!c_sW!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff980fed3-782e-4004-954b-047636dba32d_1400x680.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><em>This is an article dated November 2025 but has been re-published here. </em></p><p>One of the things you&#8217;ll stumble upon when doing AI engineering work is that there&#8217;s no real blueprint to follow.</p><p>Yes, for the most basic parts of retrieval (the &#8220;R&#8221; in RAG), you can chunk documents, use semantic search on a query, re-rank the results, and so on. This part is well known.</p><p>But once you start digging into this area, you begin to ask questions like: <strong>how can we call a system intelligent if it&#8217;s only able to read a few chunks here and there in a document?</strong></p><p>So, <strong>how do we make sure it has enough information to actually answer intelligently?</strong></p><p>Soon, you&#8217;ll find yourself going down a rabbit hole, trying to discern what others are doing in their own orgs, because none of this is properly documented, and people are still building their own setups.</p><p>This will lead you to implement various optimization strategies: <strong>building custom chunkers</strong>, <strong>rewriting user queries</strong>, <strong>using different search methods</strong>, <strong>filtering with metadata</strong>, and <strong>expanding context</strong> to include neighboring chunks.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!80y9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F279b64b1-c1a7-4a4e-ae6c-9bf44d8ee2c4_1400x571.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!80y9!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F279b64b1-c1a7-4a4e-ae6c-9bf44d8ee2c4_1400x571.png 424w, /__u/substackcdn.com/image/fetch/$s_!80y9!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F279b64b1-c1a7-4a4e-ae6c-9bf44d8ee2c4_1400x571.png 848w, /__u/substackcdn.com/image/fetch/$s_!80y9!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F279b64b1-c1a7-4a4e-ae6c-9bf44d8ee2c4_1400x571.png 1272w, /__u/substackcdn.com/image/fetch/$s_!80y9!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F279b64b1-c1a7-4a4e-ae6c-9bf44d8ee2c4_1400x571.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!80y9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F279b64b1-c1a7-4a4e-ae6c-9bf44d8ee2c4_1400x571.png" width="1400" height="571" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/279b64b1-c1a7-4a4e-ae6c-9bf44d8ee2c4_1400x571.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:571,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!80y9!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F279b64b1-c1a7-4a4e-ae6c-9bf44d8ee2c4_1400x571.png 424w, /__u/substackcdn.com/image/fetch/$s_!80y9!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F279b64b1-c1a7-4a4e-ae6c-9bf44d8ee2c4_1400x571.png 848w, /__u/substackcdn.com/image/fetch/$s_!80y9!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F279b64b1-c1a7-4a4e-ae6c-9bf44d8ee2c4_1400x571.png 1272w, /__u/substackcdn.com/image/fetch/$s_!80y9!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F279b64b1-c1a7-4a4e-ae6c-9bf44d8ee2c4_1400x571.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Hence why I&#8217;ve now built a rather bloated retrieval system to show how some of these parts work. So, let&#8217;s walk through it so we can <strong>see the results of each step</strong>, but also to <strong>discuss the trade-offs</strong>.</p><p>To demo this system in public, I&#8217;ve embedded 150 recent ArXiv papers (2,250 pages) that mention RAG. This means the system we&#8217;re testing here is designed for scientific papers, and all the test queries will be RAG-related.</p><p>I have collected the raw outputs for each step for a few queries in this <a href="https://github.com/ilsilfverskiold/Awesome-LLM-Resources-List/tree/main/guides/RAG/Custom">repository</a>, if you want to look at the whole thing in detail.</p><p>For the tech stack, I&#8217;m using Qdrant and Redis to store data, and Cohere and OpenAI for the LLMs. I do not rely on any framework to build the pipelines (as it makes it harder to debug).</p><p><em>As always, I do a quick review of what we&#8217;re doing for beginners, so if RAG is already familiar to you, feel free to skip the first section.</em></p><h3>Recap retrieval &amp; RAG</h3><p>When you work with AI knowledge systems like Copilot (where you feed it your custom docs to answer from) you work with a RAG system.</p><p><strong>RAG</strong> stands for <strong>Retrieval Augmented Generation</strong> and is separated into two parts, the retrieval part and the generation part.</p><p><strong>Retrieval</strong> refers to the process of <strong>retrieving </strong>information, using keyword and semantic matching, based on a user query. <strong>The generation</strong> part is where<strong> the LLM comes in and answers</strong> based on the provided context and the query.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!u0E3!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8f1b18a-e6ce-426a-879e-4bb9417cb7c6_1400x545.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!u0E3!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8f1b18a-e6ce-426a-879e-4bb9417cb7c6_1400x545.png 424w, /__u/substackcdn.com/image/fetch/$s_!u0E3!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8f1b18a-e6ce-426a-879e-4bb9417cb7c6_1400x545.png 848w, /__u/substackcdn.com/image/fetch/$s_!u0E3!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8f1b18a-e6ce-426a-879e-4bb9417cb7c6_1400x545.png 1272w, /__u/substackcdn.com/image/fetch/$s_!u0E3!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8f1b18a-e6ce-426a-879e-4bb9417cb7c6_1400x545.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!u0E3!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8f1b18a-e6ce-426a-879e-4bb9417cb7c6_1400x545.png" width="1400" height="545" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b8f1b18a-e6ce-426a-879e-4bb9417cb7c6_1400x545.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:545,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!u0E3!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8f1b18a-e6ce-426a-879e-4bb9417cb7c6_1400x545.png 424w, /__u/substackcdn.com/image/fetch/$s_!u0E3!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8f1b18a-e6ce-426a-879e-4bb9417cb7c6_1400x545.png 848w, /__u/substackcdn.com/image/fetch/$s_!u0E3!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8f1b18a-e6ce-426a-879e-4bb9417cb7c6_1400x545.png 1272w, /__u/substackcdn.com/image/fetch/$s_!u0E3!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb8f1b18a-e6ce-426a-879e-4bb9417cb7c6_1400x545.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>For anyone new to RAG it may seem like a chunky way to build systems. Shouldn&#8217;t an LLM do most of the work on its own?</p><p>Unfortunately, LLMs are static, and we need to engineer systems so that each time we call on them, we give them everything they need upfront so they can answer the question.</p><p>I have written about building RAG bots for Slack <a href="https://medium.com/data-science-collective/agentic-rag-company-knowledge-slack-agents-98e588fd1209">before</a>. This one uses standard chunking methods, if you&#8217;re keen to get a sense of how people build something simple.</p><p>This article goes a <strong>step further</strong> and tries to rebuild the entire retrieval pipeline without any frameworks, to do some fancy stuff like build a multi-query optimizer, fuse results, and expand the chunks to build better context for the LLM.</p><p>As we&#8217;ll see though, all of<strong> these fancy additions we&#8217;ll have to pay for </strong>in<strong> latency </strong>and<strong> additional work.</strong></p><h2>Processing different documents</h2><p>As with any data engineering problem, your first hurdle will be to architect how to store data. With retrieval, we focus on something called chunking, and how you do it and what you store with it is essential to building a well-engineered system.</p><p>When we do retrieval, we search text, and to do that we need to separate the text into different chunks. These pieces of text are what we&#8217;ll later search to find a match for a query.</p><p>Most simple systems use general chunkers, simply splitting the full text by length, paragraph, or sentence.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!2b3Q!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72c2d383-3ea1-4d8e-95ad-c1f7a71d9d81_1400x819.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!2b3Q!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72c2d383-3ea1-4d8e-95ad-c1f7a71d9d81_1400x819.png 424w, /__u/substackcdn.com/image/fetch/$s_!2b3Q!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72c2d383-3ea1-4d8e-95ad-c1f7a71d9d81_1400x819.png 848w, /__u/substackcdn.com/image/fetch/$s_!2b3Q!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72c2d383-3ea1-4d8e-95ad-c1f7a71d9d81_1400x819.png 1272w, /__u/substackcdn.com/image/fetch/$s_!2b3Q!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72c2d383-3ea1-4d8e-95ad-c1f7a71d9d81_1400x819.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!2b3Q!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72c2d383-3ea1-4d8e-95ad-c1f7a71d9d81_1400x819.png" width="1400" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/72c2d383-3ea1-4d8e-95ad-c1f7a71d9d81_1400x819.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:819,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!2b3Q!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72c2d383-3ea1-4d8e-95ad-c1f7a71d9d81_1400x819.png 424w, /__u/substackcdn.com/image/fetch/$s_!2b3Q!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72c2d383-3ea1-4d8e-95ad-c1f7a71d9d81_1400x819.png 848w, /__u/substackcdn.com/image/fetch/$s_!2b3Q!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72c2d383-3ea1-4d8e-95ad-c1f7a71d9d81_1400x819.png 1272w, /__u/substackcdn.com/image/fetch/$s_!2b3Q!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F72c2d383-3ea1-4d8e-95ad-c1f7a71d9d81_1400x819.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>But every document is different, so by doing this you risk losing context.</p><p>To understand this, you should look at different documents to see how they all follow different structures. You&#8217;ll have an HR document with clear section headers, and API docs with unnumbered sections using code blocks and tables.</p><p>If you applied the same chunking logic to all of these, you&#8217;d risk splitting each text the wrong way. This means that once the LLM gets the chunks of information, it will be incomplete, which may cause it to fail at producing an accurate answer.</p><p>Furthermore, for each chunk of information, you also need to think about the data you want it to hold.</p><p>Should it contain certain metadata so the system can apply filters? Should it link to similar information so it can connect data? Should it hold context so the LLM understands where the information comes from?</p><p>This means the architecture of how you store data becomes the most important part. If you start storing information and later realize it&#8217;s not enough, you&#8217;ll have to redo it. If you realize you&#8217;ve complicated the system, you&#8217;ll have to start from scratch.</p><p>This system will ingest Excel and PDFs, focusing on adding context, keys, and neighbors. This will allow you to see what this looks like when doing retrieval later.</p><p><em>For this demo, I have stored data in Redis and Qdrant. We use Qdrant to do semantic, BM25, and hybrid search, and to expand content we fetch data from Redis.</em></p><h3>Ingesting tabular files</h3><p>First we&#8217;ll go through how you can chunk tabular data, add context, and keep information connected with keys.</p><p>When dealing with already structured tabular data, like in Excel files, it might seem like the obvious approach is to let the system search it directly. But semantic matching is actually quite effective for messy user queries.</p><p>SQL or direct queries only work if you already know the schema and exact fields. For instance, if you get a query like &#8220;Mazda 2023 specs&#8221; from a user, semantically matching rows will give us something to go on.</p><p>I&#8217;ve talked to companies that wanted their system to match documents across different Excel files. To do this, we can store keys along with the chunks (without going full KG).</p><p>So for instance, if we&#8217;re working with Excel files containing purchase data, we could ingest data for each row like so:</p><pre><code>{
    &#8220;chunk_id&#8221;: &#8220;Sales_Q1_123::row::1&#8221;,
    &#8220;doc_id&#8221;: &#8220;Sales_Q1_123:1234&#8221;
    &#8220;location&#8221;: {&#8221;sheet_name&#8221;: &#8220;Sales Q1&#8221;, &#8220;row_n&#8221;: 1},
    &#8220;type&#8221;: &#8220;chunk&#8221;,
    &#8220;text&#8221;: &#8220;OrderID: 1001234f67 \n Customer: Alice Hemsworth \n Products: Blue sweater 4, Red pants 6&#8221;,
    &#8220;context&#8221;: &#8220;Quarterly sales snapshot&#8221;,
    &#8220;keys&#8221;: {&#8221;OrderID&#8221;: &#8220;1001234f67&#8221;},
}</code></pre><p>If we decide later in the retrieval pipeline to connect information, we can do standard search using the keys to find connecting chunks. This allows us to make quick hops between documents without adding another router step to the pipeline.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!IuAY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F885406f7-6693-4051-b2d2-63d67d2bc714_1400x636.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!IuAY!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F885406f7-6693-4051-b2d2-63d67d2bc714_1400x636.png 424w, /__u/substackcdn.com/image/fetch/$s_!IuAY!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F885406f7-6693-4051-b2d2-63d67d2bc714_1400x636.png 848w, /__u/substackcdn.com/image/fetch/$s_!IuAY!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F885406f7-6693-4051-b2d2-63d67d2bc714_1400x636.png 1272w, /__u/substackcdn.com/image/fetch/$s_!IuAY!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F885406f7-6693-4051-b2d2-63d67d2bc714_1400x636.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!IuAY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F885406f7-6693-4051-b2d2-63d67d2bc714_1400x636.png" width="1400" height="636" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/885406f7-6693-4051-b2d2-63d67d2bc714_1400x636.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:636,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!IuAY!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F885406f7-6693-4051-b2d2-63d67d2bc714_1400x636.png 424w, /__u/substackcdn.com/image/fetch/$s_!IuAY!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F885406f7-6693-4051-b2d2-63d67d2bc714_1400x636.png 848w, /__u/substackcdn.com/image/fetch/$s_!IuAY!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F885406f7-6693-4051-b2d2-63d67d2bc714_1400x636.png 1272w, /__u/substackcdn.com/image/fetch/$s_!IuAY!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F885406f7-6693-4051-b2d2-63d67d2bc714_1400x636.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Very simplified &#8212; connecting keys between tabular documents | Image by author</p><p>We can also set a summary for each document. This acts as a gatekeeper to chunks.</p><pre><code>{
    &#8220;chunk_id&#8221;: &#8220;Sales_Q1::summary&#8221;,
    &#8220;doc_id&#8221;: &#8220;Sales_Q1_123:1234&#8221;
    &#8220;location&#8221;: {&#8221;sheet_name&#8221;: &#8220;Sales Q1&#8221;},
    &#8220;type&#8221;: &#8220;summary&#8221;,
    &#8220;text&#8221;: &#8220;Sheet tracks Q1 orders for 2025, type of product, and customer names for reconciliation.&#8221;,
    &#8220;context&#8221;: &#8220;&#8221;
}</code></pre><p>The gatekeeper summary idea might be a bit complicated to understand at first, but it also helps to have the summary stored at the document level if you need it when building the context later.</p><p>When the LLM sets up this summary (and a brief context string), it can suggest the key columns (i.e. order IDs and so on).</p><p><em>As a note, always set the key columns manually if you can, if that&#8217;s not possible, set up some validation logic to make sure the keys aren&#8217;t just random (it can happen that an LLM will choose weird columns to store while ignoring the most vital ones).</em></p><p>For this system with the ArXiv papers, I&#8217;ve ingested two Excel files that contain information on title and author level.</p><p>The chunks will look something like this:</p><pre><code>{
    &#8220;chunk_id&#8221;: &#8220;titles::row::8817::250930134607&#8221;,
    &#8220;doc_id&#8221;: &#8220;titles::250930134607&#8221;,
    &#8220;location&#8221;: {
      &#8220;sheet_name&#8221;: &#8220;titles&#8221;,
      &#8220;row_n&#8221;: 8817
    },
    &#8220;type&#8221;: &#8220;chunk&#8221;,
    &#8220;text&#8221;: &#8220;id: 2507 2114\ntitle: Gender Similarities Dominate Mathematical Cognition at the Neural Level: A Japanese fMRI Study Using Advanced Wavelet Analysis and Generative AI\nkeywords: FMRI; Functional Magnetic Resonance Imaging; Gender Differences; Machine Learning; Mathematical Performance; Time Frequency Analysis; Wavelet\nabstract_url: https://arxiv.org/abs/2507.21140\ncreated: 2025-07-23 00:00:00 UTC\nauthor_1: Tatsuru Kikuchi&#8221;,
    &#8220;context&#8221;: &#8220;Analyzing trends in AI and computational research articles.&#8221;,
    &#8220;keys&#8221;: {
      &#8220;id&#8221;: &#8220;2507 2114&#8221;,
      &#8220;author_1&#8221;: &#8220;Tatsuru Kikuchi&#8221;
    }
  }</code></pre><p>These Excel files were strictly not necessary (the PDF files would have been enough), but they&#8217;re a way to demo how the system can look up keys to find connecting information.</p><p>I created summaries for these files too.</p><pre><code>{
    &#8220;chunk_id&#8221;: &#8220;titles::summary::250930134607&#8221;,
    &#8220;doc_id&#8221;: &#8220;titles::250930134607&#8221;,
    &#8220;location&#8221;: {
      &#8220;sheet_name&#8221;: &#8220;titles&#8221;
    },
    &#8220;type&#8221;: &#8220;summary&#8221;,
    &#8220;text&#8221;: &#8220;The dataset consists of articles with various attributes including ID, title, keywords, authors, and publication date. It contains a total of 2508 rows with a rich variety of topics predominantly around AI, machine learning, and advanced computational methods. Authors often contribute in teams, indicated by multiple author columns. The dataset serves academic and research purposes, enabling catego&#8221;,
  }</code></pre><p>We also store information in Redis at document level, which tells us what it&#8217;s about, where to find it, who is allowed to see it, and when it was last updated. This will allow us to update stale information later.</p><p>Now let&#8217;s turn to PDF files, which are the worst monster you&#8217;ll deal with.</p><h3>Ingesting PDF docs</h3><p>To process PDF files, we do similar things as with tabular data, but chunking them is much harder, and we store neighbors instead of keys.</p><p>To start processing PDFs, we have several frameworks to work with, such as LlamaParse and Docling, but none of them are perfect, so we have to build out the system further.</p><p>PDF documents are very hard to process, as most don&#8217;t follow the same structure. They also often contain figures and tables that most systems can&#8217;t handle correctly.</p><p>Nevertheless, a tool like <strong>Docling </strong>can help us at least parse normal tables properly and map out each element to the correct page and element number.</p><p>From here, we can create our own programmatic logic by mapping sections and subsections for each element, adding titles to each chunk, and smart-merging snippets so chunks read naturally (i.e. don&#8217;t split mid-sentence).</p><p>We also make sure to group chunks by section, keeping them together by linking their IDs in a field called <em>neighbors</em>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!BL9s!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0826e6a2-e171-440a-9f8a-731f3db801c2_1400x739.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!BL9s!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0826e6a2-e171-440a-9f8a-731f3db801c2_1400x739.png 424w, /__u/substackcdn.com/image/fetch/$s_!BL9s!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0826e6a2-e171-440a-9f8a-731f3db801c2_1400x739.png 848w, /__u/substackcdn.com/image/fetch/$s_!BL9s!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0826e6a2-e171-440a-9f8a-731f3db801c2_1400x739.png 1272w, /__u/substackcdn.com/image/fetch/$s_!BL9s!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0826e6a2-e171-440a-9f8a-731f3db801c2_1400x739.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!BL9s!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0826e6a2-e171-440a-9f8a-731f3db801c2_1400x739.png" width="1400" height="739" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0826e6a2-e171-440a-9f8a-731f3db801c2_1400x739.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:739,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!BL9s!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0826e6a2-e171-440a-9f8a-731f3db801c2_1400x739.png 424w, /__u/substackcdn.com/image/fetch/$s_!BL9s!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0826e6a2-e171-440a-9f8a-731f3db801c2_1400x739.png 848w, /__u/substackcdn.com/image/fetch/$s_!BL9s!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0826e6a2-e171-440a-9f8a-731f3db801c2_1400x739.png 1272w, /__u/substackcdn.com/image/fetch/$s_!BL9s!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0826e6a2-e171-440a-9f8a-731f3db801c2_1400x739.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This allows us to keep the chunks small but still expand them after retrieval.</p><p>The end result will be something like below:</p><pre><code>{
    &#8220;chunk_id&#8221;: &#8220;S3::C02::251009105423&#8221;,
    &#8220;doc_id&#8221;: &#8220;2507.18910v1&#8221;,
    &#8220;location&#8221;: {
      &#8220;page_start&#8221;: 2,
      &#8220;page_end&#8221;: 2
    },
    &#8220;type&#8221;: &#8220;chunk&#8221;,
    &#8220;text&#8221;: &#8220;1 Introduction\n\n1.1 Background and Motivation\n\nLarge-scale pre-trained language models have demonstrated an ability to store vast amounts of factual knowledge in their parameters, but they struggle with accessing up-to-date information and providing verifiable sources. This limitation has motivated techniques that augment generative models with information retrieval. Retrieval-Augmented Generation (RAG) emerged as a solution to this problem, combining a neural retriever with a sequence-to-sequence generator to ground outputs in external documents [52]. The seminal work of [52] introduced RAG for knowledge-intensive tasks, showing that a generative model (built on a BART encoder-decoder) could retrieve relevant Wikipedia passages and incorporate them into its responses, thereby achieving state-of-the-art performance on open-domain question answering. RAG is built upon prior efforts in which retrieval was used to enhance question answering and language modeling [48, 26, 45]. Unlike earlier extractive approaches, RAG produces free-form answers while still leveraging non-parametric memory, offering the best of both worlds: improved factual accuracy and the ability to cite sources. This capability is especially important to mitigate hallucinations (i.e., believable but incorrect outputs) and to allow knowledge updates without retraining the model [52, 33].&#8221;,
    &#8220;context&#8221;: &#8220;Systematic review of RAG&#8217;s development and applications in NLP, addressing challenges and advancements.&#8221;,
    &#8220;section_neighbours&#8221;: {
      &#8220;before&#8221;: [
        &#8220;S3::C01::251009105423&#8221;
      ],
      &#8220;after&#8221;: [
        &#8220;S3::C03::251009105423&#8221;,
        &#8220;S3::C04::251009105423&#8221;,
        &#8220;S3::C05::251009105423&#8221;,
        &#8220;S3::C06::251009105423&#8221;,
        &#8220;S3::C07::251009105423&#8221;
      ]
    },
    &#8220;keys&#8221;: {}
  }</code></pre><p>When we set up data like this, we can consider these chunks as seeds. We are searching for where there may be relevant information based on the user query, and expanding from there.</p><p>The difference from simpler RAG systems is that we try to take advantage of the LLM&#8217;s growing context window to send in more information (but there are obviously trade offs to this).</p><p>You&#8217;ll be able to see a messy solution of what this looks like when building the context in the retrieval pipeline later.</p><h2>Building the retrieval pipeline</h2><p>Since I&#8217;ve built this pipeline piece by piece, it allows us to test each part and go through why we make certain choices in how we retrieve and transform information before handing it over to the LLM.</p><p>We&#8217;ll go through semantic, hybrid, and BM25 search, building a multi-query optimizer, re-ranking results, expanding content to build the context, and then handing the results to an LLM to answer.</p><p>We&#8217;ll end the section with some discussion on latency, unnecessary complexity, and what to cut to make the system faster.</p><p>If you want to look at the output of several runs of this pipeline, go to this <a href="https://medium.com/data-science-collective/agentic-rag-company-knowledge-slack-agents-98e588fd1209">repository</a>.</p><h3>Semantic and hybrid search</h3><p>The first part of this pipeline is to make sure we are getting back relevant documents for a user query. To do this, we work with semantic, BM25, and hybrid search.</p><p>For simple retrieval systems, people will usually just use semantic search. To perform semantic search, we embed dense vectors for each chunk of text using an embedding model.</p><p><em>If this is new to you, note that embeddings represent each piece of text as a point in a high-dimensional space. The position of each point reflects how the model understands its meaning, based on patterns it learned during training.</em></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!ls0X!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6e3315b2-164d-41f3-8d33-7ae2fe76c168_1400x1003.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!ls0X!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6e3315b2-164d-41f3-8d33-7ae2fe76c168_1400x1003.png 424w, /__u/substackcdn.com/image/fetch/$s_!ls0X!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6e3315b2-164d-41f3-8d33-7ae2fe76c168_1400x1003.png 848w, /__u/substackcdn.com/image/fetch/$s_!ls0X!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6e3315b2-164d-41f3-8d33-7ae2fe76c168_1400x1003.png 1272w, /__u/substackcdn.com/image/fetch/$s_!ls0X!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6e3315b2-164d-41f3-8d33-7ae2fe76c168_1400x1003.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!ls0X!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6e3315b2-164d-41f3-8d33-7ae2fe76c168_1400x1003.png" width="1400" height="1003" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6e3315b2-164d-41f3-8d33-7ae2fe76c168_1400x1003.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1003,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!ls0X!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6e3315b2-164d-41f3-8d33-7ae2fe76c168_1400x1003.png 424w, /__u/substackcdn.com/image/fetch/$s_!ls0X!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6e3315b2-164d-41f3-8d33-7ae2fe76c168_1400x1003.png 848w, /__u/substackcdn.com/image/fetch/$s_!ls0X!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6e3315b2-164d-41f3-8d33-7ae2fe76c168_1400x1003.png 1272w, /__u/substackcdn.com/image/fetch/$s_!ls0X!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6e3315b2-164d-41f3-8d33-7ae2fe76c168_1400x1003.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Texts with similar meanings will then end up close together.</p><p>This means that if the model has seen many examples of similar language, it becomes better at placing related texts near each other, and therefore better at matching a query with the most relevant content.</p><p><em>I have written about <a href="https://medium.com/data-science/working-with-embeddings-closed-versus-open-source-39491f0b95c2">this before,</a> using clustering on various embeddings models to see how they performed for a use case, if you&#8217;re keen to learn more.</em></p><p>To create dense vectors, I used OpenAI&#8217;s Large embedding model, since I&#8217;m working with scientific papers. This model is more expensive than their small one and perhaps not ideal for this use case.</p><p>I would look into <strong>specialized models for specific domains</strong> or consider fine-tuning your own. Because remember if the embedding model hasn&#8217;t seen many examples similar to the texts you&#8217;re embedding, it will be harder to match them to relevant documents.</p><p>To support hybrid and BM25 search, we also build a lexical index (sparse vectors). BM25 works on exact tokens (for example, &#8220;ID 826384&#8221;) instead of returning &#8220;similar-meaning&#8221; text the way semantic search does.</p><p><em>Do remember that BM25 does not match partial tokens, if users ask for an ID that starts with 8263 it may match semantically but it&#8217;s not a guarantee.</em></p><p>To test semantic search, we&#8217;ll set up a query that I think the papers we&#8217;ve ingested can answer, such as: <em>&#8220;Why do LLMs get worse with longer context windows and what to do about it?&#8221;</em></p><pre><code>[1] score=0.5071 doc=docs_ingestor/docs/arxiv/2508.15253.pdf chunk=S3::C02::251009131027
  text: 1 Introduction This challenge is exacerbated when incorrect yet highly ranked contexts serve as hard negatives. (...)
[2] score=0.5022 doc=docs_ingestor/docs/arxiv/2508.19614.pdf chunk=S3::C03::251009132038
  text: 1 Introductions Despite these advances, LLMs might underutilize accurate external contexts, disproportionately favoring internal parametric knowledge during generation [50, 40]. This overreliance risks propagating outdated information or hallucinations, undermining the trustworthiness of RAG systems. Surprisingly, recent studies reveal a paradoxical phenomenon: injecting noise-random documents or tokens-to retrieved contexts that already contain answer-relevant snippets can improve the generation accuracy (...)
[3] score=0.4982 doc=docs_ingestor/docs/arxiv/2508.19614.pdf chunk=S6::C18::251009132038
  text: 4 Experiments 4.3 Analysis Experiments Qualitative Study In Table 4, we analyze a case study from the NQ dataset using the Llama2-7B model, evaluating four decoding strategies: GD(0), CS, DoLA, and LFD. Despite access to groundtruth documents, both GD(0) and DoLA generate incorrect answers (e.g., &#8216;18 minutes&#8217;), suggesting limited capacity to integrate contextual evidence (...)
[4] score=0.4857 doc=docs_ingestor/docs/arxiv/2507.23588.pdf chunk=S6::C03::251009122456
  text: 4 Results Figure 4: Change in attention pattern distribution in different models. For DiffLoRA variants we plot attention mass for main component (green) and denoiser component (yellow). Note that attention mass is normalized by the number of tokens in each part of the sequence. The negative attention is shown after it is scaled by &#955; (...)
[5] score=0.4838 doc=docs_ingestor/docs/arxiv/2508.15253.pdf chunk=S3::C03::251009131027
  text: 1 Introduction To mitigate context-memory conflict, existing studies such as adaptive retrieval (Ren et al., 2025; Baek et al., 2025) and the decoding strategies (Zhao et al., 2024; Han et al., 2025) adjust the influence of external context either before or during answer generation. However, due to the LLM&#8217;s limited capacity in detecting conflicts, (...)
[6] score=0.4827 doc=docs_ingestor/docs/arxiv/2508.05266.pdf chunk=S27::C03::251009123532
  text: B. Subclassification Criteria for Misinterpretation of Design Specifications Initially, regarding long-context scenarios, we observed that directly prompting LLMs to generate RTL code based on lengthy contexts often resulted in certain code segments failing to accurately reflect high-level requirements. (...)
[7] score=0.4798 doc=docs_ingestor/docs/arxiv/2508.19614.pdf chunk=S3::C02::251009132038
  text: 1 Introductions Figure 1: Illustration for layer-wise behavior in LLMs for RAG. Given a query and retrieved documents with the correct answer (&#8217;Real Madrid&#8217;), shallow layers capture local context, middle layers focus on answer-relevant content, while deep layers may over-rely on internal knowledge and hallucinate (e.g., &#8216;Barcelona&#8217;). Our proposal, LFD fuses middle-layer signals into the final output to preserve external knowledge and improve accuracy.(...)</code></pre><p>From the results above, we can see that it&#8217;s able to match some interesting passages where they discuss topics that can answer the query but not all are the most relevant.</p><p>If we try BM25 (which matches exact tokens) with the same query, we get back these results:</p><pre><code>[1] score=22.0764 doc=docs_ingestor/docs/arxiv/2507.20888.pdf chunk=S4::C27::251009115003
  text: 3 APPROACH 3.2.2 Project Knowledge Retrieval Similar Code Retrieval. Similar snippets within the same project are valuable for code completion, even if they are not entirely replicable. In this step, we also retrieve similar code snippets. (...)
[2] score=17.4931 doc=docs_ingestor/docs/arxiv/2508.09105.pdf chunk=S20::C08::251009124222
  text: C. Ablation Studies Ablation result across White-Box attribution: Table V shows the comparison result in methods of WhiteBox Attribution with Noise, White-Box Attrition with Alternative Model and our current method Black-Box zero-gradient (...)
[3] score=17.1458 doc=docs_ingestor/docs/arxiv/2508.05100.pdf chunk=S4::C03::251009123245
  text: Preliminaries Based on this, inspired by existing analyses (Zhang et al. 2024c), we measure the amount of information a position receives using discrete entropy, as shown in the following equation: which quantifies how much information t i receives from the attention perspective. This insight suggests that LLMs struggle with longer sequences when not trained on them (...)</code></pre><p>Here, the results are lackluster for this query except for the third one but sometimes queries include specific keywords we need to match, where BM25 is the better choice.</p><p>We can test this by changing the query to<em> &#8220;papers from Anirban Saha Anik&#8221; </em>using BM25.</p><pre><code>[1] score=62.3398 doc=authors.csv chunk=authors::row::1::251009110024
  text: author_name: Anirban Saha Anik n_papers: 2 article_1: 2509.01058 article_2: 2507.07307
[2] score=56.4007 doc=titles.csv chunk=titles::row::24::251009110138
  text: id: 2509.01058 title: Speaking at the Right Level: Literacy-Controlled Counterspeech Generation with RAG-RL keywords. UTC author_1: Xiaoying Song author_2: Anirban Saha Anik author_3: Dibakar Barua author_4: Pengcheng Luo author_5: Junhua Ding author_6: Lingzi Hong
[3] score=56.2614 doc=titles.csv chunk=titles::row::106::251009110138
  text: id: 2507.07307 title: Multi-Agent Retrieval-Augmented Framework for Evidence-Based Counterspeech Against Health Misinformation keywords ... author_1: Anirban Saha Anik author_2: Xiaoying Song author_3: Elliott Wang author_4: Bryan Wang author_5: Bengisu Yarimbas author_6: Lingzi Hong</code></pre><p>All the results above mention &#8220;Anirban Saha Anik,&#8221; which is exactly what we&#8217;re looking for.</p><p>If we ran this with semantic search, it would return not just the name &#8220;Anirban Saha Anik&#8221; but similar names as well.</p><pre><code>[1] score=0.5810 doc=authors.csv chunk=authors::row::1::251009110024
  text: author_name: Anirban Saha Anik n_papers: 2 article_1: 2509.01058 article_2: 2507.07307
[2] score=0.4499 doc=authors.csv chunk=authors::row::55::251009110024
  text: author_name: Anand A. Rajasekar n_papers: 1 article_1: 2508.0199
[3] score=0.4320 doc=authors.csv chunk=authors::row::59::251009110024
  text: author_name: Anoop Mayampurath n_papers: 1 article_1: 2508.14817
[4] score=0.4306 doc=authors.csv chunk=authors::row::69::251009110024
  text: author_name: Avishek Anand n_papers: 1 article_1: 2508.15437
[5] score=0.4215 doc=authors.csv chunk=authors::row::182::251009110024
  text: author_name: Ganesh Ananthanarayanan n_papers: 1 article_1: 2509.14608</code></pre><p>This is a good example of how semantic search isn&#8217;t always the ideal method as similar names don&#8217;t necessarily mean they&#8217;re relevant to the query.</p><p>So, there are cases where semantic search is ideal, and others where BM25 (token matching) is the better choice.</p><p>We can also use hybrid search, which combines semantic and BM25.</p><p>You&#8217;ll see the results below from running hybrid search on the original query: <em>&#8220;why do LLMs get worse with longer context windows and what to do about it?&#8221;</em></p><pre><code>[1] score=0.5000 doc=docs_ingestor/docs/arxiv/2508.15253.pdf chunk=S3::C02::251009131027
  text: 1 Introduction This challenge is exacerbated when incorrect yet highly ranked contexts serve as hard negatives... This misalignment leads to overriding correct internal representations, resulting in substantial performance degradation on questions that the model initially answered correctly...
[2] score=0.5000 doc=docs_ingestor/docs/arxiv/2507.20888.pdf chunk=S4::C27::251009115003
  text: 3 APPROACH 3.2.2 Project Knowledge Retrieval Similar Code Retrieval. (...)
[3] score=0.4133 doc=docs_ingestor/docs/arxiv/2508.19614.pdf chunk=S3::C03::251009132038
  text: 1 Introductions Despite these advances, LLMs might underutilize accurate external contexts, disproportionately favoring internal parametric knowledge during generation [50, 40]. This overreliance risks propagating outdated information or hallucinations, undermining the trustworthiness of RAG systems. (...)
[4] score=0.1813 doc=docs_ingestor/docs/arxiv/2508.19614.pdf chunk=S6::C18::251009132038
  text: 4 Experiments 4.3 Analysis Experiments Qualitative Study In Table 4, we analyze a case study from the NQ dataset using the Llama2-7B model, evaluating four decoding strategies.. suggesting limited capacity to integrate contextual evidence. Similarly, while CS produces a partially relevant response (&#8217;Texas Revolution&#8217;), it exhibits reduced factual consistency with the source material. In contrast, LFD demonstrates superior utilization of retrieved context...</code></pre><p>I found semantic search worked best for this query, which is why it can be useful to run multi-queries with different search methods to fetch the first chunks (though this also adds complexity).</p><p>So, let&#8217;s turn to building something that can transform the original query into several optimized versions, and fuse the results.</p><h3>Multi-query optimizer</h3><p>For this part we look at how we can optimize messy user queries by generating multiple targeted variations and selecting the right search method for each. It can improve recall but it introduces trade-offs.</p><p>All the agent abstraction systems you see usually transform the user query when performing search.</p><p>For example, when you use the QueryTool in LlamaIndex, it uses an LLM to optimize the incoming query.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!_P0V!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb06c87e8-0fbb-4b5f-b5d5-f422078f6db8_1400x505.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!_P0V!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb06c87e8-0fbb-4b5f-b5d5-f422078f6db8_1400x505.png 424w, /__u/substackcdn.com/image/fetch/$s_!_P0V!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb06c87e8-0fbb-4b5f-b5d5-f422078f6db8_1400x505.png 848w, /__u/substackcdn.com/image/fetch/$s_!_P0V!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb06c87e8-0fbb-4b5f-b5d5-f422078f6db8_1400x505.png 1272w, /__u/substackcdn.com/image/fetch/$s_!_P0V!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb06c87e8-0fbb-4b5f-b5d5-f422078f6db8_1400x505.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!_P0V!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb06c87e8-0fbb-4b5f-b5d5-f422078f6db8_1400x505.png" width="1400" height="505" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b06c87e8-0fbb-4b5f-b5d5-f422078f6db8_1400x505.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:505,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!_P0V!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb06c87e8-0fbb-4b5f-b5d5-f422078f6db8_1400x505.png 424w, /__u/substackcdn.com/image/fetch/$s_!_P0V!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb06c87e8-0fbb-4b5f-b5d5-f422078f6db8_1400x505.png 848w, /__u/substackcdn.com/image/fetch/$s_!_P0V!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb06c87e8-0fbb-4b5f-b5d5-f422078f6db8_1400x505.png 1272w, /__u/substackcdn.com/image/fetch/$s_!_P0V!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb06c87e8-0fbb-4b5f-b5d5-f422078f6db8_1400x505.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>We can rebuild this part ourselves, but instead we give it the ability to create multiple queries, while also setting the search method. <em>When you&#8217;re working with more documents, you could also have it set filters at this stage.</em></p><p>As for creating a lot of queries, I would try to keep it simple, as issues here will cause low-quality outputs in retrieval. The more unrelated queries the system generates, the more noise it introduces into the pipeline.</p><p>The function I&#8217;ve created here will generate 1&#8211;3 academic-style queries, along with the search method to be used, based on a messy user query.</p><pre><code>Original query:
why is everyone saying RAG doesn&#8217;t scale? how are people fixing that?

Generated queries:
- hybrid: RAG scalability issues
- hybrid: solutions to RAG scaling challenges</code></pre><p>We will get back results like these:</p><pre><code>Query 1 (hybrid) top 20 for query: RAG scalability issues

[1] score=0.5000 doc=docs_ingestor/docs/arxiv/2507.18910.pdf chunk=S22::C05::251104142800
  text: 7 Challenges of RAG 7.2.1 Scalability and Infrastructure Deploying RAG at scale requires substantial engineering to maintain large knowledge corpora and efficient retrieval indices...
[2] score=0.5000 doc=docs_ingestor/docs/arxiv/2507.07695.pdf chunk=SDOC::SUM::251104135247
  text: This paper proposes the KeyKnowledgeRAG (K2RAG) framework to enhance the efficiency and accuracy of Retrieval-Augment-Generate (RAG) systems. It addresses the high computational costs and scalability issues associated with naive RAG implementations (...)

[...]</code></pre><pre><code>Query 2 (hybrid) top 20 for query: solutions to RAG scaling challenges

[1] score=0.5000 doc=docs_ingestor/docs/arxiv/2507.18910.pdf chunk=S22::C05::251104142800
  text: 7 Challenges of RAG 7.2.1 Scalability and Infrastructure Deploying RAG at scale requires substantial engineering to maintain large knowledge corpora and efficient retrieval indices. Systems must handle millions or billions of documents, demanding significant computational resources, efficient indexing, distributed computing infrastructure, and cost management strategies [21]...
[2] score=0.5000 doc=docs_ingestor/docs/arxiv/2508.05100.pdf chunk=S3::C06::251104155301
  text: Introduction Empirical analyses across multiple real-world benchmarks reveal that BEE-RAG fundamentally alters the entropy scaling laws governing conventional RAG systems, which provides a robust and scalable solution for RAG systems dealing with long-context scenarios....
[...]</code></pre><p>We can also test the system with specific keywords like names and IDs to make sure it chooses BM25 rather than semantic search.</p><pre><code>Original query:
any papers from Chenxin Diao?

Generated queries:
- BM25: Chenxin Diao</code></pre><p>This will pull up results where <em>Chenxin Diao</em> is clearly mentioned.</p><p>If you want to do this better, you can build a retrieval system that generates a few example queries based on the input, so when the original query comes in, you fetch examples to help guide the optimizer.</p><p>This helps because smaller models aren&#8217;t great at transforming messy human queries into ones with more precise academic phrasing.</p><p>To give you an example, when a user is asking why the LLM is lying, the optimizer may transform the query to something like &#8220;causes of inaccuracies in large language models&#8221; rather than directly look for &#8220;hallicunations.&#8221;</p><p>After we fetch results in parallel, we fuse them. The result will look something like this:</p><pre><code>RRF Fusion top 38 for query: why is everyone saying RAG doesn&#8217;t scale? how are people fixing that?

[1] score=0.0328 doc=docs_ingestor/docs/arxiv/2507.18910.pdf chunk=S22::C05::251104142800
  text: 7 Challenges of RAG 7.2.1 Scalability and Infrastructure Deploying RAG at scale requires substantial engineering to maintain large knowledge corpora and efficient retrieval indices. Systems must handle millions or billions of documents, demanding significant computational resources, efficient indexing, distributed computing infrastructure, and cost management strategies [21]....
[2] score=0.0313 doc=docs_ingestor/docs/arxiv/2507.18910.pdf chunk=S22::C42::251104142800
  text: 7 Challenges of RAG 7.5.5 Scalability Scalability challenges arise as knowledge corpora expand. Advanced indexing, distributed retrieval, and approximate nearest neighbor techniques facilitate efficient handling of large-scale knowledge bases [57]. Selective indexing and corpus curation...
[3] score=0.0161 doc=docs_ingestor/docs/arxiv/2507.07695.pdf chunk=SDOC::SUM::251104135247
  text: This paper proposes the KeyKnowledgeRAG (K2RAG) framework to enhance the efficiency and accuracy of Retrieval-Augment-Generate (RAG) systems. It addresses the high computational costs and scalability issues associated with naive RAG implementations by...
[4] score=0.0161 doc=docs_ingestor/docs/arxiv/2508.05100.pdf chunk=S3::C06::251104155301
  text: Introduction Empirical analyses across multiple real-world benchmarks reveal that BEE-RAG fundamentally alters the entropy scaling laws governing conventional RAG systems, which provides a robust and scalable solution for RAG systems dealing with long-context scenarios...
[...]</code></pre><p>We see that there are some good matches, but also a few irrelevant ones that we&#8217;ll need to filter out further.</p><p>As a note before we move on, this is probably the step you&#8217;ll cut or optimize once you&#8217;re trying to reduce latency.</p><p>I find LLMs aren&#8217;t great at creating key queries that actually pull up useful information all that well, so if it&#8217;s not done right, it just adds more noise. So, don&#8217;t over do it with too many similar queries.</p><h3>Adding a re-ranker</h3><p>We do get results back from the retrieval system, and some of these are good while others are irrelevant, so most retrieval systems will use a re-ranker of some sort.</p><p>A re-ranker takes in several chunks and gives each one a relevancy score based on the original user query. You have several choices here, including using something smaller.</p><p>I&#8217;ll use Cohere&#8217;s re-ranker for this part though.</p><p>We can test this re-ranker on the first question we used in the previous section: <em>&#8220;Why is everyone saying RAG doesn&#8217;t scale? How are people fixing that?&#8221;</em></p><p>At this point, we&#8217;ve already transformed the user query, done semantic or hybrid search, and fused the results before passing the chunks to the re-ranker.</p><p>The re-ranker returns these results:</p><pre><code>[... optimizer... retrieval... fuse...]

Rerank summary:
- strategy=cohere
- model=rerank-english-v3.0
- candidates=32
- eligible_above_threshold=4
- kept=4 (reranker_threshold=0.35)

Reranked Relevant (4/32 kept &#8805; 0.35) top 4 for query: why is everyone saying RAG doesn&#8217;t scale? how are people fixing that?

[1] score=0.7920 doc=docs_ingestor/docs/arxiv/2507.07695.pdf chunk=S4::C08::251104135247
  text: 1 Introduction Scalability: Naive implementations of Retrieval-Augmented Generation (RAG) often rely on 16-bit floating-point large language models (LLMs) for the generation component....
[2] score=0.4749 doc=docs_ingestor/docs/arxiv/2507.18910.pdf chunk=S22::C42::251104142800
  text: 7 Challenges of RAG 7.5.5 Scalability Scalability challenges arise as knowledge corpora expand. Advanced indexing, distributed retrieval, and approximate nearest neighbor techniques facilitate efficient handling of large-scale knowledge bases [57]...
[3] score=0.4304 doc=docs_ingestor/docs/arxiv/2507.18910.pdf chunk=S22::C05::251104142800
  text: 7 Challenges of RAG 7.2.1 Scalability and Infrastructure Deploying RAG at scale requires substantial engineering to maintain large knowledge corpora and efficient retrieval indices...
[4] score=0.3556 doc=docs_ingestor/docs/arxiv/2509.13772.pdf chunk=S11::C02::251104182521
  text: 7. Discussion and Limitations Scalability of RAGOrigin: We extend our evaluation by scaling the NQ dataset&#8217;s knowledge database to 16.7 million texts, combining entries from the knowledge database of NQ, HotpotQA, and MS-MARCO. Using the same user questions from NQ, we assess RAGOrigin&#8217;s performance under larger data volumes...</code></pre><p>We can clearly see that it&#8217;s able to identify a few relevant chunks that we can use as seeds. <em>Remember it only has 150 docs to go on in the first place.</em></p><p>You can also see that it returns multiple chunks from the same document. We&#8217;ll set this up later in the context construction, but if you want unique documents fetched, you can add some custom logic here to set the limit for unique docs rather than chunks.</p><p>We can try the re-ranker with another question: <em>&#8220;hallucinations in RAG vs normal LLMs and how to reduce them&#8221;</em></p><pre><code>[... optimizer... retrieval... fuse...]

Rerank summary:
- strategy=cohere
- model=rerank-english-v3.0
- candidates=35
- eligible_above_threshold=12
- kept=5 (threshold=0.2)

Reranked Relevant (5/35 kept &#8805; 0.2) top 5 for query: hallucinations in rag vs normal llms and how to reduce them

[1] score=0.9965 doc=docs_ingestor/docs/arxiv/2508.19614.pdf chunk=S7::C03::251104164901
  text: 5 Related Work Hallucinations in LLMs Hallucinations in LLMs refer to instances where the model generates false or unsupported information not grounded in its reference data [42]. Existing mitigation strategies include multi-agent debating, where multiple LLM instances collaborate to detect inconsistencies through iterative debates [8, 14]...
[2] score=0.9342 doc=docs_ingestor/docs/arxiv/2508.05509.pdf chunk=S3::C01::251104160034
  text: ...Despite the success, these models are often criticized for their tendency to produce hallucinations, generating incorrect statements on tasks beyond their knowledge and perception (...). Recently, retrieval-augmented generation (RAG) (...) has emerged as a promising solution to alleviate such hallucinations. By dynamically leveraging external knowledge from textual corpora, RAG enables LLMs to generate more accurate and reliable responses without costly retraining (Lewis et al. 2020; Figure 1: Comparison of three paradigms. LAG exhibits greater lightweight properties compared to GraphRAG while...
[3] score=0.9030 doc=docs_ingestor/docs/arxiv/2509.13702.pdf chunk=S3::C01::251104182000
  text: ABSTRACT Hallucination remains a critical barrier to the reliable deployment of Large Language Models (LLMs) in high-stakes applications. Existing mitigation strategies, such as Retrieval-Augmented Generation (RAG) and post-hoc verification, are often reactive, inefficient, or fail to address the root cause within the generative process...
[4] score=0.9007 doc=docs_ingestor/docs/arxiv/2509.09360.pdf chunk=S2::C05::251104174859
  text: 1 Introduction Figure 1. Standard Retrieval-Augmented Generation (RAG)...aims to mitigate hallucinations by grounding model outputs in retrieved, up-to-date documents, as illustrated in Figure 1. By injecting retrieved text from re- a...
[5] score=0.8986 doc=docs_ingestor/docs/arxiv/2508.04057.pdf chunk=S20::C02::251104155008
  text: Parametric knowledge can generate accurate answers. Effects of LLM hallucinations. To assess the impact of hallucinations when large language models (LLMs) generate answers without retrieval, we conduct a controlled experiment based on a simple heuristic: if a generated answer contains numeric values, it is more likely to be affected by hallucination....</code></pre><p>This query also performs well enough (if you look at the full chunks returned).</p><p>We can also test messier user queries, like: <em>&#8220;why is the llm lying and rag help with this?&#8221;</em></p><pre><code>[... optimizer...]

Original query:
why is the llm lying and rag help with this?

Generated queries:
- semantic: explore reasons for LLM inaccuracies
- hybrid: RAG techniques for LLM truthfulness

[...retrieval... fuse...]

Rerank summary:
- strategy=cohere
- model=rerank-english-v3.0
- candidates=39
- eligible_above_threshold=39
- kept=6 (threshold=0)

Reranked Relevant (6/39 kept &#8805; 0) top 6 for query: why is the llm lying and rag help with this?

[1] score=0.0293 doc=docs_ingestor/docs/arxiv/2507.05714.pdf chunk=S3::C01::251104134926
  text: 1 Introduction Retrieval Augmentation Generation (hereafter referred to as RAG) helps large language models (LLMs) (OpenAI et al., 2024) reduce hallucinations (Zhang et al., 2023) and access real-time data 1 *Equal contribution....
[2] score=0.0284 doc=docs_ingestor/docs/arxiv/2508.15437.pdf chunk=S3::C01::251104164223
  text: 1 Introduction Large language models (LLMs) augmented with retrieval have become a dominant paradigm for knowledge-intensive NLP tasks. In a typical retrieval-augmented generation (RAG) setup, an LLM retrieves documents from an external corpus and conditions generation on the retrieved evidence (Lewis et al., 2020b; Izacard and Grave, 2021). This setup mitigates a key weakness of LLMs-hallucination-by grounding generation in externally sourced knowledge...
[3] score=0.0277 doc=docs_ingestor/docs/arxiv/2509.09651.pdf chunk=S3::C01::251104180034
  text: ...despite their versatility, LLMs are prone to generating false or misleading content, a phenomenon commonly referred to as hallucination...
[4] score=0.0087 doc=docs_ingestor/docs/arxiv/2507.07695.pdf chunk=S4::C08::251104135247
  text: 1 Introduction Scalability: Naive implementations of Retrieval-Augmented Generation (RAG) often rely on 16-bit floating-point large language models (LLMs) for the generation component. However, this approach introduces significant scalability challenges due to the increased memory demands required to host the LLM as well as longer inference times due to using a higher precision number type. To enable more efficient scaling, it is crucial to integrate methods or techniques that reduce the memory footprint and inference times of generator models...</code></pre><p>Before we move on, I need to note that there are moments where this re-ranker doesn&#8217;t do that well, as you&#8217;ll see above from the scores.</p><p>At times it estimates that the chunks doesn&#8217;t answer the user&#8217;s question but it actually does, at least when we look at these chunks as seeds.</p><p>Usually for a re-ranker, the chunks should hint at the entire content, but we&#8217;re using these chunks as seeds, so in some cases it will rate results very low, but it&#8217;s enough for us to go on.</p><p>This is why I&#8217;ve kept the score threshold very low.</p><p>There may be better options here that you might want to explore, maybe building a custom re-ranker that understands what you&#8217;re looking for.</p><p>Nevertheless, now that we have a few relevant documents, we&#8217;ll use its metadata that we set before on ingestion to expand and fan out the chunks so the LLM will get enough context to understand how to answer the question.</p><h3>Build the context</h3><p>Now that we have a few chunks as seeds, we&#8217;ll pull up more information from Redis, expand, and build the context.</p><p>This step is obviously a lot more complicated, as you need to build logic for which chunks to fetch and how (keys if they exist, or neighbors if there are any), fetch information in parallel, and then clean out the chunks further.</p><p>Once you have all the chunks (plus information on the documents themselves), you need to put them together, i.e. de-duping chunks, perhaps setting a limit on how far the system can expand, and highlighting which chunks were fetched and which were expanded.</p><p>The end result will look like something below:</p><pre><code>Expanded context windows (Markdown ready):

## Document #1 &#8212; Fusing Knowledge and Language: A Comparative Study of Knowledge Graph-Based Question Answering with LLMs
- `doc_id`: `doc::6371023da29b4bbe8242ffc5caf4a8cd`
- **Last Updated:** 2025-11-04T17:44:07.300967+00:00
- **Context:** Comparative study on methodologies for integrating knowledge graphs in QA systems using LLMs.
- **Content fetched inside document:**
```text
[start on page 4]
    LLMs in QA
    The advent of LLMs has steered in a transformative era in NLP, particularly within the domain of QA. These models, pre-trained on massive corpora of diverse text, exhibit sophisticated capabilities in both natural language understanding and generation. Their proficiency in producing coherent, contextually relevant, and human-like responses to a broad spectrum of prompts makes them exceptionally well-suited for QA tasks, where delivering precise and informative answers is paramount. Recent advancements by models such as BERT [57] and ChatGPT [58], have significantly propelled the field forward. LLMs have demonstrated strong performance in open-domain QA scenarios-such as commonsense reasoning[20]-owing to their extensive embedded knowledge of the world. Moreover, their ability to comprehend and articulate responses to abstract or contextually nuanced queries and reasoning tasks [22] underscores their utility in addressing complex QA challenges that require deep semantic understanding. Despite their strengths, LLMs also pose challenges: they can exhibit contextual ambiguity or overconfidence in their outputs (&#8217;hallucinations&#8217;)[21], and their substantial computational and memory requirements complicate deployment in resource-constrained environments.
    RAG, fine tuning in QA
    ---------------------- this was the passage that we matched to the query -------------
    LLMs also face problems when it comes to domain specific QA or tasks where they are needed to recall factual information accurately instead of just probabilistically generating whatever comes next. Research has also explored different prompting techniques, like chain-of-thought prompting[24], and sampling based methods[23] to reduce hallucinations. Contemporary research increasingly explores strategies such as fine-tuning and retrieval augmentation to enhance LLM-based QA systems. Fine-tuning on domain-specific corpora (e.g., BioBERT for biomedical text [17], SciBERT for scientific text [18]) has been shown to sharpen model focus, reducing irrelevant or generic responses in specialized settings such as medical or legal QA. Retrieval-augmented architectures such as RAG [19] combine LLMs with external knowledge bases, to try to further mitigate issues of factual inaccuracy and enable real-time incorporation of new information. Building on RAG&#8217;s ability to bridge parametric and non-parametric knowledge, many modern QA pipelines introduce a lightweight re-ranking step [25] to sift through the retrieved contexts and promote passages that are most relevant to the query. However, RAG still faces several challenges. One key issue lies in the retrieval step itself-if the retriever fails to fetch relevant documents, the generator is left to hallucinate or provide incomplete answers. Moreover, integrating noisy or loosely relevant contexts can degrade response quality rather than enhance it, especially in high-stakes domains where precision is critical. RAG pipelines are also sensitive to the quality and domain alignment of the underlying knowledge base, and they often require extensive tuning to balance recall and precision effectively.
    --------------------------------------------------------------------------------------
[end on page 5]
```

## Document #2 &#8212; Each to Their Own: Exploring the Optimal Embedding in RAG
- `doc_id`: `doc::3b9c43d010984d4cb11233b5de905555`
- **Last Updated:** 2025-11-04T14:00:38.215399+00:00
- **Context:** Enhancing Large Language Models using Retrieval-Augmented Generation techniques.
- **Content fetched inside document:**
```text
[start on page 1]
    1 Introduction
    Large language models (LLMs) have recently accelerated the pace of transformation across multiple fields, including transportation (Lyu et al., 2025), arts (Zhao et al., 2025), and education (Gao et al., 2024), through various paradigms such as direct answer generation, training from scratch on different types of data, and fine-tuning on target domains. However, the hallucination problem (Henkel et al., 2024) associated with LLMs has confused people for a long time, stemming from multiple factors such as a lack of knowledge on the given prompt (Huang et al., 2025b) and a biased training process (Zhao, 2025).
    Serving as a highly efficient solution, RetrievalAugmented Generation (RAG) has been widely employed in constructing foundation models (Chen et al., 2024) and practical agents (Arslan et al., 2024). Compared to training methods like fine-tuning and prompt-tuning, its plug-and-play feature makes RAG an efficient, simple, and costeffective approach. The main paradigm of RAG involves first calculating the similarities between a question and chunks in an external knowledge corpus, followed by incorporating the top K relevant chunks into the prompt to guide the LLMs (Lewis et al., 2020).
    Despite the advantages of RAG, selecting the appropriate embedding models remains a crucial concern, as the quality of retrieved references directly influences the generation results of the LLM (Tu et al., 2025). Variations in training data and model architecture lead to different embedding models providing benefits across various domains. The differing similarity calculations across embedding models often leave researchers uncertain about how to choose the optimal one. Consequently, improving the accuracy of RAG from the perspective of embedding models continues to be an ongoing area of research.
    ---------------------- this was the passage that we matched to the query -------------
    To address this research gap, we propose two methods for improving RAG by combining the benefits of multiple embedding models. The first method is named Mixture-Embedding RAG, which sorts the retrieved materials from multiple embedding models based on normalized similarity and selects the top K materials as final references. The second method is named Confident RAG, where we first utilize vanilla RAG to generate answers multiple times, each time employing a different embedding model and recording the associated confidence metrics, and then select the answer with the highest confidence level as the final response. By validating our approach using multiple LLMs and embedding models, we illustrate the superior performance and generalization of Confident RAG, even though MixtureEmbedding RAG may lose to vanilla RAG. The main contributions of this paper can be summarized as follows:
    We first point out that in RAG, different embedding models operate within their own prior domains. To leverage the strengths of various embedding models, we propose and test two novel RAG methods: MixtureEmbedding RAG and Confident RAG. These methods effectively utilize the retrieved results from different embedding models to their fullest extent.
    --------------------------------------------------------------------------------------
    While Mixture-Embedding RAG performs similarly to vanilla RAG, the Confident RAG method exhibits superior performance compared to both the vanilla LLM and vanilla RAG, with average improvements of 9.9% and 4.9%, respectively, when using the best confidence metric. Additionally, we discuss the optimal number of embedding models for the Confident RAG method based on the results.
[...]</code></pre><p>The total context will contain a few documents and lands around 2&#8211;3k tokens. There is some waste here, but instead of deciding for the LLM, we send in more information so it can scan entire documents rather than isolated chunks.</p><p><em>Remember you can take a look at the pipeline for five different queries <a href="https://github.com/ilsilfverskiold/Awesome-LLM-Resources-List/tree/main/guides/RAG/Custom">here</a> to see the results.</em></p><p>For the system you build, you can use this context as state so the LLM can answer follow-up questions from the same information.</p><h3><strong>Add in the LLM to answer (the G in RAG)</strong></h3><p>Let&#8217;s finish up with the last step, just to see how the LLM answers with the context it has been handed.</p><p>I did not plan on adding this part, as this article is primarily about the retrieval pipeline, but it&#8217;s good to test with an LLM to see if it can interpret the information into something coherent.</p><p>If you use a larger LLM here, you can expect the latency to go up quite a bit.</p><p>I decided to test with GPT-5 (low reasoning), as it&#8217;s very cheap to use but also quite slow. You&#8217;ll see the result below with the messy user query: <em>&#8220;why is the llm lying and can rag help with this?&#8221;</em></p><pre><code>[optimize query... retrieve... fuse... re-rank... expand... build context...]

LLM Answers:

LLMs sometimes &#8220;lie&#8221; because they generate the next likely words based on patterns, not on verified facts. Without grounding in sources, they can produce confident but false statements&#8212;known as hallucinations&#8212;and their built&#8209;in knowledge can be outdated or incomplete [6][3]. This is risky in domains that demand strict accuracy (e.g., law or telecom regulations) where small errors have big consequences [3].
RAG helps by having the model first retrieve relevant documents from an external corpus and then generate answers grounded in that evidence. This reduces hallucinations and brings in up&#8209;to&#8209;date information [2][6][1]. In practice, domain&#8209;tailored RAG has raised accuracy substantially; for example, a radio&#8209;regulations RAG pipeline boosted GPT&#8209;4o accuracy by +11.9% over vanilla prompting and achieved up to 97% retrieval accuracy, supporting more reliable answers [3].
However, RAG is only as good as its retrieval and how well the model uses the retrieved text. Static, off&#8209;the&#8209;shelf retrievers can be brittle for complex tasks (e.g., multihop questions or procedural queries) that need iterative lookups or query reformulation [2]. Naive RAG can also drown the model in long, noisy contexts or pull semantically similar but irrelevant chunks&#8212;the &#8220;needle&#8209;in&#8209;a&#8209;haystack&#8221; problem&#8212;leading to wrong answers [4].
Better RAG practices address this: instruction&#8209;tuning for RAG that teaches filtering, combining multiple sources, and RAG&#8209;specific reasoning with a &#8220;think before answering&#8221; approach (HIRAG) improves use of evidence [1]; adaptive, feedback&#8209;driven retrieval decides when and how to retrieve and re&#8209;rank evidence [2]; and pipeline designs that optimize chunking and retrieval raise answer accuracy [4].
If hallucination still persists, techniques that steer decoding directly (beyond RAG) can further suppress it [5].

cited documents:
  [1] doc::b0610cc6134b401db0ea68a77096e883 &#8212; HIRAG: Hierarchical-Thought Instruction-Tuning Retrieval-Augmented Generation
  [2] doc::53b521e646b84289b46e648c66dde56a &#8212; Test-time Corpus Feedback: From Retrieval to RAG
  [3] doc::9694bd0124d0453c81ecb32dd75ab489 &#8212; Retrieval-Augmented Generation for Reliable Interpretation of Radio Regulations
  [4] doc::6d7a7d88cfc04636b20931fdf22f1e61 &#8212; KeyKnowledgeRAG (K^2RAG): An Enhanced RAG method for improved LLM question-answering capabilities
  [5] doc::3c9a1937ecbc454b8faff4f66bdf427f &#8212; DSCC-HS: A Dynamic Self-Reinforcing Framework for Hallucination Suppression in Large Language Models
  [6] doc::688cfbc0abdc4520a73e219ac26aff41 &#8212; A Systematic Review of Key Retrieval-Augmented Generation (RAG) Systems: Progress, Gaps, and Future Directions</code></pre><p>You&#8217;ll see that it cites sources correctly and uses the information it has been handed, but as we&#8217;re using GPT-5, the latency is quite high with this large context.</p><p>It takes about 9 seconds to first token with GPT-5 (but it will depend on your environment) if you use GPT-5-mini this is reduced to about 4&#8211;5 seconds.</p><p>If the entire retrieval pipeline takes about 3&#8211;5 seconds, this means the last part will take about 2&#8211;3 times longer.</p><p>Some people will argue that you need to send in less information in the context window to decrease latency for this part but that also defeats the purpose of what we&#8217;re trying to do.</p><p>Others will argue for using chain prompting, having one smaller LLM extract useful information and then letting another bigger LLM answer with an optimized context window but I&#8217;m not sure how much you save in terms of time.</p><p>Others will go as small as possible, sacrificing &#8220;intelligence&#8221; for speed and cost. In cases where we serve the LLM the information it needs up front, like we do here, it may do well to use smaller.</p><p>Nevertheless, it&#8217;s up to you how you optimize the system. That is the hard part.</p><p>If you want to examine the entire pipeline for a few queries remember to see <a href="https://github.com/ilsilfverskiold/Awesome-LLM-Resources-List/tree/main/guides/RAG/Custom">this</a> folder.</p><h3>Let&#8217;s talk latency &amp; cost</h3><p>People talking about sending in entire docs into an LLM are probably not ruthlessly optimizing for latency in their systems. This is the part you&#8217;ll spend the most time with, users don&#8217;t want to wait.</p><p>Yes you can apply some UX tricks while the user is waiting, such as &#8220;searching documents&#8221;, &#8220;finding a few relevant passages&#8221; but you still need to work on speed.</p><p>This is also why it&#8217;s interesting that we see this shift into agentic search in the wild, it&#8217;s so much slower to add large context windows, LLM-based query transforms, auto &#8220;router&#8221; chains, sub-question decomposition and multi-step &#8220;agentic&#8221; query engines.</p><p>For this system here (mostly built with Codex and my instructions) we land at around 4 seconds for retrieval in a Serverless environment.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!THqP!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71d8ba0b-02dc-4f94-99d7-f5e2deb1a018_1400x394.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!THqP!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71d8ba0b-02dc-4f94-99d7-f5e2deb1a018_1400x394.png 424w, /__u/substackcdn.com/image/fetch/$s_!THqP!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71d8ba0b-02dc-4f94-99d7-f5e2deb1a018_1400x394.png 848w, /__u/substackcdn.com/image/fetch/$s_!THqP!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71d8ba0b-02dc-4f94-99d7-f5e2deb1a018_1400x394.png 1272w, /__u/substackcdn.com/image/fetch/$s_!THqP!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71d8ba0b-02dc-4f94-99d7-f5e2deb1a018_1400x394.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!THqP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71d8ba0b-02dc-4f94-99d7-f5e2deb1a018_1400x394.png" width="1400" height="394" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/71d8ba0b-02dc-4f94-99d7-f5e2deb1a018_1400x394.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:394,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!THqP!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71d8ba0b-02dc-4f94-99d7-f5e2deb1a018_1400x394.png 424w, /__u/substackcdn.com/image/fetch/$s_!THqP!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71d8ba0b-02dc-4f94-99d7-f5e2deb1a018_1400x394.png 848w, /__u/substackcdn.com/image/fetch/$s_!THqP!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71d8ba0b-02dc-4f94-99d7-f5e2deb1a018_1400x394.png 1272w, /__u/substackcdn.com/image/fetch/$s_!THqP!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F71d8ba0b-02dc-4f94-99d7-f5e2deb1a018_1400x394.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This is kind of slow (but pretty cheap).</p><p>You can optimize each step here to bring that number down, keeping most things warm. However, using the APIs you can&#8217;t always control how fast they return a response.</p><p>Some people will argue to host your own smaller models for the optimizer and routers, but then you need to add in costs to host which can easily add a few hundred dollars per month.</p><p>Adding the final LLM at the end will be the worst culprit (as you saw in the previous section).</p><p>But with this pipeline here, each run (without caching) costs us 1.2 cents ($0.0121) so if you had your org ask 200 questions every day you would pay around $2.42 with GPT-5.</p><p>If you use GPT-5-mini for the main LLM, one pipeline run would drop to 0.0041 cents, and amount to about $0.82 per day for 200 runs.</p><p>You can cache repeat questions to lower this too.</p><p>As for embedding the documents, I paid around $0.5 for 200 PDF files using OpenAI&#8217;s large model. This will increase as you scale which is something to consider, then it can make sense with small or specialized model (or setting up a two-part system to use keyword search first to narrow candidates).</p><h2>Notes on this system</h2><p>As we&#8217;re only working with recent RAG papers, once you scale it, you can add some stuff to make it more robust.</p><p>I should first note though that you may not see most of the real issues until your docs start growing. Whatever feels solid with a few hundred docs will start to feel messy once you ingest tens of thousands.</p><p>You can have the optimizer set filters, perhaps using semantic matching for topics. You can also have it set the dates to keep the information fresh while introducing an authority signal in re-ranking that boosts certain sources.</p><p>Some teams take this a bit further and design their own scoring functions to decide what should surface and how to prioritize documents, but this depends entirely on what your corpus looks like.</p><p>If you need to ingest several thousand docs, it might make sense to skip the LLM during ingestion and instead use it in the retrieval pipeline, where it analyzes documents only when a query asks for it. You can then cache that result for next time.</p><p>If you&#8217;re still with me this far, a question you can ask yourself is whether it&#8217;s worth it to build a system like this and how much work it takes to do it well?</p><p>&#10084;</p>]]></content:encoded></item><item><title><![CDATA[Deep Dive: How to Save on Tokens]]></title><description><![CDATA[Caching, lazy-loading, routing, compaction, and so on]]></description><link>https://howtouseai.substack.com/p/deep-dive-how-to-save-on-tokens</link><guid isPermaLink="false">https://howtouseai.substack.com/p/deep-dive-how-to-save-on-tokens</guid><dc:creator><![CDATA[Ida Silfverskiold]]></dc:creator><pubDate>Tue, 18 Aug 2026 11:27:46 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!Dbve!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9154bdc-a1c0-4636-81ea-03a122b2fc2f_1400x865.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!Dbve!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9154bdc-a1c0-4636-81ea-03a122b2fc2f_1400x865.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!Dbve!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9154bdc-a1c0-4636-81ea-03a122b2fc2f_1400x865.webp 424w, /__u/substackcdn.com/image/fetch/$s_!Dbve!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9154bdc-a1c0-4636-81ea-03a122b2fc2f_1400x865.webp 848w, /__u/substackcdn.com/image/fetch/$s_!Dbve!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9154bdc-a1c0-4636-81ea-03a122b2fc2f_1400x865.webp 1272w, /__u/substackcdn.com/image/fetch/$s_!Dbve!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9154bdc-a1c0-4636-81ea-03a122b2fc2f_1400x865.webp 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!Dbve!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9154bdc-a1c0-4636-81ea-03a122b2fc2f_1400x865.webp" width="1400" height="865" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c9154bdc-a1c0-4636-81ea-03a122b2fc2f_1400x865.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:865,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:32864,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/webp&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://howtouseai.substack.com/i/211694927?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9154bdc-a1c0-4636-81ea-03a122b2fc2f_1400x865.webp&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!Dbve!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9154bdc-a1c0-4636-81ea-03a122b2fc2f_1400x865.webp 424w, /__u/substackcdn.com/image/fetch/$s_!Dbve!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9154bdc-a1c0-4636-81ea-03a122b2fc2f_1400x865.webp 848w, /__u/substackcdn.com/image/fetch/$s_!Dbve!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9154bdc-a1c0-4636-81ea-03a122b2fc2f_1400x865.webp 1272w, /__u/substackcdn.com/image/fetch/$s_!Dbve!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9154bdc-a1c0-4636-81ea-03a122b2fc2f_1400x865.webp 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Your first agent might ship with a 500-token system prompt and two tools, but those numbers usually balloon fast.</p><p>Just to illustrate, the leaked Claude system prompt ran around 24,000 tokens, and OpenClaw users have <a href="https://github.com/openclaw/openclaw/issues/21999">reported</a> more than 150,000 input tokens sent to Gemini 3.1 Pro for 29 tokens of output on the first turn.</p><p>An unoptimized agent that runs at 100 messages a day at 166K input tokens can cost around $996 a month on Gemini 3.1 Pro and roughly $2,490 on Claude Opus 4.6.</p><p>There are tricks to keep these costs down, closer to $50 and $100 a month.</p><p>So I wanted to go through a few design principles people usually consider when building.</p><p>We&#8217;ll go through how prompt caching works and why it&#8217;s a quick win, semantic caching, lazy-loading tools and MCPs, routing and cascading, delegating to subagents, and a bit on what it saves to keep the context clean.</p><p>I am including interactive graphs throughout this article that help you visualize the cost savings each principle can get you based on the amount of tokens you are using.</p><p><em>Yes, I am obviously staying real throughout, every saving comes with trade-offs.</em></p><h3>Four design principles to keep in mind</h3><p>In this article we&#8217;ll go through four different parts with four different interactive calculators.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!IHEw!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c48a0eb-165d-432d-af29-0bc0fdf1193d_1400x629.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!IHEw!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c48a0eb-165d-432d-af29-0bc0fdf1193d_1400x629.png 424w, /__u/substackcdn.com/image/fetch/$s_!IHEw!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c48a0eb-165d-432d-af29-0bc0fdf1193d_1400x629.png 848w, /__u/substackcdn.com/image/fetch/$s_!IHEw!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c48a0eb-165d-432d-af29-0bc0fdf1193d_1400x629.png 1272w, /__u/substackcdn.com/image/fetch/$s_!IHEw!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c48a0eb-165d-432d-af29-0bc0fdf1193d_1400x629.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!IHEw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c48a0eb-165d-432d-af29-0bc0fdf1193d_1400x629.png" width="1400" height="629" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8c48a0eb-165d-432d-af29-0bc0fdf1193d_1400x629.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:629,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!IHEw!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c48a0eb-165d-432d-af29-0bc0fdf1193d_1400x629.png 424w, /__u/substackcdn.com/image/fetch/$s_!IHEw!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c48a0eb-165d-432d-af29-0bc0fdf1193d_1400x629.png 848w, /__u/substackcdn.com/image/fetch/$s_!IHEw!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c48a0eb-165d-432d-af29-0bc0fdf1193d_1400x629.png 1272w, /__u/substackcdn.com/image/fetch/$s_!IHEw!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8c48a0eb-165d-432d-af29-0bc0fdf1193d_1400x629.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>First, we&#8217;ll be looking at how to reuse tokens when possible, looking at <a href="https://claude.ai/public/artifacts/af356b1d-175b-467a-9c09-2568b7f0bcd0">prompt caching</a> and <a href="https://claude.ai/public/artifacts/9231bfa1-6291-4c05-8082-10f1b6f282b8">semantic caching</a>. Then we&#8217;ll look at how to minimize the stable and always added tokens like memory and <a href="https://claude.ai/public/artifacts/bedb2567-6e8f-4d71-bcc6-4c9c18cde2cb">tool definitions</a>.</p><p>It will also go through how to <a href="https://claude.ai/public/artifacts/f231d43b-0063-40d4-9907-36fe18cdafce">route to smaller models</a>, or escalate to a larger model, looking at the quality risks and the savings thereof.</p><p>The last section will talk about <a href="https://claude.ai/public/artifacts/5b50845f-6fc9-4912-a577-100183ef253a">keeping the context clean</a> for performance and economic reasons, while briefly mentioning compaction.</p><h2>Reuse tokens when possible</h2><p>LLM cost doesn&#8217;t just come from calling the model too often. It also comes from repeatedly paying to process the same tokens again and again.</p><p>So for this section we&#8217;ll cover K/V caching, the under-the-hood mechanism behind prompt caching, and semantic caching, which are two very different things. We&#8217;ll go through what they are, what they do, what you can save.</p><p>Prompt caching is a quick win for long system prompts, while semantic caching is a bit more work and comes with a bit more risk.</p><h3>K/V caching &amp; prefix caching</h3><p>Before a model can generate anything, it first has to process the prompt. This part costs compute, which means latency and money. So, to be efficient, we shouldn&#8217;t keep re-processing the same content.</p><p>When you use a large language model, the prompt first gets tokenized, then those tokens turn into vectors, and then inside each attention layer those vectors get projected into K/V tensors.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!kIkK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9d20fb1-f2aa-4ccf-bb1a-d67e33d2320d_1400x549.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!kIkK!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9d20fb1-f2aa-4ccf-bb1a-d67e33d2320d_1400x549.png 424w, /__u/substackcdn.com/image/fetch/$s_!kIkK!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9d20fb1-f2aa-4ccf-bb1a-d67e33d2320d_1400x549.png 848w, /__u/substackcdn.com/image/fetch/$s_!kIkK!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9d20fb1-f2aa-4ccf-bb1a-d67e33d2320d_1400x549.png 1272w, /__u/substackcdn.com/image/fetch/$s_!kIkK!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9d20fb1-f2aa-4ccf-bb1a-d67e33d2320d_1400x549.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!kIkK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9d20fb1-f2aa-4ccf-bb1a-d67e33d2320d_1400x549.png" width="1400" height="549" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c9d20fb1-f2aa-4ccf-bb1a-d67e33d2320d_1400x549.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:549,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!kIkK!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9d20fb1-f2aa-4ccf-bb1a-d67e33d2320d_1400x549.png 424w, /__u/substackcdn.com/image/fetch/$s_!kIkK!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9d20fb1-f2aa-4ccf-bb1a-d67e33d2320d_1400x549.png 848w, /__u/substackcdn.com/image/fetch/$s_!kIkK!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9d20fb1-f2aa-4ccf-bb1a-d67e33d2320d_1400x549.png 1272w, /__u/substackcdn.com/image/fetch/$s_!kIkK!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9d20fb1-f2aa-4ccf-bb1a-d67e33d2320d_1400x549.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Caching means holding on to these do we don&#8217;t recompute them next time | Image by author</p><p>The inference engine caches the K/V tensors during generation, otherwise the math doesn&#8217;t work at any reasonable speed as I understand it.</p><p>But instead of throwing the cache away when the response ends, it&#8217;s possible to store it.</p><p>Next time a request comes in, we&#8217;d check whether that same part of the prompt matches something we already have tensors for. If yes, we load those tensors and skip re-processing it.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!Vvba!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9feecdc1-dd2f-408a-ad77-65cf25003e40_1400x603.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!Vvba!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9feecdc1-dd2f-408a-ad77-65cf25003e40_1400x603.png 424w, /__u/substackcdn.com/image/fetch/$s_!Vvba!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9feecdc1-dd2f-408a-ad77-65cf25003e40_1400x603.png 848w, /__u/substackcdn.com/image/fetch/$s_!Vvba!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9feecdc1-dd2f-408a-ad77-65cf25003e40_1400x603.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Vvba!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9feecdc1-dd2f-408a-ad77-65cf25003e40_1400x603.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!Vvba!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9feecdc1-dd2f-408a-ad77-65cf25003e40_1400x603.png" width="1400" height="603" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9feecdc1-dd2f-408a-ad77-65cf25003e40_1400x603.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:603,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!Vvba!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9feecdc1-dd2f-408a-ad77-65cf25003e40_1400x603.png 424w, /__u/substackcdn.com/image/fetch/$s_!Vvba!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9feecdc1-dd2f-408a-ad77-65cf25003e40_1400x603.png 848w, /__u/substackcdn.com/image/fetch/$s_!Vvba!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9feecdc1-dd2f-408a-ad77-65cf25003e40_1400x603.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Vvba!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9feecdc1-dd2f-408a-ad77-65cf25003e40_1400x603.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>To get a sense of why this matters economically: <strong>let&#8217;s say it takes one second to process 2,000 tokens</strong>, and you have a system prompt of 10,000 tokens.</p><p><strong>That&#8217;s 5 seconds saved on every single LLM call</strong>, just by not recomputing that same start of the prompt through the model over and over again (though prefill throughput varies a lot based on the setup).</p><p>It&#8217;s important to note that we have to match the input exactly to the stored K/V cache.</p><p>If the tokens change, we don&#8217;t have precomputed K/V tensors for that exact part of the prompt anymore, so it has to be processed again. This is where people keep stumbling: a new space added, a reordered tool definition, a timestamp in the wrong place.</p><p>So, storing the cache has real value in terms of speeding up the request, and in turn making the request cheaper.</p><p><em>Note that storing these tensors is not free. Cached K/V takes up memory on the serving side which is why a lot of providers have a TTL window of around 5&#8211;10 minutes.</em></p><p>Now, we don&#8217;t have to build this ourselves, this was just to think through the mechanics. There are frameworks that help with this, and the API providers have their own prompt-caching rules, and we&#8217;ll go through both.</p><h3>Prefix caching for self-hosted inference</h3><p>If you are hosting an open source model, you&#8217;d ideally use an LLM serving framework, like vLLM. Though there are other frameworks that will help with the caching layer, vLLM has an add-on feature we can run through.</p><p>The caching layer in vLLM works by chopping up the prompt into blocks, hashing each block based on its tokens (plus the tokens before it), and storing the K/V tensors against those hashes.</p><p>Like most setups the static part that should be cached should go in the first part of the prompt.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!atRx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32ca6406-4e47-4634-88ee-6f251708b96d_1400x758.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!atRx!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32ca6406-4e47-4634-88ee-6f251708b96d_1400x758.png 424w, /__u/substackcdn.com/image/fetch/$s_!atRx!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32ca6406-4e47-4634-88ee-6f251708b96d_1400x758.png 848w, /__u/substackcdn.com/image/fetch/$s_!atRx!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32ca6406-4e47-4634-88ee-6f251708b96d_1400x758.png 1272w, /__u/substackcdn.com/image/fetch/$s_!atRx!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32ca6406-4e47-4634-88ee-6f251708b96d_1400x758.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!atRx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32ca6406-4e47-4634-88ee-6f251708b96d_1400x758.png" width="1400" height="758" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/32ca6406-4e47-4634-88ee-6f251708b96d_1400x758.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:758,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!atRx!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32ca6406-4e47-4634-88ee-6f251708b96d_1400x758.png 424w, /__u/substackcdn.com/image/fetch/$s_!atRx!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32ca6406-4e47-4634-88ee-6f251708b96d_1400x758.png 848w, /__u/substackcdn.com/image/fetch/$s_!atRx!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32ca6406-4e47-4634-88ee-6f251708b96d_1400x758.png 1272w, /__u/substackcdn.com/image/fetch/$s_!atRx!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F32ca6406-4e47-4634-88ee-6f251708b96d_1400x758.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>To enable caching in vLLM use the flag <code>--enable-prefix-caching</code></p><p>To adjust the block size you can use the flag <code>--block-size</code>. Block sizes means tokens per block. If block size is 16 then you have 16 tokens per block before it cuts off and starts another.</p><p>You can also use the flag <code>--kv-cache-memory-bytes</code> which explicitly sets KV cache size per GPU. The more memory you give it, the longer it can hold on to cached blocks. But if you have lots of different long requests happening at the same time, that memory fills up faster, so old blocks get removed faster.</p><p>There are other solutions out there, but you get the idea. It&#8217;s the same mechanics we spoke about for the previous section.</p><p>You can also check out SGLang and RadixAttention for prefix caching, as well as LMCache that should plug into serving engines.</p><p>Most people though use the API providers and they have their own policies on how to use prompt caching so let&#8217;s walk through those.</p><h3>Prompt caching via API providers</h3><p>Using the API providers you need to make sure to structure your prompts so they hit the cache. There are things you need to follow for this to be done correctly.</p><p>I will use OpenAI first here as an example.</p><p>For OpenAI, they are explicit, to cache part of the prompt they require an exact prefix match. I.e. the same static input at the start of the prompt.</p><p>This means you always put stable instructions, examples, and tools first, and variable content later.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!lgXC!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc799d5b4-746f-4050-ba5d-781458a7d1cf_1400x528.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!lgXC!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc799d5b4-746f-4050-ba5d-781458a7d1cf_1400x528.png 424w, /__u/substackcdn.com/image/fetch/$s_!lgXC!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc799d5b4-746f-4050-ba5d-781458a7d1cf_1400x528.png 848w, /__u/substackcdn.com/image/fetch/$s_!lgXC!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc799d5b4-746f-4050-ba5d-781458a7d1cf_1400x528.png 1272w, /__u/substackcdn.com/image/fetch/$s_!lgXC!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc799d5b4-746f-4050-ba5d-781458a7d1cf_1400x528.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!lgXC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc799d5b4-746f-4050-ba5d-781458a7d1cf_1400x528.png" width="1400" height="528" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c799d5b4-746f-4050-ba5d-781458a7d1cf_1400x528.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:528,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!lgXC!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc799d5b4-746f-4050-ba5d-781458a7d1cf_1400x528.png 424w, /__u/substackcdn.com/image/fetch/$s_!lgXC!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc799d5b4-746f-4050-ba5d-781458a7d1cf_1400x528.png 848w, /__u/substackcdn.com/image/fetch/$s_!lgXC!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc799d5b4-746f-4050-ba5d-781458a7d1cf_1400x528.png 1272w, /__u/substackcdn.com/image/fetch/$s_!lgXC!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc799d5b4-746f-4050-ba5d-781458a7d1cf_1400x528.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>You can also send in <code>prompt-cache-key</code> which can help route similar requests together and improve cache hit rates.</p><p>There are more specifics around this too. Caching is enabled automatically for prompts that are 1,024 tokens or longer, but they use the first 256 tokens to route requests back to the same cache. So, that static part of the prompt needs to be more than 256 tokens.</p><p>For Anthropic you have to enable caching with the <code>cache-control</code> parameter.</p><p>Worth mentioning too that typically the evictions (TTL) occur around 5&#8211;10 minutes of inactivity but can be extended. It&#8217;s the same for Anthropic but you can push it to one hour (but it would cost you more at 2x).</p><p>Earlier I talked about the time you save, and if you&#8217;re self-hosting, this also saves money. With API providers, the savings show up as cheaper cached input tokens.</p><p>With OpenAI cached input is up to 90% off the base input.</p><p>Anthropic gives you the same discount on cached inputs, but you also pay to store that cache. So, if you&#8217;re not using it correctly, Anthropic will be more expensive.</p><p>In general though, if you have 90% of your prompt being static, you can look below at your possible pricing if you use the cache right.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!CQ2J!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a77f769-3a0f-4161-8765-eb33fbdb39d7_1400x909.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!CQ2J!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a77f769-3a0f-4161-8765-eb33fbdb39d7_1400x909.png 424w, /__u/substackcdn.com/image/fetch/$s_!CQ2J!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a77f769-3a0f-4161-8765-eb33fbdb39d7_1400x909.png 848w, /__u/substackcdn.com/image/fetch/$s_!CQ2J!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a77f769-3a0f-4161-8765-eb33fbdb39d7_1400x909.png 1272w, /__u/substackcdn.com/image/fetch/$s_!CQ2J!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a77f769-3a0f-4161-8765-eb33fbdb39d7_1400x909.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!CQ2J!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a77f769-3a0f-4161-8765-eb33fbdb39d7_1400x909.png" width="1400" height="909" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4a77f769-3a0f-4161-8765-eb33fbdb39d7_1400x909.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:909,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!CQ2J!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a77f769-3a0f-4161-8765-eb33fbdb39d7_1400x909.png 424w, /__u/substackcdn.com/image/fetch/$s_!CQ2J!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a77f769-3a0f-4161-8765-eb33fbdb39d7_1400x909.png 848w, /__u/substackcdn.com/image/fetch/$s_!CQ2J!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a77f769-3a0f-4161-8765-eb33fbdb39d7_1400x909.png 1272w, /__u/substackcdn.com/image/fetch/$s_!CQ2J!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4a77f769-3a0f-4161-8765-eb33fbdb39d7_1400x909.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>I decided to create an interactive graph for this with Claude <a href="https://claude.ai/public/artifacts/af356b1d-175b-467a-9c09-2568b7f0bcd0">here</a>, so you can play around with it.</p><p>So, prompt caching is a pretty good win for everyone if you&#8217;re using longer system prompts that stay the same and something to consider to save on tokens.</p><p>Let&#8217;s move onto semantic caching, which is something else entirely.</p><h3>Semantic caching</h3><p>Semantic caching matches on meaning, i.e. if it is a similar enough request, return the cached result. Although it sounds easy enough, there are clear pitfalls to watch out for.</p><p>To semantically match texts, we use embeddings. You can do some research here if the word is new to you. I wrote about it a few years ago.</p><p>In essence, embeddings are vectors that we can compare against each other using cosine similarity. If similarity is high, the meaning should be similar, though it depends on the model.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!mucE!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92984e1b-3353-4b5e-b754-a1b7b9492944_1400x645.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!mucE!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92984e1b-3353-4b5e-b754-a1b7b9492944_1400x645.png 424w, /__u/substackcdn.com/image/fetch/$s_!mucE!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92984e1b-3353-4b5e-b754-a1b7b9492944_1400x645.png 848w, /__u/substackcdn.com/image/fetch/$s_!mucE!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92984e1b-3353-4b5e-b754-a1b7b9492944_1400x645.png 1272w, /__u/substackcdn.com/image/fetch/$s_!mucE!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92984e1b-3353-4b5e-b754-a1b7b9492944_1400x645.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!mucE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92984e1b-3353-4b5e-b754-a1b7b9492944_1400x645.png" width="1400" height="645" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/92984e1b-3353-4b5e-b754-a1b7b9492944_1400x645.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:645,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!mucE!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92984e1b-3353-4b5e-b754-a1b7b9492944_1400x645.png 424w, /__u/substackcdn.com/image/fetch/$s_!mucE!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92984e1b-3353-4b5e-b754-a1b7b9492944_1400x645.png 848w, /__u/substackcdn.com/image/fetch/$s_!mucE!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92984e1b-3353-4b5e-b754-a1b7b9492944_1400x645.png 1272w, /__u/substackcdn.com/image/fetch/$s_!mucE!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F92984e1b-3353-4b5e-b754-a1b7b9492944_1400x645.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>What semantic caching is proposing is then to match similar requests to answers that already exist. Asking for &#8220;What&#8217;s the capital of France?&#8221; and &#8220;Quick, give me the capital of France&#8221; should then route to the same answer.</p><p>No need to use an LLM to answer the same thing over and over again.</p><p>This works fine if many people ask near-identical generic questions and the data isn&#8217;t going stale too fast.</p><p>So why not do it for every case? <strong>There are a ton of pitfalls here.</strong></p><p>Just at the top of my head, you need to consider what threshold to use for similarity, how long the answer should stay valid, and what happens on multi-turn questions.</p><p>Then you need to think about what actually gets stored, whether there should be a router implemented too, how to separate users, and what happens if the wrong answer is cached.</p><p>You also need to consider (Time to Live &#8212; TTL), as in when information turns stale and for which questions.</p><p>So even though the mechanics are quite simple, you still need metadata filters and tags, such as user, workspace, corpus version, persona, session/user scoping, smart TTL, and some rule for &#8220;is the return enough?&#8221;</p><p>This then turns into <strong>a bit of a project</strong>.</p><p>So, if you want to do it, perhaps <strong>use the semantic index to find a previous question</strong>. Different<strong> questions can point to the same stored answer</strong>, which means less storage blowup. <strong>Be smart about TTL by usage</strong>, if something is reused often, retain it longer, otherwise, remove it.</p><p>I would also suggest you do it after you see repetition in the logs rather than at the start. It may be that the use case is just not good for it.</p><p>As for how to do it, many databases can do this for you but there are also libraries like semanticcache, prompt-cache, GPTCache, vCache, Upstash semantic-cache, Redis + LangCache, that help with plumbing.</p><p>There are obviously savings here to be made though. <a href="https://redis.io/blog/how-to-cache-semantic-search/">Redis</a> claims up to 68.8% fewer API calls and 40&#8211;50% latency improvement, though be aware this is a bit of marketing as they are using a clear Q&amp;A use cases here.</p><p>So it completely depends on your setup. <strong>If you have a Q/A bot with a lot of redundant calls, then you can save more. </strong>If you have a <strong>coding bot with unique calls, then you&#8217;ll save less</strong>.</p><p>Prompt caching does well when the changing question sits inside a large static prompt. Semantic caching does well when people keep asking the same thing in different words.</p><p>You can play around with this <a href="https://claude.ai/public/artifacts/9231bfa1-6291-4c05-8082-10f1b6f282b8">interactive tool</a> to see the cost savings with both.</p><p>If you&#8217;re looking at 15% semantic match, the additional savings aren&#8217;t as steep, but if that number increased above 45&#8211;50% then it could make more sense.</p><p>Before we move on from this section, I would just point out that there are a <strong>lot of savings to be made from standard caching too.</strong></p><p>Remember to cache the expensive deterministic stuff like SQL query results, tool outputs, and retrieval results. Never run this stuff more times than you need to.</p><p>I do this for one of my tools. It gathers keyword data to summarize, then caches it until that data is stale. If it is stale, it reruns it when that route is hit.</p><p>So, semantic caching is an interesting concept and could save you tokens for certain use cases but it takes engineering to do it well.</p><h2>Don&#8217;t preload dormant tokens</h2><p>This part is about what happens when your system prompt starts growing because of things like bulky tools or growing memory.</p><p>For smaller agents, this isn&#8217;t really an issue, but if you are working with agent specs that keep growing, there are ways to slim it down and fetch information on-demand (or at least try to).</p><h3>Keep context slim and fetch details on-demand</h3><p>Once your agent prompt grows beyond a certain point, it can be good to keep the always-loaded layer as small and stable as possible, and keep growing details separate.</p><p>This matters because once these layers start to grow, such as when you load a few hundred tools or send full MCP server descriptions that keep changing, it gets noisy.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!A4Rc!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F420c41cf-bd0b-4dbf-9808-8f45f6d41f46_1400x775.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!A4Rc!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F420c41cf-bd0b-4dbf-9808-8f45f6d41f46_1400x775.png 424w, /__u/substackcdn.com/image/fetch/$s_!A4Rc!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F420c41cf-bd0b-4dbf-9808-8f45f6d41f46_1400x775.png 848w, /__u/substackcdn.com/image/fetch/$s_!A4Rc!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F420c41cf-bd0b-4dbf-9808-8f45f6d41f46_1400x775.png 1272w, /__u/substackcdn.com/image/fetch/$s_!A4Rc!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F420c41cf-bd0b-4dbf-9808-8f45f6d41f46_1400x775.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!A4Rc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F420c41cf-bd0b-4dbf-9808-8f45f6d41f46_1400x775.png" width="1400" height="775" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/420c41cf-bd0b-4dbf-9808-8f45f6d41f46_1400x775.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:775,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!A4Rc!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F420c41cf-bd0b-4dbf-9808-8f45f6d41f46_1400x775.png 424w, /__u/substackcdn.com/image/fetch/$s_!A4Rc!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F420c41cf-bd0b-4dbf-9808-8f45f6d41f46_1400x775.png 848w, /__u/substackcdn.com/image/fetch/$s_!A4Rc!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F420c41cf-bd0b-4dbf-9808-8f45f6d41f46_1400x775.png 1272w, /__u/substackcdn.com/image/fetch/$s_!A4Rc!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F420c41cf-bd0b-4dbf-9808-8f45f6d41f46_1400x775.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The problem is obviously not just cost, but also performance. And if one of these layers keeps changing, prompt caching becomes much harder to hit properly.</p><p>So, the idea is to keep the top layer as compact and stable as you can. The top layer should help the model understand where it is and where to go next, but it does not need to carry the whole world up front.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!KXT2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cf777d1-ad83-4819-9e10-648f04d15357_1400x642.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!KXT2!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cf777d1-ad83-4819-9e10-648f04d15357_1400x642.png 424w, /__u/substackcdn.com/image/fetch/$s_!KXT2!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cf777d1-ad83-4819-9e10-648f04d15357_1400x642.png 848w, /__u/substackcdn.com/image/fetch/$s_!KXT2!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cf777d1-ad83-4819-9e10-648f04d15357_1400x642.png 1272w, /__u/substackcdn.com/image/fetch/$s_!KXT2!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cf777d1-ad83-4819-9e10-648f04d15357_1400x642.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!KXT2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cf777d1-ad83-4819-9e10-648f04d15357_1400x642.png" width="1400" height="642" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9cf777d1-ad83-4819-9e10-648f04d15357_1400x642.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:642,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!KXT2!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cf777d1-ad83-4819-9e10-648f04d15357_1400x642.png 424w, /__u/substackcdn.com/image/fetch/$s_!KXT2!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cf777d1-ad83-4819-9e10-648f04d15357_1400x642.png 848w, /__u/substackcdn.com/image/fetch/$s_!KXT2!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cf777d1-ad83-4819-9e10-648f04d15357_1400x642.png 1272w, /__u/substackcdn.com/image/fetch/$s_!KXT2!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9cf777d1-ad83-4819-9e10-648f04d15357_1400x642.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>If you&#8217;ve looked through the source code for Claude Code, you&#8217;ve seen that they use something like this for their memory system.</p><p>They have an always-loaded index file that shouldn&#8217;t grow beyond 200 lines, with detailed topic files elsewhere. <em>Though what the agent does in practice versus what the system wants is a topic for another time.</em></p><p>You can see the same idea pop up elsewhere too, such as in Claude&#8217;s advanced tool setup, Claude Skills&#8217; layered setup, and attempts to lazy-load MCP tools instead of dumping every server definition into the prompt up front.</p><h3>Where this is done and if it works</h3><p>The idea is sound. When context grows, it gets harder for an LLM to pick the right action. But this space is still early, so we&#8217;ll go through a tool as an example to see how this can work.</p><p>A few months ago, Anthropic released something called advanced Tool Search. This goes into the space of how to keep context slim while still giving the model access to hundreds of tools.</p><p>Anthropic <a href="https://www.anthropic.com/engineering/advanced-tool-use">says</a> they have seen 55K to 134K tokens of tool definitions before optimization, and that wrong tool selection is a common failure mode when the context grows this large.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!p4bj!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8a22e1e-a8cd-4a18-9107-0f38572220bd_1400x752.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!p4bj!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8a22e1e-a8cd-4a18-9107-0f38572220bd_1400x752.png 424w, /__u/substackcdn.com/image/fetch/$s_!p4bj!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8a22e1e-a8cd-4a18-9107-0f38572220bd_1400x752.png 848w, /__u/substackcdn.com/image/fetch/$s_!p4bj!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8a22e1e-a8cd-4a18-9107-0f38572220bd_1400x752.png 1272w, /__u/substackcdn.com/image/fetch/$s_!p4bj!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8a22e1e-a8cd-4a18-9107-0f38572220bd_1400x752.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!p4bj!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8a22e1e-a8cd-4a18-9107-0f38572220bd_1400x752.png" width="1400" height="752" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e8a22e1e-a8cd-4a18-9107-0f38572220bd_1400x752.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:752,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!p4bj!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8a22e1e-a8cd-4a18-9107-0f38572220bd_1400x752.png 424w, /__u/substackcdn.com/image/fetch/$s_!p4bj!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8a22e1e-a8cd-4a18-9107-0f38572220bd_1400x752.png 848w, /__u/substackcdn.com/image/fetch/$s_!p4bj!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8a22e1e-a8cd-4a18-9107-0f38572220bd_1400x752.png 1272w, /__u/substackcdn.com/image/fetch/$s_!p4bj!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe8a22e1e-a8cd-4a18-9107-0f38572220bd_1400x752.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>So, a search tool would then optimize the context by having the LLM use it to find tools, rather than define them all up front.</p><pre><code>tools=[
        {
            &#8220;type&#8221;: &#8220;tool_search_tool_bm25_20251119&#8221;, 
            &#8220;name&#8221;: &#8220;tool_search&#8221;
        },
        {
            &#8220;name&#8221;: &#8220;search_contacts&#8221;,
            &#8220;description&#8221;: &#8220;Find a contact by name or email.&#8221;,
            &#8220;input_schema&#8221;: {
                &#8220;type&#8221;: &#8220;object&#8221;,
                &#8220;properties&#8221;: {
                    &#8220;query&#8221;: {&#8221;type&#8221;: &#8220;string&#8221;}
                },
                &#8220;required&#8221;: [&#8221;query&#8221;]
            }
        },
        {
            &#8220;name&#8221;: &#8220;send_email&#8221;,
            &#8220;description&#8221;: &#8220;Send an email to one or more recipients.&#8221;,
            &#8220;input_schema&#8221;: {
                &#8220;type&#8221;: &#8220;object&#8221;,
                &#8220;properties&#8221;: {
                    &#8220;to&#8221;: {&#8221;type&#8221;: &#8220;string&#8221;},
                    &#8220;subject&#8221;: {&#8221;type&#8221;: &#8220;string&#8221;},
                    &#8220;body&#8221;: {&#8221;type&#8221;: &#8220;string&#8221;}
                },
                &#8220;required&#8221;: [&#8221;to&#8221;, &#8220;subject&#8221;, &#8220;body&#8221;]
            },
            &#8220;defer_loading&#8221;: True
        }
    ]</code></pre><p>What you see above is that we define one tool called <code>tool_search</code>. You can pick one of the out-of-the-box options, BM25 or Regex, or build your own custom one. Then we set one tool as deferred as an example. You would only do this if you had 10+ tools though.</p><p>Anthropic does the searching for you, so you don&#8217;t see how it adds this tool schema into the system prompt, nor do you see how the search happens under the hood.</p><p>They do say that once there is a tool match, its definition is appended inline as a <code>tool_reference</code> block in the conversation for the LLM.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!0iO-!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a7dcd68-9410-4984-973e-05f0daa00ef8_1400x567.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!0iO-!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a7dcd68-9410-4984-973e-05f0daa00ef8_1400x567.png 424w, /__u/substackcdn.com/image/fetch/$s_!0iO-!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a7dcd68-9410-4984-973e-05f0daa00ef8_1400x567.png 848w, /__u/substackcdn.com/image/fetch/$s_!0iO-!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a7dcd68-9410-4984-973e-05f0daa00ef8_1400x567.png 1272w, /__u/substackcdn.com/image/fetch/$s_!0iO-!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a7dcd68-9410-4984-973e-05f0daa00ef8_1400x567.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!0iO-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a7dcd68-9410-4984-973e-05f0daa00ef8_1400x567.png" width="1400" height="567" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2a7dcd68-9410-4984-973e-05f0daa00ef8_1400x567.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:567,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!0iO-!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a7dcd68-9410-4984-973e-05f0daa00ef8_1400x567.png 424w, /__u/substackcdn.com/image/fetch/$s_!0iO-!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a7dcd68-9410-4984-973e-05f0daa00ef8_1400x567.png 848w, /__u/substackcdn.com/image/fetch/$s_!0iO-!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a7dcd68-9410-4984-973e-05f0daa00ef8_1400x567.png 1272w, /__u/substackcdn.com/image/fetch/$s_!0iO-!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2a7dcd68-9410-4984-973e-05f0daa00ef8_1400x567.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><strong>The idea is neat: smaller initial context, but you&#8217;re still adding one extra search step.</strong> People have also <a href="https://www.arcade.dev/blog/anthropic-tool-search-4000-tools-test/">tested</a> this tool with somewhat lackluster results, but that was with 4,000 tools, so there is more room for testing.</p><p>It&#8217;s also on us to define the tools well enough that they can be searched. But it becomes harder to debug what is happening when you can&#8217;t see the intermediate step.</p><p>This idea pops up elsewhere too, but people generally just call it good AI engineering. Do not expose the agent to huge messy context. Instead, give it a way to narrow things down, and only then let it inspect or load the tool when needed.</p><p>For this part, there are serious savings to be made as well, though it depends on how many tokens you are sending in the first place. We created <a href="https://claude.ai/public/artifacts/bedb2567-6e8f-4d71-bcc6-4c9c18cde2cb">this</a> additional calculator that compares tool search and prompt caching.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!Qkum!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa05a5405-744e-4311-ae6e-cca7847f2a0c_1400x819.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!Qkum!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa05a5405-744e-4311-ae6e-cca7847f2a0c_1400x819.png 424w, /__u/substackcdn.com/image/fetch/$s_!Qkum!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa05a5405-744e-4311-ae6e-cca7847f2a0c_1400x819.png 848w, /__u/substackcdn.com/image/fetch/$s_!Qkum!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa05a5405-744e-4311-ae6e-cca7847f2a0c_1400x819.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Qkum!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa05a5405-744e-4311-ae6e-cca7847f2a0c_1400x819.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!Qkum!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa05a5405-744e-4311-ae6e-cca7847f2a0c_1400x819.png" width="1400" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a05a5405-744e-4311-ae6e-cca7847f2a0c_1400x819.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:819,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!Qkum!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa05a5405-744e-4311-ae6e-cca7847f2a0c_1400x819.png 424w, /__u/substackcdn.com/image/fetch/$s_!Qkum!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa05a5405-744e-4311-ae6e-cca7847f2a0c_1400x819.png 848w, /__u/substackcdn.com/image/fetch/$s_!Qkum!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa05a5405-744e-4311-ae6e-cca7847f2a0c_1400x819.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Qkum!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa05a5405-744e-4311-ae6e-cca7847f2a0c_1400x819.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Vibe calculated example of savings for prompt caching + lazy-loading tools</p><p>What we see is that both prompt caching and lazy-loading context give you savings, but together it&#8217;s not a huge change. Tool search like this isn&#8217;t just about savings though as it helps keep the context clean performance wise.</p><p>But if you&#8217;re just looking for savings, the biggest win is to pick at least one.</p><h2>Use cheap models for cheap work</h2><p>This section is about routing prompts to different models, along with using subagents with cheaper models for certain tasks, and how this can decrease token costs but also risk quality.</p><p>This space is interesting because most people argue that 60% or more of incoming questions are easy tasks, and thus do not need the strongest model, especially not a thinking one.</p><p>ChatGPT does this using signals like conversation type, complexity, tool needs, and explicit intent (&#8220;think hard&#8221;). Claude uses description-based delegation and built-in subagents like Explore.</p><p>The idea is simple to understand, but being able to do it right without risking too much quality is the hard part.</p><p>So, let&#8217;s go through both predictive routing and output-checked approaches like cascades and subagents, so you can get a feel for what you can test on your own.</p><p>The savings that can be made here are very real. I created another interactive graph for this part that you can find <a href="https://claude.ai/public/artifacts/f231d43b-0063-40d4-9907-36fe18cdafce">here</a>.</p><h3>Route to models based on task difficulty</h3><p>Request-level routing means trying to estimate difficulty and intent before seeing any output. The upside is high, but a bad choice can poison the whole session, so there are some quality drawbacks to keep in mind.</p><p>To do this, you need some kind of router model that decides where to route the request.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!DoiY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8906fdbd-ef39-4930-980b-89be73142ed2_1400x555.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!DoiY!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8906fdbd-ef39-4930-980b-89be73142ed2_1400x555.png 424w, /__u/substackcdn.com/image/fetch/$s_!DoiY!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8906fdbd-ef39-4930-980b-89be73142ed2_1400x555.png 848w, /__u/substackcdn.com/image/fetch/$s_!DoiY!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8906fdbd-ef39-4930-980b-89be73142ed2_1400x555.png 1272w, /__u/substackcdn.com/image/fetch/$s_!DoiY!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8906fdbd-ef39-4930-980b-89be73142ed2_1400x555.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!DoiY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8906fdbd-ef39-4930-980b-89be73142ed2_1400x555.png" width="1400" height="555" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8906fdbd-ef39-4930-980b-89be73142ed2_1400x555.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:555,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!DoiY!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8906fdbd-ef39-4930-980b-89be73142ed2_1400x555.png 424w, /__u/substackcdn.com/image/fetch/$s_!DoiY!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8906fdbd-ef39-4930-980b-89be73142ed2_1400x555.png 848w, /__u/substackcdn.com/image/fetch/$s_!DoiY!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8906fdbd-ef39-4930-980b-89be73142ed2_1400x555.png 1272w, /__u/substackcdn.com/image/fetch/$s_!DoiY!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8906fdbd-ef39-4930-980b-89be73142ed2_1400x555.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>We don&#8217;t know exactly what OpenAI uses as signals to route to different models, but I don&#8217;t know about you, I frequently feel like I&#8217;m being delegated to a less competent model at times and it can be infuriating.</p><p>There are ways for us to still gather intel though, looking at the open source community. We can look at <a href="https://github.com/lm-sys/RouteLLM">RouteLLM</a> from LMSYS, the Berkeley group behind Chatbot Arena. This solution learns from real preference data from Chatbot Arena.</p><p>RouteLLM uses standard embeddings and then a tiny router head, so hosting this shouldn&#8217;t be that expensive.</p><p>I have not tested this solution myself, but they report large cost reductions while keeping most of GPT-4&#8217;s performance.</p><p>I did, though, dig into the <a href="https://arxiv.org/abs/2601.07206">LLMRouterBench</a> paper, which pretty much said that many learned routers barely beat simple baselines, such as keyword/heuristic routing, embedding nearest-neighbor, or kNN-style routing.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!Gy8B!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5a713981-2527-4219-8ea9-9a1a3e7653cb_1400x781.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!Gy8B!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5a713981-2527-4219-8ea9-9a1a3e7653cb_1400x781.png 424w, /__u/substackcdn.com/image/fetch/$s_!Gy8B!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5a713981-2527-4219-8ea9-9a1a3e7653cb_1400x781.png 848w, /__u/substackcdn.com/image/fetch/$s_!Gy8B!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5a713981-2527-4219-8ea9-9a1a3e7653cb_1400x781.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Gy8B!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5a713981-2527-4219-8ea9-9a1a3e7653cb_1400x781.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!Gy8B!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5a713981-2527-4219-8ea9-9a1a3e7653cb_1400x781.png" width="1400" height="781" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5a713981-2527-4219-8ea9-9a1a3e7653cb_1400x781.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:781,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!Gy8B!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5a713981-2527-4219-8ea9-9a1a3e7653cb_1400x781.png 424w, /__u/substackcdn.com/image/fetch/$s_!Gy8B!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5a713981-2527-4219-8ea9-9a1a3e7653cb_1400x781.png 848w, /__u/substackcdn.com/image/fetch/$s_!Gy8B!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5a713981-2527-4219-8ea9-9a1a3e7653cb_1400x781.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Gy8B!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5a713981-2527-4219-8ea9-9a1a3e7653cb_1400x781.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This means that the fancy routers around may not give you that much of a boost compared to just using something simple.</p><p>People haven&#8217;t abandoned routing because of this, but people are still tinkering with it, so it&#8217;s not a slam dunk in terms of savings if the quality of the answers can&#8217;t keep up.</p><p>Now, there are also out-of-the-box solutions in this space, such as OpenRouter Auto and Switchpoint. However, there is nothing public on their routing internals or public accuracy numbers in the way I wanted.</p><p>But for this section, also check out the savings <a href="https://claude.ai/public/artifacts/f231d43b-0063-40d4-9907-36fe18cdafce">calculator</a> we did for LLMRouter, heuristics, self-hosted classifier, LLM-as-router, RouteLLM, OpenRouter Auto.</p><p>As for quality and how this works for real-world projects, I can&#8217;t say before doing better testing on my own first with clear numbers to show, so this space really deserves its own article in the future.</p><p>We should also briefly cover cascading and then subagents before moving on.</p><h3>Start with cheap and cascade on low confidence</h3><p>Instead of guessing from the prompt whether a request is &#8220;easy&#8221; or &#8220;hard,&#8221; we can also let the cheap model try first, then decide whether to keep that answer or escalate.</p><p>Google&#8217;s &#8220;Speculative Cascades&#8221; write-up frames this tradeoff: use smaller models first for cost and speed, and defer to larger models only when needed.</p><p>To do this, you have the cheap model generate first, then use a lightweight checker that looks at things like logprobs/token probabilities, entropy or margin-style uncertainty, and/or semantic alignment.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!5AWr!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F27292e8b-d0b0-4bb7-b841-b112105d483e_1400x622.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!5AWr!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F27292e8b-d0b0-4bb7-b841-b112105d483e_1400x622.png 424w, /__u/substackcdn.com/image/fetch/$s_!5AWr!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F27292e8b-d0b0-4bb7-b841-b112105d483e_1400x622.png 848w, /__u/substackcdn.com/image/fetch/$s_!5AWr!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F27292e8b-d0b0-4bb7-b841-b112105d483e_1400x622.png 1272w, /__u/substackcdn.com/image/fetch/$s_!5AWr!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F27292e8b-d0b0-4bb7-b841-b112105d483e_1400x622.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!5AWr!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F27292e8b-d0b0-4bb7-b841-b112105d483e_1400x622.png" width="1400" height="622" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/27292e8b-d0b0-4bb7-b841-b112105d483e_1400x622.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:622,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!5AWr!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F27292e8b-d0b0-4bb7-b841-b112105d483e_1400x622.png 424w, /__u/substackcdn.com/image/fetch/$s_!5AWr!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F27292e8b-d0b0-4bb7-b841-b112105d483e_1400x622.png 848w, /__u/substackcdn.com/image/fetch/$s_!5AWr!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F27292e8b-d0b0-4bb7-b841-b112105d483e_1400x622.png 1272w, /__u/substackcdn.com/image/fetch/$s_!5AWr!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F27292e8b-d0b0-4bb7-b841-b112105d483e_1400x622.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This idea is pretty attractive, as prompt difficulty is often hard to predict and most routers don&#8217;t do perfectly. Furthermore, quality is easier to judge after you have an answer.</p><p>It also only makes sense if you think most questions can be answered by a simpler model, as you need to pay for two calls for the ones being escalated.</p><p>But from the people implementing this, I&#8217;ve heard it&#8217;s an attractive choice as validation latency between calls can stay under 20ms.</p><p>I did look into some open-source implementations like <a href="https://github.com/lemony-ai/cascadeflow">CascadeFlow,</a> which claims 69% savings and 96% quality retention vs GPT-5. But it&#8217;s good to note that the prompts they tested had verifiable ground truth, such as math answers and multiple choice.</p><p>A main issue to consider is that small models are often &#8220;confidently wrong,&#8221; so it might make sense to start with conservative thresholds and escalate more often. That will inevitably bring costs up.</p><p>I also added in Cascade (cheap-first) into the <a href="https://claude.ai/public/artifacts/f231d43b-0063-40d4-9907-36fe18cdafce">interactive graph</a>, so you can compare the savings with the other choices. If true, it may slash costs by 50% using this technique, if you need larger models for certain requests at all, that is.</p><h3>Delegate work to subagents</h3><p>Subagents are about delegating work to isolated agents. Sometimes these use smaller models, so we can say it&#8217;s a form of routing as well. The savings aren&#8217;t as steep here, but it&#8217;s worth mentioning.</p><p>Delegating to subagents is not just about cost. It&#8217;s also about keeping the context clean so each agent can fully focus on the task it should complete.</p><p>Anthropic ships Claude Code with built-in subagents, as many have seen. The Explore subagent is explicitly a Haiku worker for codebase search and exploration. So, the design principle is there: use smaller models for cheaper tasks.</p><p>The main Claude session also delegates via description matching, but we don&#8217;t see it. We just get cheaper aggregate cost.</p><p>But because the orchestrator often still stays in the loop for planning, synthesis, and retries, you don&#8217;t save as much as we saw with routing.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!KQA4!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19135e10-8d9d-4eb3-943c-cde360b5b591_1400x695.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!KQA4!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19135e10-8d9d-4eb3-943c-cde360b5b591_1400x695.png 424w, /__u/substackcdn.com/image/fetch/$s_!KQA4!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19135e10-8d9d-4eb3-943c-cde360b5b591_1400x695.png 848w, /__u/substackcdn.com/image/fetch/$s_!KQA4!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19135e10-8d9d-4eb3-943c-cde360b5b591_1400x695.png 1272w, /__u/substackcdn.com/image/fetch/$s_!KQA4!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19135e10-8d9d-4eb3-943c-cde360b5b591_1400x695.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!KQA4!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19135e10-8d9d-4eb3-943c-cde360b5b591_1400x695.png" width="1400" height="695" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/19135e10-8d9d-4eb3-943c-cde360b5b591_1400x695.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:695,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!KQA4!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19135e10-8d9d-4eb3-943c-cde360b5b591_1400x695.png 424w, /__u/substackcdn.com/image/fetch/$s_!KQA4!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19135e10-8d9d-4eb3-943c-cde360b5b591_1400x695.png 848w, /__u/substackcdn.com/image/fetch/$s_!KQA4!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19135e10-8d9d-4eb3-943c-cde360b5b591_1400x695.png 1272w, /__u/substackcdn.com/image/fetch/$s_!KQA4!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19135e10-8d9d-4eb3-943c-cde360b5b591_1400x695.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>You can look at the <a href="https://claude.ai/public/artifacts/f231d43b-0063-40d4-9907-36fe18cdafce">graphs</a> we created above and see that subagents may shave off around 11% from the &#8220;no routing&#8221; option by our calculations, so it is not the main thing to go for if you&#8217;re just looking to cut costs.</p><p>My next article will dig into subagents, but more as a way to delegate work and isolate tasks when working with deepagents.</p><p>Let&#8217;s go through the last section before rounding off.</p><h2>Keep your context clean</h2><p>Good context engineering is usually about performance, but it can also be about cost efficiency. So, let&#8217;s go through context compaction and talk about how keeping the context clean can save tokens.</p><p>The issue is that agents keep accumulating junk: tool outputs, logs, repeated observations, old plans, stale attempts, and duplicated state.</p><p>This is especially true for people building agents for the first time, where they keep dumping results into the working state for the main agent.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!rdJJ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0e1eed19-0a6c-4f62-a717-8c3c20e2cc19_1400x781.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!rdJJ!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0e1eed19-0a6c-4f62-a717-8c3c20e2cc19_1400x781.png 424w, /__u/substackcdn.com/image/fetch/$s_!rdJJ!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0e1eed19-0a6c-4f62-a717-8c3c20e2cc19_1400x781.png 848w, /__u/substackcdn.com/image/fetch/$s_!rdJJ!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0e1eed19-0a6c-4f62-a717-8c3c20e2cc19_1400x781.png 1272w, /__u/substackcdn.com/image/fetch/$s_!rdJJ!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0e1eed19-0a6c-4f62-a717-8c3c20e2cc19_1400x781.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!rdJJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0e1eed19-0a6c-4f62-a717-8c3c20e2cc19_1400x781.png" width="1400" height="781" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0e1eed19-0a6c-4f62-a717-8c3c20e2cc19_1400x781.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:781,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!rdJJ!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0e1eed19-0a6c-4f62-a717-8c3c20e2cc19_1400x781.png 424w, /__u/substackcdn.com/image/fetch/$s_!rdJJ!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0e1eed19-0a6c-4f62-a717-8c3c20e2cc19_1400x781.png 848w, /__u/substackcdn.com/image/fetch/$s_!rdJJ!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0e1eed19-0a6c-4f62-a717-8c3c20e2cc19_1400x781.png 1272w, /__u/substackcdn.com/image/fetch/$s_!rdJJ!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0e1eed19-0a6c-4f62-a717-8c3c20e2cc19_1400x781.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>I&#8217;ve naturally done this myself too, especially with a draft agent, to see &#8220;how it does&#8221; first.</p><p>But I&#8217;ve also seen people complain about OpenClaw context build-up, so it happens everywhere. People complain about it in Claude Code too, because in general, it&#8217;s easier to just have it add stuff to it than to work on cleaning it up.</p><p>Let&#8217;s briefly talk about this without going too much into the performance side, which is also why you should do it.</p><h3>The hard part is building a state pipeline</h3><p>This is a two-tier problem. Not only are you &#8220;compressing the chat,&#8221; but you also need to keep things clean as you add them to the working state, and this becomes tedious engineering work.</p><p>First, to keep the context clean, we don&#8217;t want this kind of result to start eating up the context.</p><pre><code>bad state:
agent does work
&#8594; dumps tool output into context
&#8594; reads files
&#8594; dumps files into context
&#8594; runs tests
&#8594; dumps logs into context
&#8594; retries
&#8594; keeps everything</code></pre><p>So, the real job is first to preserve the right state while deleting exhaust as you go along.</p><p>Raw output like this can go into an archive, and only what is needed goes into active context. In general, the enemy is probably tool-output bloat here, so the work is to make tools less noisy by default.</p><pre><code>Good active context

[system rules]
[project rules]
[user task]
[current working state]

Keep:
+ auth flow lives in auth.ts + session.ts
+ bug only happens on refresh path
+ failing test: session_refresh_keeps_user
+ likely overwrite during refresh
+ files in scope: auth.ts, session.ts, auth.test.ts

Drop:
- raw grep results
- full test logs
- duplicate file dumps
- dead-end retries</code></pre><p>I&#8217;m also thinking certain pieces of context can have a lifecycle or a set expiry.</p><p>Then, once you reach the point where you need to compress it, it will be easier to know what is useful for the LLM.</p><p>If we do a little Anthropic reading on long-horizon tasks, they note that you need to figure out a way to preserve architectural decisions, unresolved bugs, and implementation details for compression once it gets to that point as well.</p><p>For LangChain&#8217;s autonomous compression, they have the agent decide when to compact, instead of only doing it after the context is already bloated, as I think is the case with Anthropic.</p><p>It&#8217;s interesting that teams are starting to evaluate compression as a systems problem too, with benchmarks and agent-specific policies, not as a generic summarization trick.</p><p>We can look at a recent paper here too to get an idea of the results we can get. This <a href="https://arxiv.org/html/2603.28119v1">one by Jia et al.</a> argued that at 6x compression, it gave a 51.8&#8211;71.3% token-budget reduction, while achieving a 5.0&#8211;9.2% improvement in issue resolution rates on SWE-bench Verified.</p><p>So, this is not just about cost, but also about performance in general.</p><p>As for costs, there is obviously a lot of work in building good context itself, but removing junk can probably clear up 30&#8211;70% of your context, which then saves just as much in dollars.</p><p>To illustrate, for a 10k context window, if you clean up 30% to 50% at 100k runs, you might save up to $1,500. At a 40k context window, that number goes up to $6,000.</p><p>We did a calculation for this <a href="https://claude.ai/public/artifacts/5b50845f-6fc9-4912-a577-100183ef253a">here</a> as well so you can visualize it. It&#8217;s good to note that compressing agents that are using very small cheap models may turn out to be more expensive.</p><p>Nevertheless, what&#8217;s good about trying to keep the context clean is that you&#8217;re not sacrificing quality, as can happen with semantic caching or routing, so if done well, it&#8217;s a clear gain.</p><p>The issue is obviously the work to do so.</p><h2>Rounding up the conversation</h2><p>This is a very long article that serves up four different ways you can cut token costs when building agents.</p><p>It very much depends on your use case, use prompt caching when dealing with large system prompts that stay unchanged as you loop LLM calls, use semantic caching if you are dealing with a generic Q/A bot that needs to stay cheap.</p><p>Test routing if you need to be able to answer both easy and hard questions, and if you want to make sure you don&#8217;t send unnecessary tokens keep the context as clean as possible.</p><p>It may be worth it in the future to make a shorter, more economics focused article to focus on certain setups.</p><p>Nevertheless I hope it was informational, follow me here, at <a href="https://www.linkedin.com/in/ida-silfverskiold/">LinkedIn,</a> or via my <a href="https://www.ilsilfverskiold.com/">website</a>, if just want to read more of the same stuff.</p><p>&#10084;</p>]]></content:encoded></item><item><title><![CDATA[Agentic RAG: Company Knowledge Slack Agents]]></title><description><![CDATA[Lessons learnt using LlamaIndex and Modal]]></description><link>https://howtouseai.substack.com/p/agentic-rag-company-knowledge-slack</link><guid isPermaLink="false">https://howtouseai.substack.com/p/agentic-rag-company-knowledge-slack</guid><dc:creator><![CDATA[Ida Silfverskiold]]></dc:creator><pubDate>Thu, 25 Sep 2025 10:24:53 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!l1Eq!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63de09e8-cced-4cac-afb0-ad35a2ba2e97_1400x789.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!l1Eq!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63de09e8-cced-4cac-afb0-ad35a2ba2e97_1400x789.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!l1Eq!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63de09e8-cced-4cac-afb0-ad35a2ba2e97_1400x789.png 424w, /__u/substackcdn.com/image/fetch/$s_!l1Eq!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63de09e8-cced-4cac-afb0-ad35a2ba2e97_1400x789.png 848w, /__u/substackcdn.com/image/fetch/$s_!l1Eq!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63de09e8-cced-4cac-afb0-ad35a2ba2e97_1400x789.png 1272w, /__u/substackcdn.com/image/fetch/$s_!l1Eq!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63de09e8-cced-4cac-afb0-ad35a2ba2e97_1400x789.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!l1Eq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63de09e8-cced-4cac-afb0-ad35a2ba2e97_1400x789.png" width="1400" height="789" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/63de09e8-cced-4cac-afb0-ad35a2ba2e97_1400x789.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:789,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!l1Eq!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63de09e8-cced-4cac-afb0-ad35a2ba2e97_1400x789.png 424w, /__u/substackcdn.com/image/fetch/$s_!l1Eq!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63de09e8-cced-4cac-afb0-ad35a2ba2e97_1400x789.png 848w, /__u/substackcdn.com/image/fetch/$s_!l1Eq!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63de09e8-cced-4cac-afb0-ad35a2ba2e97_1400x789.png 1272w, /__u/substackcdn.com/image/fetch/$s_!l1Eq!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63de09e8-cced-4cac-afb0-ad35a2ba2e97_1400x789.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>I would have figured that most companies would have built or implemented their own RAG agents by now.</p><p>An AI knowledge agent can dig through internal documentation &#8212; websites, PDFs, random docs &#8212; and answer employees in Slack (or Teams/Discord) within a few seconds. So, these bots should significantly reduce time sifting through information for employees.</p><p>I&#8217;ve seen a few of these in bigger tech companies, like AskHR from IBM, but they aren&#8217;t all that mainstream yet.</p><p>If you&#8217;re keen to understand how they are built and how much resources it takes to build a simple one, this is an article for you.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!yiCc!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9627ee9-88f4-4965-acd0-4f248fd5c3b5_1400x553.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!yiCc!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9627ee9-88f4-4965-acd0-4f248fd5c3b5_1400x553.png 424w, /__u/substackcdn.com/image/fetch/$s_!yiCc!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9627ee9-88f4-4965-acd0-4f248fd5c3b5_1400x553.png 848w, /__u/substackcdn.com/image/fetch/$s_!yiCc!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9627ee9-88f4-4965-acd0-4f248fd5c3b5_1400x553.png 1272w, /__u/substackcdn.com/image/fetch/$s_!yiCc!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9627ee9-88f4-4965-acd0-4f248fd5c3b5_1400x553.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!yiCc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9627ee9-88f4-4965-acd0-4f248fd5c3b5_1400x553.png" width="1400" height="553" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f9627ee9-88f4-4965-acd0-4f248fd5c3b5_1400x553.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:553,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!yiCc!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9627ee9-88f4-4965-acd0-4f248fd5c3b5_1400x553.png 424w, /__u/substackcdn.com/image/fetch/$s_!yiCc!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9627ee9-88f4-4965-acd0-4f248fd5c3b5_1400x553.png 848w, /__u/substackcdn.com/image/fetch/$s_!yiCc!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9627ee9-88f4-4965-acd0-4f248fd5c3b5_1400x553.png 1272w, /__u/substackcdn.com/image/fetch/$s_!yiCc!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9627ee9-88f4-4965-acd0-4f248fd5c3b5_1400x553.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>I&#8217;ll go through the tools, techniques, and architecture involved, while also looking at the economics of building something like this. I&#8217;ll also include a section on what you&#8217;ll end up focusing the most on.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!zoud!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fddb78375-58c3-4919-86c0-d3734b76309f_1400x488.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!zoud!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fddb78375-58c3-4919-86c0-d3734b76309f_1400x488.png 424w, /__u/substackcdn.com/image/fetch/$s_!zoud!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fddb78375-58c3-4919-86c0-d3734b76309f_1400x488.png 848w, /__u/substackcdn.com/image/fetch/$s_!zoud!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fddb78375-58c3-4919-86c0-d3734b76309f_1400x488.png 1272w, /__u/substackcdn.com/image/fetch/$s_!zoud!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fddb78375-58c3-4919-86c0-d3734b76309f_1400x488.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!zoud!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fddb78375-58c3-4919-86c0-d3734b76309f_1400x488.png" width="1400" height="488" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ddb78375-58c3-4919-86c0-d3734b76309f_1400x488.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:488,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!zoud!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fddb78375-58c3-4919-86c0-d3734b76309f_1400x488.png 424w, /__u/substackcdn.com/image/fetch/$s_!zoud!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fddb78375-58c3-4919-86c0-d3734b76309f_1400x488.png 848w, /__u/substackcdn.com/image/fetch/$s_!zoud!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fddb78375-58c3-4919-86c0-d3734b76309f_1400x488.png 1272w, /__u/substackcdn.com/image/fetch/$s_!zoud!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fddb78375-58c3-4919-86c0-d3734b76309f_1400x488.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>There is also a demo at the end for what this will look like in Slack.</p><p>If you&#8217;re already familiar with RAG, feel free to skip the next section &#8212; it&#8217;s just a bit of repetitive stuff around agents and RAG.</p><h3><strong>What is RAG and Agentic RAG?</strong></h3><p>Retrieval-Augmented Generation (RAG) is a way to fetch information that gets fed into the large language model (LLM) before it answers the user&#8217;s question.</p><p>This allows us to provide relevant information from various documents to the bot in real time so it can answer the user correctly.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!LA4p!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe6896cc-fac1-4c41-b2fe-b17af40122d6_1400x678.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!LA4p!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe6896cc-fac1-4c41-b2fe-b17af40122d6_1400x678.png 424w, /__u/substackcdn.com/image/fetch/$s_!LA4p!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe6896cc-fac1-4c41-b2fe-b17af40122d6_1400x678.png 848w, /__u/substackcdn.com/image/fetch/$s_!LA4p!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe6896cc-fac1-4c41-b2fe-b17af40122d6_1400x678.png 1272w, /__u/substackcdn.com/image/fetch/$s_!LA4p!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe6896cc-fac1-4c41-b2fe-b17af40122d6_1400x678.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!LA4p!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe6896cc-fac1-4c41-b2fe-b17af40122d6_1400x678.png" width="1400" height="678" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/be6896cc-fac1-4c41-b2fe-b17af40122d6_1400x678.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:678,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!LA4p!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe6896cc-fac1-4c41-b2fe-b17af40122d6_1400x678.png 424w, /__u/substackcdn.com/image/fetch/$s_!LA4p!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe6896cc-fac1-4c41-b2fe-b17af40122d6_1400x678.png 848w, /__u/substackcdn.com/image/fetch/$s_!LA4p!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe6896cc-fac1-4c41-b2fe-b17af40122d6_1400x678.png 1272w, /__u/substackcdn.com/image/fetch/$s_!LA4p!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe6896cc-fac1-4c41-b2fe-b17af40122d6_1400x678.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This retrieval system is doing more than simple keyword search, as it finds similar matches rather than just exact ones. For example, if someone asks about fonts, a similarity search might return documents on typography.</p><p>Many would say that RAG is a fairly simple concept to understand, but how you store information, how you fetch it, and what kind of embedding models you use still matter a lot.</p><p>If you&#8217;re keen to learn more about embeddings and retrieval, I&#8217;ve written about this <a href="https://medium.com/data-science/working-with-embeddings-closed-versus-open-source-39491f0b95c2">here</a>.</p><p>Today, people have gone further and primarily work with agent systems.</p><p>In agent systems, the LLM can decide where and how it should fetch information, rather than just having content dumped into its context before generating a response.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!nj5y!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb020720-196a-49fb-baf9-b5ad26b0c5de_1400x614.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!nj5y!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb020720-196a-49fb-baf9-b5ad26b0c5de_1400x614.png 424w, /__u/substackcdn.com/image/fetch/$s_!nj5y!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb020720-196a-49fb-baf9-b5ad26b0c5de_1400x614.png 848w, /__u/substackcdn.com/image/fetch/$s_!nj5y!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb020720-196a-49fb-baf9-b5ad26b0c5de_1400x614.png 1272w, /__u/substackcdn.com/image/fetch/$s_!nj5y!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb020720-196a-49fb-baf9-b5ad26b0c5de_1400x614.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!nj5y!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb020720-196a-49fb-baf9-b5ad26b0c5de_1400x614.png" width="1400" height="614" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/eb020720-196a-49fb-baf9-b5ad26b0c5de_1400x614.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:614,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!nj5y!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb020720-196a-49fb-baf9-b5ad26b0c5de_1400x614.png 424w, /__u/substackcdn.com/image/fetch/$s_!nj5y!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb020720-196a-49fb-baf9-b5ad26b0c5de_1400x614.png 848w, /__u/substackcdn.com/image/fetch/$s_!nj5y!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb020720-196a-49fb-baf9-b5ad26b0c5de_1400x614.png 1272w, /__u/substackcdn.com/image/fetch/$s_!nj5y!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb020720-196a-49fb-baf9-b5ad26b0c5de_1400x614.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>It&#8217;s important to remember that just because more advanced tools exist doesn&#8217;t mean you should always use them. You want to keep the system intuitive and also keep API calls to a minimum.</p><p>With agent systems the API calls will increase, as it needs to at least call one tool and then make another call to generate a response.</p><p>That said, I really like the user experience of the bot &#8220;going somewhere&#8221; &#8212; to a tool &#8212; to look something up. Seeing that flow in Slack helps the user understand what&#8217;s happening.</p><p>But going with an agent or using a full framework isn&#8217;t necessarily the better choice. I&#8217;ll elaborate on this as we continue.</p><h3>Technical Stack</h3><p>There is a ton of options for agent frameworks, vector databases, and deployment options, so I&#8217;ll go through some.</p><p>For the <strong>deployment option</strong>, since we&#8217;re working with Slack webhooks, we&#8217;re dealing with event-driven architecture where the code only runs when there&#8217;s a question in Slack.</p><p>To keep costs to a minimum, we can use <strong>serverless functions</strong>. The choice is either going with AWS Lambda or picking a new vendor.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!1Utw!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc02d9e71-cb69-4a55-a811-bcb24dd876f1_1400x457.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!1Utw!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc02d9e71-cb69-4a55-a811-bcb24dd876f1_1400x457.png 424w, /__u/substackcdn.com/image/fetch/$s_!1Utw!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc02d9e71-cb69-4a55-a811-bcb24dd876f1_1400x457.png 848w, /__u/substackcdn.com/image/fetch/$s_!1Utw!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc02d9e71-cb69-4a55-a811-bcb24dd876f1_1400x457.png 1272w, /__u/substackcdn.com/image/fetch/$s_!1Utw!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc02d9e71-cb69-4a55-a811-bcb24dd876f1_1400x457.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!1Utw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc02d9e71-cb69-4a55-a811-bcb24dd876f1_1400x457.png" width="1400" height="457" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c02d9e71-cb69-4a55-a811-bcb24dd876f1_1400x457.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:457,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!1Utw!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc02d9e71-cb69-4a55-a811-bcb24dd876f1_1400x457.png 424w, /__u/substackcdn.com/image/fetch/$s_!1Utw!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc02d9e71-cb69-4a55-a811-bcb24dd876f1_1400x457.png 848w, /__u/substackcdn.com/image/fetch/$s_!1Utw!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc02d9e71-cb69-4a55-a811-bcb24dd876f1_1400x457.png 1272w, /__u/substackcdn.com/image/fetch/$s_!1Utw!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc02d9e71-cb69-4a55-a811-bcb24dd876f1_1400x457.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Platforms like Modal are technically built to serve LLM models, but they work well for long-running ETL processes, and for LLM apps in general.</p><p>Modal hasn&#8217;t been battle-tested as much, and you&#8217;ll notice that in terms of latency, but it&#8217;s very smooth and offers super cheap CPU pricing.</p><p>I should note though that when setting this up with Modal on the free tier, I&#8217;ve had a few 500 errors, but that might be expected.</p><p>As for <strong>how to pick the agent framework</strong>, this is completely optional. I did a comparison piece a few weeks ago on open-source agentic frameworks that you can find <a href="https://medium.com/data-science-collective/agentic-ai-comparing-new-open-source-frameworks-21ec676732df">here</a>, and the one I left out was <strong>LlamaIndex</strong>.</p><p>So I decided to give it a try here.</p><p>The last thing you need to pick is a <strong>vector database,</strong> or a database that supports vector search. This is where we store the embeddings and other metadata, so we can perform similarity search when a user&#8217;s query comes in.</p><p>There are a lot of options out there, but I think the ones with the highest potential are Weaviate, Milvus, pgvector, Redis, and Qdrant.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!fc7b!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19705de9-c655-4edf-93e4-7e250dccb5ef_1400x876.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!fc7b!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19705de9-c655-4edf-93e4-7e250dccb5ef_1400x876.png 424w, /__u/substackcdn.com/image/fetch/$s_!fc7b!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19705de9-c655-4edf-93e4-7e250dccb5ef_1400x876.png 848w, /__u/substackcdn.com/image/fetch/$s_!fc7b!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19705de9-c655-4edf-93e4-7e250dccb5ef_1400x876.png 1272w, /__u/substackcdn.com/image/fetch/$s_!fc7b!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19705de9-c655-4edf-93e4-7e250dccb5ef_1400x876.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!fc7b!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19705de9-c655-4edf-93e4-7e250dccb5ef_1400x876.png" width="1400" height="876" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/19705de9-c655-4edf-93e4-7e250dccb5ef_1400x876.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:876,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!fc7b!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19705de9-c655-4edf-93e4-7e250dccb5ef_1400x876.png 424w, /__u/substackcdn.com/image/fetch/$s_!fc7b!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19705de9-c655-4edf-93e4-7e250dccb5ef_1400x876.png 848w, /__u/substackcdn.com/image/fetch/$s_!fc7b!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19705de9-c655-4edf-93e4-7e250dccb5ef_1400x876.png 1272w, /__u/substackcdn.com/image/fetch/$s_!fc7b!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19705de9-c655-4edf-93e4-7e250dccb5ef_1400x876.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Both Qdrant and Milvus have pretty generous free tiers for their cloud options. Qdrant, I know, allows us to store both dense and sparse vectors. Llamaindex, along with most agent frameworks, support many different vector databases so any can work.</p><p>I&#8217;ll try Milvus more in the future to compare performance and latency, but for now, Qdrant works well.</p><p>Redis is a solid pick too, or really any vector extension of your existing database.</p><h3>Cost &amp; time to build</h3><p>In terms of time and cost, you have to account for engineering hours, cloud, embedding, and large language model<strong> </strong>(LLM) costs.</p><p>It doesn&#8217;t take that much time to boot up a framework to run something minimal. What takes time is connecting the content properly, prompting the system, parsing the outputs, and making sure it runs fast enough.</p><p>But if we turn to overhead costs, <strong>cloud costs</strong> to run the agent system is <strong>minimal</strong> for just one bot for one company using serverless functions as you saw in the table in the last section.</p><p>However, for the <strong>vector databases</strong>, it will get more expensive the more data you store.</p><p>Both Zilliz and Qdrant Cloud has a good amount of free tier for your first 1 to 5GBs of data, so unless you go beyond a few thousand chunks you may not pay for anything.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!aQEi!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96ae1e23-c95f-49fa-9918-7735c665edfb_1400x444.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!aQEi!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96ae1e23-c95f-49fa-9918-7735c665edfb_1400x444.png 424w, /__u/substackcdn.com/image/fetch/$s_!aQEi!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96ae1e23-c95f-49fa-9918-7735c665edfb_1400x444.png 848w, /__u/substackcdn.com/image/fetch/$s_!aQEi!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96ae1e23-c95f-49fa-9918-7735c665edfb_1400x444.png 1272w, /__u/substackcdn.com/image/fetch/$s_!aQEi!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96ae1e23-c95f-49fa-9918-7735c665edfb_1400x444.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!aQEi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96ae1e23-c95f-49fa-9918-7735c665edfb_1400x444.png" width="1400" height="444" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/96ae1e23-c95f-49fa-9918-7735c665edfb_1400x444.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:444,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!aQEi!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96ae1e23-c95f-49fa-9918-7735c665edfb_1400x444.png 424w, /__u/substackcdn.com/image/fetch/$s_!aQEi!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96ae1e23-c95f-49fa-9918-7735c665edfb_1400x444.png 848w, /__u/substackcdn.com/image/fetch/$s_!aQEi!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96ae1e23-c95f-49fa-9918-7735c665edfb_1400x444.png 1272w, /__u/substackcdn.com/image/fetch/$s_!aQEi!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F96ae1e23-c95f-49fa-9918-7735c665edfb_1400x444.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>You will start paying though once you go beyond the thousands mark, with Weaviate being the most expensive of the vendors above.</p><p>As for the <strong>embeddings,</strong> these are <strong>generally very cheap.</strong></p><p>You can see a table below on using OpenAI&#8217;s <code>text-embedding-3-small</code> with chunks of different sizes once you embed 1 to 10 million texts.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!Ee8Z!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2ca0006-523a-4b32-bc24-6a191e01a3d2_1400x445.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!Ee8Z!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2ca0006-523a-4b32-bc24-6a191e01a3d2_1400x445.png 424w, /__u/substackcdn.com/image/fetch/$s_!Ee8Z!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2ca0006-523a-4b32-bc24-6a191e01a3d2_1400x445.png 848w, /__u/substackcdn.com/image/fetch/$s_!Ee8Z!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2ca0006-523a-4b32-bc24-6a191e01a3d2_1400x445.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Ee8Z!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2ca0006-523a-4b32-bc24-6a191e01a3d2_1400x445.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!Ee8Z!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2ca0006-523a-4b32-bc24-6a191e01a3d2_1400x445.png" width="1400" height="445" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b2ca0006-523a-4b32-bc24-6a191e01a3d2_1400x445.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:445,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!Ee8Z!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2ca0006-523a-4b32-bc24-6a191e01a3d2_1400x445.png 424w, /__u/substackcdn.com/image/fetch/$s_!Ee8Z!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2ca0006-523a-4b32-bc24-6a191e01a3d2_1400x445.png 848w, /__u/substackcdn.com/image/fetch/$s_!Ee8Z!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2ca0006-523a-4b32-bc24-6a191e01a3d2_1400x445.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Ee8Z!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb2ca0006-523a-4b32-bc24-6a191e01a3d2_1400x445.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>When people start optimizing for embeddings and storage, they&#8217;ve usually moved beyond embedding millions of texts.</p><p>The one thing that <strong>matters the most </strong>though is what<strong> large language model (LLM) you use</strong>. You need to think about API prices, since an agent system will typically call an LLM two to four times per run.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!vBAC!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd66788c-e043-4e23-93f3-742f29cfc465_1400x570.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!vBAC!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd66788c-e043-4e23-93f3-742f29cfc465_1400x570.png 424w, /__u/substackcdn.com/image/fetch/$s_!vBAC!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd66788c-e043-4e23-93f3-742f29cfc465_1400x570.png 848w, /__u/substackcdn.com/image/fetch/$s_!vBAC!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd66788c-e043-4e23-93f3-742f29cfc465_1400x570.png 1272w, /__u/substackcdn.com/image/fetch/$s_!vBAC!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd66788c-e043-4e23-93f3-742f29cfc465_1400x570.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!vBAC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd66788c-e043-4e23-93f3-742f29cfc465_1400x570.png" width="1400" height="570" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/dd66788c-e043-4e23-93f3-742f29cfc465_1400x570.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:570,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!vBAC!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd66788c-e043-4e23-93f3-742f29cfc465_1400x570.png 424w, /__u/substackcdn.com/image/fetch/$s_!vBAC!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd66788c-e043-4e23-93f3-742f29cfc465_1400x570.png 848w, /__u/substackcdn.com/image/fetch/$s_!vBAC!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd66788c-e043-4e23-93f3-742f29cfc465_1400x570.png 1272w, /__u/substackcdn.com/image/fetch/$s_!vBAC!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd66788c-e043-4e23-93f3-742f29cfc465_1400x570.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>For this system, I&#8217;m using GPT-4o-mini or Gemini Flash 2.0, which are the cheapest options.</p><p>So let&#8217;s say a company is using the bot a few hundred times per day and each run costs us 2&#8211;4 API calls, we might end up at around less of a dollar per day and around $10&#8211;50 dollars per month.</p><p>You can see that switching to a more expensive model would increase the monthly bill by 10x to 100x. Using ChatGPT is mostly subsidized for free users, but when you build your own applications you&#8217;ll be financing it.</p><p>There will be smarter and cheaper models in the future, so whatever you build now will likely improve over time. But start small, because costs add up and for simple systems like this you don&#8217;t need them to be exceptional.</p><p>The next section will get into how to build this system.</p><h3>The architecture (processing documents)</h3><p>The system has two parts. The first is how we split up documents &#8212; what we call chunking &#8212; and embed them. This first part is very important, as it will dictate how the agent answers later.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!AYga!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9b32b13-37f8-410d-b4af-39f957cf51cd_1400x716.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!AYga!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9b32b13-37f8-410d-b4af-39f957cf51cd_1400x716.png 424w, /__u/substackcdn.com/image/fetch/$s_!AYga!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9b32b13-37f8-410d-b4af-39f957cf51cd_1400x716.png 848w, /__u/substackcdn.com/image/fetch/$s_!AYga!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9b32b13-37f8-410d-b4af-39f957cf51cd_1400x716.png 1272w, /__u/substackcdn.com/image/fetch/$s_!AYga!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9b32b13-37f8-410d-b4af-39f957cf51cd_1400x716.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!AYga!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9b32b13-37f8-410d-b4af-39f957cf51cd_1400x716.png" width="1400" height="716" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c9b32b13-37f8-410d-b4af-39f957cf51cd_1400x716.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:716,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!AYga!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9b32b13-37f8-410d-b4af-39f957cf51cd_1400x716.png 424w, /__u/substackcdn.com/image/fetch/$s_!AYga!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9b32b13-37f8-410d-b4af-39f957cf51cd_1400x716.png 848w, /__u/substackcdn.com/image/fetch/$s_!AYga!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9b32b13-37f8-410d-b4af-39f957cf51cd_1400x716.png 1272w, /__u/substackcdn.com/image/fetch/$s_!AYga!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9b32b13-37f8-410d-b4af-39f957cf51cd_1400x716.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>So, to make sure you&#8217;re preparing all the sources properly, you need to think carefully about how to chunk them.</p><p>If you look at the document above, you can see that we can miss context if we split the document based on headings but also on the number of characters where the paragraphs attached to the first heading is split up for being too long.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!CmId!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42d6f73e-d713-4a8c-8b01-c0b92b328ecb_1400x580.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!CmId!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42d6f73e-d713-4a8c-8b01-c0b92b328ecb_1400x580.png 424w, /__u/substackcdn.com/image/fetch/$s_!CmId!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42d6f73e-d713-4a8c-8b01-c0b92b328ecb_1400x580.png 848w, /__u/substackcdn.com/image/fetch/$s_!CmId!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42d6f73e-d713-4a8c-8b01-c0b92b328ecb_1400x580.png 1272w, /__u/substackcdn.com/image/fetch/$s_!CmId!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42d6f73e-d713-4a8c-8b01-c0b92b328ecb_1400x580.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!CmId!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42d6f73e-d713-4a8c-8b01-c0b92b328ecb_1400x580.png" width="1400" height="580" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/42d6f73e-d713-4a8c-8b01-c0b92b328ecb_1400x580.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:580,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!CmId!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42d6f73e-d713-4a8c-8b01-c0b92b328ecb_1400x580.png 424w, /__u/substackcdn.com/image/fetch/$s_!CmId!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42d6f73e-d713-4a8c-8b01-c0b92b328ecb_1400x580.png 848w, /__u/substackcdn.com/image/fetch/$s_!CmId!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42d6f73e-d713-4a8c-8b01-c0b92b328ecb_1400x580.png 1272w, /__u/substackcdn.com/image/fetch/$s_!CmId!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F42d6f73e-d713-4a8c-8b01-c0b92b328ecb_1400x580.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>You need to be smart about ensuring each chunk has enough context (but not too much). You also need to make sure the chunk is attached to metadata so it&#8217;s easy to trace back to where it was found.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!NPm5!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9ca7a02-b543-43e5-985d-37fcc5f300a7_1400x660.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!NPm5!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9ca7a02-b543-43e5-985d-37fcc5f300a7_1400x660.png 424w, /__u/substackcdn.com/image/fetch/$s_!NPm5!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9ca7a02-b543-43e5-985d-37fcc5f300a7_1400x660.png 848w, /__u/substackcdn.com/image/fetch/$s_!NPm5!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9ca7a02-b543-43e5-985d-37fcc5f300a7_1400x660.png 1272w, /__u/substackcdn.com/image/fetch/$s_!NPm5!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9ca7a02-b543-43e5-985d-37fcc5f300a7_1400x660.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!NPm5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9ca7a02-b543-43e5-985d-37fcc5f300a7_1400x660.png" width="1400" height="660" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c9ca7a02-b543-43e5-985d-37fcc5f300a7_1400x660.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:660,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!NPm5!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9ca7a02-b543-43e5-985d-37fcc5f300a7_1400x660.png 424w, /__u/substackcdn.com/image/fetch/$s_!NPm5!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9ca7a02-b543-43e5-985d-37fcc5f300a7_1400x660.png 848w, /__u/substackcdn.com/image/fetch/$s_!NPm5!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9ca7a02-b543-43e5-985d-37fcc5f300a7_1400x660.png 1272w, /__u/substackcdn.com/image/fetch/$s_!NPm5!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9ca7a02-b543-43e5-985d-37fcc5f300a7_1400x660.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This is where you&#8217;ll spend the most time, and honestly, I think there should be better tools out there to do this intelligently.</p><p>I ended up using Docling for PDFs, building it out to attach elements based on headings and paragraph sizes. For web pages, I built a crawler that looked over page elements to decide whether to chunk based on anchor tags, headings, or general content.</p><p>Remember, if the bot is supposed to cite sources, each chunk needs to be attached to URLs, anchor tags, page numbers, block IDs, permalinks so the system can locate the information correctly being used.</p><p>There is also the option to keep the chunks small, but fetching surrounding chunks for context expansion after retrieval.</p><p>You then make sure that the retrieval stage has a greater chance of finding the correct information while making sure the LLM has enough information to provide a coherent answer.</p><p>Since most of the content you&#8217;re working with is scattered and often low quality, I also decided to just push in summarizations using an LLM.</p><p>These summaries were given different labels with higher authority, which meant they were prioritized during retrieval. I suppose you can use them to gatekeep other chunks, working with it as a filter. </p><p>Here another option is to add semantic topics to the chunks that you later use on retrieval. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!cOJG!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3b17296-3799-40fa-ad27-a221bc088893_1400x734.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!cOJG!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3b17296-3799-40fa-ad27-a221bc088893_1400x734.png 424w, /__u/substackcdn.com/image/fetch/$s_!cOJG!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3b17296-3799-40fa-ad27-a221bc088893_1400x734.png 848w, /__u/substackcdn.com/image/fetch/$s_!cOJG!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3b17296-3799-40fa-ad27-a221bc088893_1400x734.png 1272w, /__u/substackcdn.com/image/fetch/$s_!cOJG!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3b17296-3799-40fa-ad27-a221bc088893_1400x734.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!cOJG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3b17296-3799-40fa-ad27-a221bc088893_1400x734.png" width="1400" height="734" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a3b17296-3799-40fa-ad27-a221bc088893_1400x734.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:734,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!cOJG!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3b17296-3799-40fa-ad27-a221bc088893_1400x734.png 424w, /__u/substackcdn.com/image/fetch/$s_!cOJG!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3b17296-3799-40fa-ad27-a221bc088893_1400x734.png 848w, /__u/substackcdn.com/image/fetch/$s_!cOJG!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3b17296-3799-40fa-ad27-a221bc088893_1400x734.png 1272w, /__u/substackcdn.com/image/fetch/$s_!cOJG!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3b17296-3799-40fa-ad27-a221bc088893_1400x734.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>There is also the option to push in the summaries in their own tools, and keep deep dive information separate. Letting the agent decide which one to use but it will look strange to users as it&#8217;s not intuitive behavior.</p><p>Still, I have to stress that if the quality of the source information is poor, it&#8217;s hard to make the system work well.</p><p>For example, if a user asks how an API request should be made and there are four different web pages giving different answers, the bot won&#8217;t know which one is most relevant. You can naturally timestamp the chunks but if the docs are ingested at the same time then it will be harder. </p><p>To show companies how it worked, I had to do some manual review. I also had AI do deeper research around the company to help fill in gaps, and then I embedded that too.</p><p>In the future, I think I&#8217;ll build something better for document ingestion. </p><h3>The architecture (the agent)</h3><p>For the second part, where we connect to this data, we need to build a system where an agent can connect to different tools that contain different amounts of data from our vector database.</p><p>We keep to one agent only to make it easy enough to control. This one agent can decide what information it needs based on the user&#8217;s question</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!jm0H!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F317c2a40-a451-4bf1-970a-e57fe2a81afb_1400x766.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!jm0H!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F317c2a40-a451-4bf1-970a-e57fe2a81afb_1400x766.png 424w, /__u/substackcdn.com/image/fetch/$s_!jm0H!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F317c2a40-a451-4bf1-970a-e57fe2a81afb_1400x766.png 848w, /__u/substackcdn.com/image/fetch/$s_!jm0H!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F317c2a40-a451-4bf1-970a-e57fe2a81afb_1400x766.png 1272w, /__u/substackcdn.com/image/fetch/$s_!jm0H!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F317c2a40-a451-4bf1-970a-e57fe2a81afb_1400x766.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!jm0H!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F317c2a40-a451-4bf1-970a-e57fe2a81afb_1400x766.png" width="1400" height="766" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/317c2a40-a451-4bf1-970a-e57fe2a81afb_1400x766.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:766,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!jm0H!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F317c2a40-a451-4bf1-970a-e57fe2a81afb_1400x766.png 424w, /__u/substackcdn.com/image/fetch/$s_!jm0H!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F317c2a40-a451-4bf1-970a-e57fe2a81afb_1400x766.png 848w, /__u/substackcdn.com/image/fetch/$s_!jm0H!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F317c2a40-a451-4bf1-970a-e57fe2a81afb_1400x766.png 1272w, /__u/substackcdn.com/image/fetch/$s_!jm0H!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F317c2a40-a451-4bf1-970a-e57fe2a81afb_1400x766.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>It&#8217;s good not to complicate things and build it out to use too many agents, or you&#8217;ll run into issues, especially with these smaller models.</p><p>Although this may go against my own recommendations, I did set up a first LLM function that decides if we need to run the agent at all.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!V5iq!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fff6f7247-63e7-47fa-a839-bae152440e14_1400x651.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!V5iq!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fff6f7247-63e7-47fa-a839-bae152440e14_1400x651.png 424w, /__u/substackcdn.com/image/fetch/$s_!V5iq!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fff6f7247-63e7-47fa-a839-bae152440e14_1400x651.png 848w, /__u/substackcdn.com/image/fetch/$s_!V5iq!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fff6f7247-63e7-47fa-a839-bae152440e14_1400x651.png 1272w, /__u/substackcdn.com/image/fetch/$s_!V5iq!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fff6f7247-63e7-47fa-a839-bae152440e14_1400x651.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!V5iq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fff6f7247-63e7-47fa-a839-bae152440e14_1400x651.png" width="1400" height="651" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ff6f7247-63e7-47fa-a839-bae152440e14_1400x651.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:651,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!V5iq!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fff6f7247-63e7-47fa-a839-bae152440e14_1400x651.png 424w, /__u/substackcdn.com/image/fetch/$s_!V5iq!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fff6f7247-63e7-47fa-a839-bae152440e14_1400x651.png 848w, /__u/substackcdn.com/image/fetch/$s_!V5iq!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fff6f7247-63e7-47fa-a839-bae152440e14_1400x651.png 1272w, /__u/substackcdn.com/image/fetch/$s_!V5iq!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fff6f7247-63e7-47fa-a839-bae152440e14_1400x651.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This was primarily for the user experience, as it takes a few extra seconds to boot up the agent (even when starting it as a background task when the container starts).</p><p>As for how to build the agent itself, this is easy, as LlamaIndex does most of the work for us. For this, you can use the <code>FunctionAgent</code>, passing in different tools when setting it up.</p><pre><code># Only runs if the first LLM thinks it is necessary

access_links_tool = get_access_links_tool()
public_docs_tool = get_public_docs_tool()
onboarding_tool = get_onboarding_information_tool()
general_info_tool = get_general_info_tool()
    
formatted_system_prompt = get_system_prompt(team_name)
    
agent = FunctionAgent(
  tools=[onboarding_tool, public_docs_tool, access_links_tool, general_info_tool],
  llm=global_llm,
  system_prompt=formatted_system_prompt
)</code></pre><p>The tools have access to different data from the vector database, and they are wrappers around the <code>CitationQueryEngine</code>. This engine helps to cite the source nodes in the text. We can access the source nodes at the end of the agent run, which you can attach to the message and in the footer.</p><p>To make sure the user experience is good, you can tap into the event stream to send updates back to Slack.</p><pre><code>handler = agent.run(user_msg=full_msg, ctx=ctx, memory=memory)

async for event in handler.stream_events():
  if isinstance(event, ToolCall):
     display_tool_name = format_tool_name(event.tool_name)
     message = f&#8221;&#9989; Checking {display_tool_name}&#8221;
     post_thinking(message)
  if isinstance(event, ToolCallResult):
     post_thinking(f&#8221;&#9989; Done checking...&#8221;)

final_output = await handler  
final_text = final_output
blocks = build_slack_blocks(final_text, mention)

post_to_slack(
  channel_id=channel_id, 
  blocks=blocks,
  timestamp=initial_message_ts,
  client=client 
)</code></pre><p>Make sure to format the messages and Slack blocks well, and refine the system prompt for the agent so it formats the messages correctly based on the information that the tools will return.</p><p>The architecture should be easy enough to understand, but there are still some retrieval techniques we should dig into.</p><h3>Techniques you can try</h3><p>A lot of people will emphasize certain techniques when building RAG systems, and they&#8217;re partially right. You should use hybrid search along with some kind of re-ranking.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!654G!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff511eac7-966f-4210-afab-50c96236c1fa_1400x638.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!654G!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff511eac7-966f-4210-afab-50c96236c1fa_1400x638.png 424w, /__u/substackcdn.com/image/fetch/$s_!654G!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff511eac7-966f-4210-afab-50c96236c1fa_1400x638.png 848w, /__u/substackcdn.com/image/fetch/$s_!654G!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff511eac7-966f-4210-afab-50c96236c1fa_1400x638.png 1272w, /__u/substackcdn.com/image/fetch/$s_!654G!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff511eac7-966f-4210-afab-50c96236c1fa_1400x638.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!654G!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff511eac7-966f-4210-afab-50c96236c1fa_1400x638.png" width="1400" height="638" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f511eac7-966f-4210-afab-50c96236c1fa_1400x638.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:638,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!654G!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff511eac7-966f-4210-afab-50c96236c1fa_1400x638.png 424w, /__u/substackcdn.com/image/fetch/$s_!654G!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff511eac7-966f-4210-afab-50c96236c1fa_1400x638.png 848w, /__u/substackcdn.com/image/fetch/$s_!654G!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff511eac7-966f-4210-afab-50c96236c1fa_1400x638.png 1272w, /__u/substackcdn.com/image/fetch/$s_!654G!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff511eac7-966f-4210-afab-50c96236c1fa_1400x638.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The first I will mention is hybrid search when we perform retrieval.</p><p>I mentioned that we use semantic similarity to fetch chunks of data in the various tools, but you also need to account for cases where exact keyword search is required.</p><p>Just imagine a user asking for a specific certificate name, like CAT-00568. In that case, the system needs to find exact matches just as much as fuzzy ones.</p><p>With hybrid search, supported by both Qdrant and LlamaIndex, we use both dense and sparse vectors.</p><pre><code># when setting up the vector store (both for embedding and fetching)
vector_store = QdrantVectorStore(
   client=client,
   aclient=async_client,
   collection_name=&#8221;knowledge_bases&#8221;,
   enable_hybrid=True,
   fastembed_sparse_model=&#8221;Qdrant/bm25&#8221;
 )</code></pre><p>Sparse is perfect for exact keywords but blind to synonyms, whereas dense is great for &#8220;fuzzy&#8221; matches (&#8220;benefits policy&#8221; matches &#8220;employee perks&#8221;) but they can miss literal strings like <em>CAT-00568</em>.</p><p>Once the results are fetched, it&#8217;s useful to apply deduplication and re-ranking to filter out irrelevant chunks before sending them to the LLM for citation and synthesis.</p><pre><code>reranker = LLMRerank(llm=OpenAI(model=&#8221;gpt-3.5-turbo&#8221;), top_n=5)
dedup = SimilarityPostprocessor(similarity_cutoff=0.9)

engine = CitationQueryEngine(
    retriever=retriever,
    node_postprocessors=[dedup, reranker],
    metadata_mode=MetadataMode.ALL,
)</code></pre><p>This part wouldn&#8217;t be necessary if your data were exceptionally clean, which is why it shouldn&#8217;t be your main focus. It adds overhead and another API call.</p><p>It&#8217;s also not necessary to use a large model for re-ranking, but you&#8217;ll need to do some research on your own to figure out your options.</p><p>These techniques are easy to understand and quick to set up, so they aren&#8217;t where you&#8217;ll spend most of your time.</p><h3>What you&#8217;ll actually spend time on</h3><p>Most of the things you&#8217;ll spend time on aren&#8217;t so sexy. It&#8217;s prompting, reducing latency, and chunking documents correctly.</p><p>Before you start, you should <strong>look into different prompt templates</strong> from various frameworks to see how they prompt the models. You&#8217;ll spend quite a bit of time making sure the system prompt is well-crafted for the LLM you choose.</p><p>The second thing you&#8217;ll spend most of your time on is <strong>making it fas</strong>t. I&#8217;ve looked into internal tools from tech companies building <strong>AI knowledge agents</strong> and found they usually <strong>respond in about 8 to 13 seconds.</strong></p><p>So, you need something in that range.</p><p>Using a serverless provider can be a problem here because of cold starts. LLM providers also introduce their own latency, which is hard to control.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!Z0QD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F086b154f-fc18-43d4-bf14-24302fa2df2b_1400x543.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!Z0QD!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F086b154f-fc18-43d4-bf14-24302fa2df2b_1400x543.png 424w, /__u/substackcdn.com/image/fetch/$s_!Z0QD!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F086b154f-fc18-43d4-bf14-24302fa2df2b_1400x543.png 848w, /__u/substackcdn.com/image/fetch/$s_!Z0QD!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F086b154f-fc18-43d4-bf14-24302fa2df2b_1400x543.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Z0QD!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F086b154f-fc18-43d4-bf14-24302fa2df2b_1400x543.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!Z0QD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F086b154f-fc18-43d4-bf14-24302fa2df2b_1400x543.png" width="1400" height="543" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/086b154f-fc18-43d4-bf14-24302fa2df2b_1400x543.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:543,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!Z0QD!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F086b154f-fc18-43d4-bf14-24302fa2df2b_1400x543.png 424w, /__u/substackcdn.com/image/fetch/$s_!Z0QD!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F086b154f-fc18-43d4-bf14-24302fa2df2b_1400x543.png 848w, /__u/substackcdn.com/image/fetch/$s_!Z0QD!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F086b154f-fc18-43d4-bf14-24302fa2df2b_1400x543.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Z0QD!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F086b154f-fc18-43d4-bf14-24302fa2df2b_1400x543.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>That said, you can look into <strong>spinning up resources before they&#8217;re used</strong>, switching to <strong>lower-latency models</strong>, skipping frameworks to reduce overhead, and generally <strong>decreasing the number of API calls per run.</strong></p><p>The last thing, which takes a huge amount of work and which I&#8217;ve mentioned before, is <strong>chunking documents</strong>.</p><p>If you had exceptionally clean data with clear headers and separations, this part would be easy. But more often, you&#8217;ll be dealing with poorly structured HTML, PDFs, raw text files, Notion boards, and Confluence notes &#8212; often scattered and formatted inconsistently.</p><p>The challenge is figuring out how to programmatically ingest these documents so the system gets the full information needed to answer a question.</p><p>Just working with PDFs, for example, you&#8217;ll need to extract tables and images properly, separate sections by page numbers or layout elements, and trace each source back to the correct page.</p><p>You want enough context, but not chunks that are too large, or it will be harder to retrieve the right info later.</p><p>This kind of stuff isn&#8217;t well generalized. You can&#8217;t just push it in and expect the system to understand it &#8212; you have to think it through before you build it.</p><h3>How to build it out further</h3><p>At this point, it works well for what it&#8217;s supposed to do, but there are a few pieces I should cover (or people will think I&#8217;m simplifying too much). You&#8217;ll want to implement caching, a way to update the data, and long-term memory.</p><p><strong>Caching </strong>isn&#8217;t essential, but you can at least <strong>cache the query&#8217;s embedding</strong> in larger systems to speed up retrieval, and<strong> store recent source results</strong> for follow-up questions. I don&#8217;t think LlamaIndex helps much here, but you should be able to intercept the <code>QueryTool</code> on your own.</p><p>You&#8217;ll also want a way to continuously update information in the vector databases. This is the biggest headache &#8212; it&#8217;s hard to know when something has changed, so you need some kind of change-detection method along with an ID for each chunk.</p><p>You could just use periodic re-embedding strategies where you update a chunk with different meta tags altogether (this is my preferred approach because I&#8217;m lazy).</p><p>The last thing I want to mention is long-term memory for the agent, so it can understand conversations you&#8217;ve had in the past. For that, I&#8217;ve implemented some state by fetching history from the Slack API. This lets the agent see around 3&#8211;6 previous messages when responding.</p><p>We don&#8217;t want to push in too much history, since the context window grows &#8212; which not only increases cost but also tends to confuse the agent.</p><p>That said, there are better ways to handle long-term memory using external tools. I&#8217;m keen to write more on that in the future.</p><h3>Learnings and so on</h3><p>After doing this now for a bit I have a few notes to share about working with frameworks and keeping it simple (that I personally don&#8217;t always follow).</p><p>You learn a lot from using a framework, especially how to prompt well and how to structure the code. But at some point, working around the framework adds overhead.</p><p>For instance, in this system, I&#8217;m bypassing the framework a bit by adding an initial API call that decides whether to move on to the agent and responds to the user quickly.</p><p>If I had built this without a framework, I think I could have handled that kind of logic better where the first model decides what tool to call right away.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!tejE!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1f52450-33cc-4722-83b0-5bd5b2700c12_1400x623.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!tejE!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1f52450-33cc-4722-83b0-5bd5b2700c12_1400x623.png 424w, /__u/substackcdn.com/image/fetch/$s_!tejE!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1f52450-33cc-4722-83b0-5bd5b2700c12_1400x623.png 848w, /__u/substackcdn.com/image/fetch/$s_!tejE!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1f52450-33cc-4722-83b0-5bd5b2700c12_1400x623.png 1272w, /__u/substackcdn.com/image/fetch/$s_!tejE!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1f52450-33cc-4722-83b0-5bd5b2700c12_1400x623.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!tejE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1f52450-33cc-4722-83b0-5bd5b2700c12_1400x623.png" width="1400" height="623" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e1f52450-33cc-4722-83b0-5bd5b2700c12_1400x623.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:623,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!tejE!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1f52450-33cc-4722-83b0-5bd5b2700c12_1400x623.png 424w, /__u/substackcdn.com/image/fetch/$s_!tejE!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1f52450-33cc-4722-83b0-5bd5b2700c12_1400x623.png 848w, /__u/substackcdn.com/image/fetch/$s_!tejE!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1f52450-33cc-4722-83b0-5bd5b2700c12_1400x623.png 1272w, /__u/substackcdn.com/image/fetch/$s_!tejE!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe1f52450-33cc-4722-83b0-5bd5b2700c12_1400x623.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>I haven&#8217;t tried this but I&#8217;m assuming this would be cleaner.</p><p>Also, LlamaIndex optimizes the user query, which it should, before retrieval.</p><p>But sometimes it reduces the query too much, and I need to go in and fix it. The citation synthesizer doesn&#8217;t have access to the conversation history, so with that overly simplified query, it doesn&#8217;t always answer well.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!W2pd!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe88bbab6-7f03-475f-a21c-faceaf05b540_1400x514.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!W2pd!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe88bbab6-7f03-475f-a21c-faceaf05b540_1400x514.png 424w, /__u/substackcdn.com/image/fetch/$s_!W2pd!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe88bbab6-7f03-475f-a21c-faceaf05b540_1400x514.png 848w, /__u/substackcdn.com/image/fetch/$s_!W2pd!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe88bbab6-7f03-475f-a21c-faceaf05b540_1400x514.png 1272w, /__u/substackcdn.com/image/fetch/$s_!W2pd!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe88bbab6-7f03-475f-a21c-faceaf05b540_1400x514.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!W2pd!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe88bbab6-7f03-475f-a21c-faceaf05b540_1400x514.png" width="1400" height="514" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e88bbab6-7f03-475f-a21c-faceaf05b540_1400x514.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:514,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!W2pd!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe88bbab6-7f03-475f-a21c-faceaf05b540_1400x514.png 424w, /__u/substackcdn.com/image/fetch/$s_!W2pd!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe88bbab6-7f03-475f-a21c-faceaf05b540_1400x514.png 848w, /__u/substackcdn.com/image/fetch/$s_!W2pd!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe88bbab6-7f03-475f-a21c-faceaf05b540_1400x514.png 1272w, /__u/substackcdn.com/image/fetch/$s_!W2pd!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe88bbab6-7f03-475f-a21c-faceaf05b540_1400x514.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>With a framework, it&#8217;s also hard to trace where latency is coming from in the workflow since you can&#8217;t always see everything, even with observation tools.</p><p>Most developers recommend using frameworks for quick prototyping or bootstrapping, then rewriting the core logic with direct calls in production.</p><p>It&#8217;s not because the frameworks aren&#8217;t useful, but because at some point it&#8217;s better to write something you fully understand that only does what you need.</p><p>The general recommendation is to keep things as simple as possible and minimize LLM calls (which I am not even fully doing myself here).</p><p>But if all you need is RAG and not an agent, stick with that.</p><p>You can create a simple LLM call that sets the right parameters in the vector DB. From the user&#8217;s point of view, it&#8217;ll still look like the system is &#8220;looking into the database&#8221; and returning relevant info.</p><div><hr></div><p>Once finished though, the result will look like this:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!r4la!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F967056db-6901-4c0c-aedc-70c35fe80132_800x505.gif" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!r4la!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F967056db-6901-4c0c-aedc-70c35fe80132_800x505.gif 424w, /__u/substackcdn.com/image/fetch/$s_!r4la!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F967056db-6901-4c0c-aedc-70c35fe80132_800x505.gif 848w, /__u/substackcdn.com/image/fetch/$s_!r4la!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F967056db-6901-4c0c-aedc-70c35fe80132_800x505.gif 1272w, /__u/substackcdn.com/image/fetch/$s_!r4la!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F967056db-6901-4c0c-aedc-70c35fe80132_800x505.gif 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!r4la!,w_1456,c_limit,f_auto,q_auto:good,fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F967056db-6901-4c0c-aedc-70c35fe80132_800x505.gif" width="800" height="505" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/967056db-6901-4c0c-aedc-70c35fe80132_800x505.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:505,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!r4la!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F967056db-6901-4c0c-aedc-70c35fe80132_800x505.gif 424w, /__u/substackcdn.com/image/fetch/$s_!r4la!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F967056db-6901-4c0c-aedc-70c35fe80132_800x505.gif 848w, /__u/substackcdn.com/image/fetch/$s_!r4la!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F967056db-6901-4c0c-aedc-70c35fe80132_800x505.gif 1272w, /__u/substackcdn.com/image/fetch/$s_!r4la!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F967056db-6901-4c0c-aedc-70c35fe80132_800x505.gif 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>If you&#8217;re going down the same path, I hope this was useful.</p><p>There is bit more to it though. You&#8217;ll want to implement some kind of evaluation, guardrails, and monitoring (I&#8217;ve used Phoenix here).</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://howtouseai.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading AI Practical Deep Dives. Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p></p>]]></content:encoded></item><item><title><![CDATA[Agentic AI: Comparing New Open-Source Frameworks]]></title><description><![CDATA[By looking at functionality and learning curves]]></description><link>https://howtouseai.substack.com/p/agentic-ai-comparing-new-open-source</link><guid isPermaLink="false">https://howtouseai.substack.com/p/agentic-ai-comparing-new-open-source</guid><dc:creator><![CDATA[Ida Silfverskiold]]></dc:creator><pubDate>Wed, 24 Sep 2025 12:33:58 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!ydnx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F677ce991-089f-4ca8-b519-f3d1a512db09_1400x881.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!ydnx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F677ce991-089f-4ca8-b519-f3d1a512db09_1400x881.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!ydnx!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F677ce991-089f-4ca8-b519-f3d1a512db09_1400x881.jpeg 424w, /__u/substackcdn.com/image/fetch/$s_!ydnx!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F677ce991-089f-4ca8-b519-f3d1a512db09_1400x881.jpeg 848w, /__u/substackcdn.com/image/fetch/$s_!ydnx!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F677ce991-089f-4ca8-b519-f3d1a512db09_1400x881.jpeg 1272w, /__u/substackcdn.com/image/fetch/$s_!ydnx!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F677ce991-089f-4ca8-b519-f3d1a512db09_1400x881.jpeg 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!ydnx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F677ce991-089f-4ca8-b519-f3d1a512db09_1400x881.jpeg" width="1400" height="881" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/677ce991-089f-4ca8-b519-f3d1a512db09_1400x881.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:881,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!ydnx!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F677ce991-089f-4ca8-b519-f3d1a512db09_1400x881.jpeg 424w, /__u/substackcdn.com/image/fetch/$s_!ydnx!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F677ce991-089f-4ca8-b519-f3d1a512db09_1400x881.jpeg 848w, /__u/substackcdn.com/image/fetch/$s_!ydnx!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F677ce991-089f-4ca8-b519-f3d1a512db09_1400x881.jpeg 1272w, /__u/substackcdn.com/image/fetch/$s_!ydnx!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F677ce991-089f-4ca8-b519-f3d1a512db09_1400x881.jpeg 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>We&#8217;ve all heard of <strong><a href="https://www.crewai.com/">CrewAI</a></strong> and <strong><a href="https://microsoft.github.io/autogen/stable//index.html">AutoGen</a></strong>, but did you know there are dozens of open-source agentic frameworks out there &#8212; and many of them have been released in the last year.</p><p>I&#8217;ve briefly tested some of the more popular ones to get a feel for how they work and how easy they are to get started with. So follow along as I go through what each one brings to the table.</p><p>The focus will be on <strong>LangGraph</strong>, <strong>Agno</strong>, <strong>SmolAgents</strong>, <strong>Mastra</strong>, <strong>Pydantic AI</strong>, and <strong>Atomic Agents</strong>. We&#8217;ll also compare them to <strong>CrewAI</strong> and <strong>AutoGen</strong>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!B_RY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F768cc044-44e6-49c3-9da0-694b0fbc930a_1400x444.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!B_RY!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F768cc044-44e6-49c3-9da0-694b0fbc930a_1400x444.jpeg 424w, /__u/substackcdn.com/image/fetch/$s_!B_RY!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F768cc044-44e6-49c3-9da0-694b0fbc930a_1400x444.jpeg 848w, /__u/substackcdn.com/image/fetch/$s_!B_RY!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F768cc044-44e6-49c3-9da0-694b0fbc930a_1400x444.jpeg 1272w, /__u/substackcdn.com/image/fetch/$s_!B_RY!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F768cc044-44e6-49c3-9da0-694b0fbc930a_1400x444.jpeg 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!B_RY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F768cc044-44e6-49c3-9da0-694b0fbc930a_1400x444.jpeg" width="1400" height="444" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/768cc044-44e6-49c3-9da0-694b0fbc930a_1400x444.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:444,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!B_RY!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F768cc044-44e6-49c3-9da0-694b0fbc930a_1400x444.jpeg 424w, /__u/substackcdn.com/image/fetch/$s_!B_RY!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F768cc044-44e6-49c3-9da0-694b0fbc930a_1400x444.jpeg 848w, /__u/substackcdn.com/image/fetch/$s_!B_RY!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F768cc044-44e6-49c3-9da0-694b0fbc930a_1400x444.jpeg 1272w, /__u/substackcdn.com/image/fetch/$s_!B_RY!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F768cc044-44e6-49c3-9da0-694b0fbc930a_1400x444.jpeg 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>We&#8217;ll look at what a framework actually does, the different design choices, how they differ from one another, and a bit about the schools of thought behind them.</p><h3>Agentic AI</h3><p>Agentic AI is basically about building systems around LLMs so they can have accurate knowledge, access to data, and the ability to act. You can think of it as using natural language to automate processes and tasks.</p><p>Using natural language processing in automation isn&#8217;t new &#8212; we&#8217;ve used NLP for years to extract and process data. What&#8217;s new is the amount of freedom we can now give language models, allowing them to handle ambiguity and make decisions dynamically.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!ZnGk!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c4a8d33-c790-43c1-8eee-e9bd468f2f66_1400x506.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!ZnGk!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c4a8d33-c790-43c1-8eee-e9bd468f2f66_1400x506.png 424w, /__u/substackcdn.com/image/fetch/$s_!ZnGk!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c4a8d33-c790-43c1-8eee-e9bd468f2f66_1400x506.png 848w, /__u/substackcdn.com/image/fetch/$s_!ZnGk!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c4a8d33-c790-43c1-8eee-e9bd468f2f66_1400x506.png 1272w, /__u/substackcdn.com/image/fetch/$s_!ZnGk!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c4a8d33-c790-43c1-8eee-e9bd468f2f66_1400x506.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!ZnGk!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c4a8d33-c790-43c1-8eee-e9bd468f2f66_1400x506.png" width="1400" height="506" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4c4a8d33-c790-43c1-8eee-e9bd468f2f66_1400x506.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:506,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!ZnGk!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c4a8d33-c790-43c1-8eee-e9bd468f2f66_1400x506.png 424w, /__u/substackcdn.com/image/fetch/$s_!ZnGk!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c4a8d33-c790-43c1-8eee-e9bd468f2f66_1400x506.png 848w, /__u/substackcdn.com/image/fetch/$s_!ZnGk!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c4a8d33-c790-43c1-8eee-e9bd468f2f66_1400x506.png 1272w, /__u/substackcdn.com/image/fetch/$s_!ZnGk!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4c4a8d33-c790-43c1-8eee-e9bd468f2f66_1400x506.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>But just because LLMs can understand language doesn&#8217;t mean they have agency &#8212; or even understand the task you&#8217;re trying to automate. That&#8217;s why there&#8217;s a lot of engineering involved in building reliable systems.</p><p>I go through Agentic AI in more detail <a href="https://medium.com/gitconnected/agentic-workflows-build-a-tech-research-agent-da5e8247e123">here</a> and <a href="https://towardsdatascience.com/agentic-ai-single-vs-multi-agent-systems/">here</a>, if you&#8217;re keen to get a beginner friendly overview.</p><h3>What does a framework do?</h3><p>At their core, agentic frameworks help you with prompt engineering and routing data to and from the LLMs&#8212; but they also offer additional abstractions that make it easier to get started.</p><p>If you were to build a system from scratch where an LLM should use different APIs &#8212; tools &#8212; you&#8217;d define that in the system prompt. Then you&#8217;d request that the LLM returns its response along with the tool it wants to call, so the system can parse and execute the API call.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!ryAb!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6de498a8-318c-4585-9121-ab17dd84ff4e_1400x497.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!ryAb!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6de498a8-318c-4585-9121-ab17dd84ff4e_1400x497.png 424w, /__u/substackcdn.com/image/fetch/$s_!ryAb!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6de498a8-318c-4585-9121-ab17dd84ff4e_1400x497.png 848w, /__u/substackcdn.com/image/fetch/$s_!ryAb!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6de498a8-318c-4585-9121-ab17dd84ff4e_1400x497.png 1272w, /__u/substackcdn.com/image/fetch/$s_!ryAb!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6de498a8-318c-4585-9121-ab17dd84ff4e_1400x497.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!ryAb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6de498a8-318c-4585-9121-ab17dd84ff4e_1400x497.png" width="1400" height="497" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6de498a8-318c-4585-9121-ab17dd84ff4e_1400x497.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:497,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!ryAb!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6de498a8-318c-4585-9121-ab17dd84ff4e_1400x497.png 424w, /__u/substackcdn.com/image/fetch/$s_!ryAb!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6de498a8-318c-4585-9121-ab17dd84ff4e_1400x497.png 848w, /__u/substackcdn.com/image/fetch/$s_!ryAb!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6de498a8-318c-4585-9121-ab17dd84ff4e_1400x497.png 1272w, /__u/substackcdn.com/image/fetch/$s_!ryAb!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6de498a8-318c-4585-9121-ab17dd84ff4e_1400x497.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>So basically, we&#8217;re talking about prompt engineering &#8212; which forms the foundation of any framework.</p><p>The framework usually helps in two ways: it structures the prompt properly to make sure the LLM responds in the right format, and then parses the response to route it to the correct tool &#8212; or API, document or what have you.</p><p>When we set up knowledge, a framework might help with chunking documents, embedding them and storing them. This gets added to the prompt as context, similar to how we build standard RAG systems.</p><p>A framework can also help with things like error handling, structured outputs, validation, observability, deployment &#8212; and generally help you organize your code so you can build more complex systems, like multi-agent setups.</p><p>Still, a lot of people feel that using a full framework is overkill.</p><p>The issue is: if the LLM doesn&#8217;t use the tool correctly or something breaks, the abstraction becomes a pain because you can&#8217;t debug it easily. This can also be a problem if you switch models &#8212; the system prompt might have been tailored for one and not transfer well to others.</p><p>That&#8217;s why some developers end up rewriting parts of a framework &#8212;such as <code>create_react_agent</code> in LangGraph &#8212; to get better control.</p><p>Some frameworks are lighter, some heavier and offer additional features, but there&#8217;s community around them to help you get started. And once you learn one (including how it works under the hood), it becomes easier to pick up others.</p><h3>Different open source frameworks</h3><p>We do look to the community to understand how well a framework performs in real cases. However, the most popular frameworks may not always be the ideal choice.</p><p>The ones we&#8217;ve all heard about are <strong>CrewAI </strong>and <strong>AutoGen</strong>.</p><p><strong>CrewAI</strong> is a very high-abstraction framework that lets you build agent systems quickly by hiding low-level details. <strong>AutoGen</strong> focuses on autonomous, asynchronous agent collaboration, where agents have the freedom to collaborate as they see fit &#8212; which may make it more suited for testing and research.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!2TiU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14d4ab39-a152-4de1-959f-8181e5b3060f_1400x457.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!2TiU!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14d4ab39-a152-4de1-959f-8181e5b3060f_1400x457.png 424w, /__u/substackcdn.com/image/fetch/$s_!2TiU!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14d4ab39-a152-4de1-959f-8181e5b3060f_1400x457.png 848w, /__u/substackcdn.com/image/fetch/$s_!2TiU!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14d4ab39-a152-4de1-959f-8181e5b3060f_1400x457.png 1272w, /__u/substackcdn.com/image/fetch/$s_!2TiU!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14d4ab39-a152-4de1-959f-8181e5b3060f_1400x457.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!2TiU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14d4ab39-a152-4de1-959f-8181e5b3060f_1400x457.png" width="1400" height="457" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/14d4ab39-a152-4de1-959f-8181e5b3060f_1400x457.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:457,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!2TiU!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14d4ab39-a152-4de1-959f-8181e5b3060f_1400x457.png 424w, /__u/substackcdn.com/image/fetch/$s_!2TiU!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14d4ab39-a152-4de1-959f-8181e5b3060f_1400x457.png 848w, /__u/substackcdn.com/image/fetch/$s_!2TiU!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14d4ab39-a152-4de1-959f-8181e5b3060f_1400x457.png 1272w, /__u/substackcdn.com/image/fetch/$s_!2TiU!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14d4ab39-a152-4de1-959f-8181e5b3060f_1400x457.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><strong>LangGraph</strong> is still a fairly well-known system but deserves to be highlighted as one of the main frameworks for developers. It uses a graph-based approach where you build nodes and connect them via agents. Compared to the other two, it gives you stricter engineering control over workflows and doesn&#8217;t assume agents should have much agency.</p><p>It should be noted that many feel that LangGraph is overly complicated in its abstractions and difficult to debug. The idea is that it has a steep learning curve but once you learn the fundamentals it should get easier.</p><p>Now, there are a few newer frameworks I also want to include here.</p><p>The next one is <strong>Agno</strong> (previously Phi-Data) which focuses on providing a very good developer experience. It also has one of the cleanest documentations I&#8217;ve seen. It&#8217;s very plug-and-play, helping you get started quickly with a lot of built-in features, organized into logical, clean abstractions that make sense.</p><p><strong>SmolAgents</strong> is a very bare-bones framework that introduces an agent &#8212; CodingAgent &#8212; which routes data via code rather than JSON. It also gives you direct access to the entire Hugging Face model library out of the box.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!KsCL!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F249c6dcb-8279-4221-a2f9-a4170bc31f33_1400x587.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!KsCL!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F249c6dcb-8279-4221-a2f9-a4170bc31f33_1400x587.png 424w, /__u/substackcdn.com/image/fetch/$s_!KsCL!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F249c6dcb-8279-4221-a2f9-a4170bc31f33_1400x587.png 848w, /__u/substackcdn.com/image/fetch/$s_!KsCL!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F249c6dcb-8279-4221-a2f9-a4170bc31f33_1400x587.png 1272w, /__u/substackcdn.com/image/fetch/$s_!KsCL!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F249c6dcb-8279-4221-a2f9-a4170bc31f33_1400x587.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!KsCL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F249c6dcb-8279-4221-a2f9-a4170bc31f33_1400x587.png" width="1400" height="587" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/249c6dcb-8279-4221-a2f9-a4170bc31f33_1400x587.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:587,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!KsCL!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F249c6dcb-8279-4221-a2f9-a4170bc31f33_1400x587.png 424w, /__u/substackcdn.com/image/fetch/$s_!KsCL!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F249c6dcb-8279-4221-a2f9-a4170bc31f33_1400x587.png 848w, /__u/substackcdn.com/image/fetch/$s_!KsCL!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F249c6dcb-8279-4221-a2f9-a4170bc31f33_1400x587.png 1272w, /__u/substackcdn.com/image/fetch/$s_!KsCL!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F249c6dcb-8279-4221-a2f9-a4170bc31f33_1400x587.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>As for the open-source frameworks that aren&#8217;t as often mentioned:</p><p><strong>PydanticAI</strong> builds on Pydantic with minimal abstraction, offering a bare-bones framework that&#8217;s highly transparent. It&#8217;s great when you need strict type safety and predictable, validated outputs, for fine-grained control, making it easier to debug.</p><p><strong>Atomic Agents</strong> is developed by an individual agent builder and uses schema-driven building blocks you connect like Lego, with a strong focus on structure and control. It was built in response to the lack of alternatives that worked well in practice.</p><p>Both of PydanticAI&#8217;s and Atomic Agent&#8217;s goal is to move away from black-box AI that acts independently.</p><p><strong>Mastra</strong>, created by the team behind Gatsby, is a JavaScript framework built for frontend developers to easily build agents within their own ecosystem.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!bGTq!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac6ce3bb-a1ea-4e86-9ffe-38808b7a3f4c_1400x506.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!bGTq!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac6ce3bb-a1ea-4e86-9ffe-38808b7a3f4c_1400x506.png 424w, /__u/substackcdn.com/image/fetch/$s_!bGTq!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac6ce3bb-a1ea-4e86-9ffe-38808b7a3f4c_1400x506.png 848w, /__u/substackcdn.com/image/fetch/$s_!bGTq!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac6ce3bb-a1ea-4e86-9ffe-38808b7a3f4c_1400x506.png 1272w, /__u/substackcdn.com/image/fetch/$s_!bGTq!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac6ce3bb-a1ea-4e86-9ffe-38808b7a3f4c_1400x506.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!bGTq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac6ce3bb-a1ea-4e86-9ffe-38808b7a3f4c_1400x506.png" width="1400" height="506" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ac6ce3bb-a1ea-4e86-9ffe-38808b7a3f4c_1400x506.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:506,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!bGTq!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac6ce3bb-a1ea-4e86-9ffe-38808b7a3f4c_1400x506.png 424w, /__u/substackcdn.com/image/fetch/$s_!bGTq!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac6ce3bb-a1ea-4e86-9ffe-38808b7a3f4c_1400x506.png 848w, /__u/substackcdn.com/image/fetch/$s_!bGTq!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac6ce3bb-a1ea-4e86-9ffe-38808b7a3f4c_1400x506.png 1272w, /__u/substackcdn.com/image/fetch/$s_!bGTq!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac6ce3bb-a1ea-4e86-9ffe-38808b7a3f4c_1400x506.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>We&#8217;ll go through what each of these have, and what makes them different.</p><h3>What they all have</h3><p>Most frameworks come with the same core building blocks: support for different models, tools, memory, and RAG.</p><p><strong>Most open-source frameworks are more or less model agnostic</strong>. This means they&#8217;re built to support various providers. However, as mentioned earlier, each framework has its own structure for system prompts &#8212; and that structure may work better with some models than others.</p><p>That&#8217;s also why you ideally want access to the system prompt and the ability to tweak it if needed.</p><p><strong>All agentic frameworks support tooling</strong>, since tools are essential for building systems that can act. They also make it easy to define your own custom tools through simple abstractions. Today, most frameworks support MCP, either officially or through community solutions.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!9sJ5!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2f8f13e-b651-4e4e-b5e2-53ed6170907a_1400x471.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!9sJ5!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2f8f13e-b651-4e4e-b5e2-53ed6170907a_1400x471.png 424w, /__u/substackcdn.com/image/fetch/$s_!9sJ5!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2f8f13e-b651-4e4e-b5e2-53ed6170907a_1400x471.png 848w, /__u/substackcdn.com/image/fetch/$s_!9sJ5!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2f8f13e-b651-4e4e-b5e2-53ed6170907a_1400x471.png 1272w, /__u/substackcdn.com/image/fetch/$s_!9sJ5!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2f8f13e-b651-4e4e-b5e2-53ed6170907a_1400x471.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!9sJ5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2f8f13e-b651-4e4e-b5e2-53ed6170907a_1400x471.png" width="1400" height="471" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d2f8f13e-b651-4e4e-b5e2-53ed6170907a_1400x471.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:471,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!9sJ5!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2f8f13e-b651-4e4e-b5e2-53ed6170907a_1400x471.png 424w, /__u/substackcdn.com/image/fetch/$s_!9sJ5!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2f8f13e-b651-4e4e-b5e2-53ed6170907a_1400x471.png 848w, /__u/substackcdn.com/image/fetch/$s_!9sJ5!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2f8f13e-b651-4e4e-b5e2-53ed6170907a_1400x471.png 1272w, /__u/substackcdn.com/image/fetch/$s_!9sJ5!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2f8f13e-b651-4e4e-b5e2-53ed6170907a_1400x471.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>It&#8217;s important to understand that not all models are built for function calling, which is necessary for using tools. To figure out which models are best suited as the base LLM, you can check <strong>Hugging Face&#8217;s agent <a href="https://huggingface.co/blog/pratikbhavsar/agent-leaderboard">leaderboard</a>.</strong></p><p>To enable agents to retain short-term memory between LLM calls, <strong>all frameworks make use of state</strong>. State helps the LLM remember what was said in earlier steps or parts of the conversation.</p><p>Most frameworks also offer <strong>simple options to set up RAG</strong> with different databases to provide the agent with knowledge.</p><p>Finally, nearly all frameworks support <strong>asynchronous calls, structured outputs, streaming, and the ability to add observability.</strong></p><h3>What some don&#8217;t have</h3><p>Frameworks will differ in some areas, such as supporting multimodal input, memory, and multi-agent systems. Some handle it for you, while others leave the wiring to you.</p><p>First, some frameworks have <strong>built-in solutions for handling multimodality</strong> &#8212; i.e. text, image, and voice. It&#8217;s all possible to implement this yourself, as long as the model supports it.</p><p>As said previously, short-term memory (state) is always included &#8212; without it, you can&#8217;t build a system that uses tools. However, l<strong>ong-term memory is trickier to implement</strong>, and this is where frameworks differ. Some offer built-in solutions, while others you&#8217;ll have to connect other solutions on your own.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!NFNu!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5fd0d2e-ce82-4d4d-9190-c3d72f042599_1400x731.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!NFNu!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5fd0d2e-ce82-4d4d-9190-c3d72f042599_1400x731.png 424w, /__u/substackcdn.com/image/fetch/$s_!NFNu!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5fd0d2e-ce82-4d4d-9190-c3d72f042599_1400x731.png 848w, /__u/substackcdn.com/image/fetch/$s_!NFNu!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5fd0d2e-ce82-4d4d-9190-c3d72f042599_1400x731.png 1272w, /__u/substackcdn.com/image/fetch/$s_!NFNu!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5fd0d2e-ce82-4d4d-9190-c3d72f042599_1400x731.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!NFNu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5fd0d2e-ce82-4d4d-9190-c3d72f042599_1400x731.png" width="1400" height="731" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a5fd0d2e-ce82-4d4d-9190-c3d72f042599_1400x731.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:731,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!NFNu!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5fd0d2e-ce82-4d4d-9190-c3d72f042599_1400x731.png 424w, /__u/substackcdn.com/image/fetch/$s_!NFNu!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5fd0d2e-ce82-4d4d-9190-c3d72f042599_1400x731.png 848w, /__u/substackcdn.com/image/fetch/$s_!NFNu!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5fd0d2e-ce82-4d4d-9190-c3d72f042599_1400x731.png 1272w, /__u/substackcdn.com/image/fetch/$s_!NFNu!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa5fd0d2e-ce82-4d4d-9190-c3d72f042599_1400x731.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><strong>Frameworks also vary in how they handle multi-agent capabilities.</strong> Multi-agent systems allow you to build collaborative or hierarchical setups with teams of agents connected via supervisors.</p><p>Most frameworks recommend keeping agents focused &#8212; a narrow scope with a limited set of tools. That means you&#8217;ll likely need to build out teams of agents to handle complex workflows. All frameworks let you build one team, but some get complicated when scaling into multi-hierarchical systems with multiple layers.</p><p><strong>This is where LangGraph stands out </strong>&#8212; you can build out nodes, connect them to various supervisors, and visualize how different teams interact. <strong>It&#8217;s clearly the most flexible when building multi-agent systems at scale.</strong></p><p><strong>Agno recently added support for teams</strong>, both collaborative and hierarchical, but there aren&#8217;t many examples yet for more complex, multi-hierarchical setups.</p><p><strong>SmolAgents lets you connect agents to a supervisor but can get complex as the system grows.</strong> It reminds me of CrewAI in how it structures agent teams. Mastra is similar in that sense.</p><p>With PydanticAI and Atomic Agents, you&#8217;ll need to manually chain your agent teams, so orchestration falls on you.</p><p>I have gathered a lot of the research I have done in this google <a href="https://docs.google.com/spreadsheets/d/1zjcww1w0vARZz9Z6GDxNMp-PKyg7iRyNYAnDo59HjzI/edit?usp=sharing">sheet</a>, you&#8217;ll also find a table in this <a href="https://github.com/ilsilfverskiold/Awesome-LLM-Resources-List/blob/main/README.md#agentic-frameworks-core-capabilities">repository</a> if you need a better overview.</p><h3>How they&#8217;re all different</h3><p>Frameworks differ in how much they abstract away, how much control they give agents, and how much coding you&#8217;ll need to do to get something working.</p><p>First, some frameworks make it a point to include a lot of built&#8209;in features, making it easy to get started quickly.</p><p>I&#8217;d say Mastra, CrewAI, and to some extent Agno are built to be plug and play.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!DFQG!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F151f1156-1616-4e3a-a4fa-af635117ac8f_1400x625.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!DFQG!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F151f1156-1616-4e3a-a4fa-af635117ac8f_1400x625.png 424w, /__u/substackcdn.com/image/fetch/$s_!DFQG!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F151f1156-1616-4e3a-a4fa-af635117ac8f_1400x625.png 848w, /__u/substackcdn.com/image/fetch/$s_!DFQG!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F151f1156-1616-4e3a-a4fa-af635117ac8f_1400x625.png 1272w, /__u/substackcdn.com/image/fetch/$s_!DFQG!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F151f1156-1616-4e3a-a4fa-af635117ac8f_1400x625.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!DFQG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F151f1156-1616-4e3a-a4fa-af635117ac8f_1400x625.png" width="1400" height="625" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/151f1156-1616-4e3a-a4fa-af635117ac8f_1400x625.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:625,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!DFQG!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F151f1156-1616-4e3a-a4fa-af635117ac8f_1400x625.png 424w, /__u/substackcdn.com/image/fetch/$s_!DFQG!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F151f1156-1616-4e3a-a4fa-af635117ac8f_1400x625.png 848w, /__u/substackcdn.com/image/fetch/$s_!DFQG!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F151f1156-1616-4e3a-a4fa-af635117ac8f_1400x625.png 1272w, /__u/substackcdn.com/image/fetch/$s_!DFQG!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F151f1156-1616-4e3a-a4fa-af635117ac8f_1400x625.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><strong>LangGraph</strong> also has a good amount of abstraction, but it uses a graph-based system where you manually connect nodes. That gives you more control but also means you have to set up and manage every connection yourself, which comes with a <strong>steeper learning curve.</strong></p><p>Then we have the low-level abstraction frameworks like PydanticAI, SmolAgents, and Atomic Agents.</p><p><strong>These make it a point to be transparent, but you often have to build out the orchestration yourself.</strong> This gives you full control and helps with debugging &#8212; but it also increases time to build.</p><p>Another point of difference is how much agency the framework assumes the agent should have. <strong>Some are built on the idea that LLMs should be smart enough to figure out how to complete the task on their own.</strong> Others lean toward tight control &#8212; giving agents one job and guiding them step by step.</p><p><strong>AutoGen and SmolAgents fall into the first camp. The rest lean more towards control.</strong></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!M7AU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa941d12e-1e3c-46b6-aee5-69f23888c025_1400x613.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!M7AU!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa941d12e-1e3c-46b6-aee5-69f23888c025_1400x613.png 424w, /__u/substackcdn.com/image/fetch/$s_!M7AU!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa941d12e-1e3c-46b6-aee5-69f23888c025_1400x613.png 848w, /__u/substackcdn.com/image/fetch/$s_!M7AU!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa941d12e-1e3c-46b6-aee5-69f23888c025_1400x613.png 1272w, /__u/substackcdn.com/image/fetch/$s_!M7AU!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa941d12e-1e3c-46b6-aee5-69f23888c025_1400x613.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!M7AU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa941d12e-1e3c-46b6-aee5-69f23888c025_1400x613.png" width="1400" height="613" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a941d12e-1e3c-46b6-aee5-69f23888c025_1400x613.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:613,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!M7AU!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa941d12e-1e3c-46b6-aee5-69f23888c025_1400x613.png 424w, /__u/substackcdn.com/image/fetch/$s_!M7AU!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa941d12e-1e3c-46b6-aee5-69f23888c025_1400x613.png 848w, /__u/substackcdn.com/image/fetch/$s_!M7AU!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa941d12e-1e3c-46b6-aee5-69f23888c025_1400x613.png 1272w, /__u/substackcdn.com/image/fetch/$s_!M7AU!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa941d12e-1e3c-46b6-aee5-69f23888c025_1400x613.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>There&#8217;s something to consider here: when developers build frameworks that focus on tight control, it&#8217;s often because they haven&#8217;t found a way to let agents work on their own yet &#8212; at least not reliably.</p><p>This space is also starting to look more and more like engineering.</p><p>If you&#8217;re going to build these systems, you do need to understand how to code. The real question is how much the frameworks differ in terms of how technical you need to be.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!jBCD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7afc5d54-82e2-4f69-937e-24ed75d00910_1400x579.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!jBCD!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7afc5d54-82e2-4f69-937e-24ed75d00910_1400x579.png 424w, /__u/substackcdn.com/image/fetch/$s_!jBCD!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7afc5d54-82e2-4f69-937e-24ed75d00910_1400x579.png 848w, /__u/substackcdn.com/image/fetch/$s_!jBCD!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7afc5d54-82e2-4f69-937e-24ed75d00910_1400x579.png 1272w, /__u/substackcdn.com/image/fetch/$s_!jBCD!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7afc5d54-82e2-4f69-937e-24ed75d00910_1400x579.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!jBCD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7afc5d54-82e2-4f69-937e-24ed75d00910_1400x579.png" width="1400" height="579" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7afc5d54-82e2-4f69-937e-24ed75d00910_1400x579.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:579,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!jBCD!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7afc5d54-82e2-4f69-937e-24ed75d00910_1400x579.png 424w, /__u/substackcdn.com/image/fetch/$s_!jBCD!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7afc5d54-82e2-4f69-937e-24ed75d00910_1400x579.png 848w, /__u/substackcdn.com/image/fetch/$s_!jBCD!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7afc5d54-82e2-4f69-937e-24ed75d00910_1400x579.png 1272w, /__u/substackcdn.com/image/fetch/$s_!jBCD!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7afc5d54-82e2-4f69-937e-24ed75d00910_1400x579.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>If you&#8217;re less experienced, going with CrewAI, Agno, or Mastra might be a good idea.</p><p>SmolAgents is also pretty straightforward for simple use cases.</p><p>As for PydanticAI, Atomic Agents, and LangGraph &#8212; you&#8217;ll be writing a lot more of the logic yourself. Though to be fair, it&#8217;s always possible to build an agent to help you structure your code correctly as well.</p><p>If you&#8217;re completely new to programming then you can check out Flowise or Dify.</p><p>Lastly, it&#8217;s worth mentioning <strong>the developer experience across these frameworks.</strong></p><p>From what I&#8217;ve seen, most developers find CrewAI and AutoGen tricky to debug. SmolAgents&#8217; CodeAgent introduces a novel approach where agents output code to route data &#8212; a cool idea, but it doesn&#8217;t always work as intended.</p><p>LangGraph, especially when paired with LangChain, comes with a steep learning curve and some confusing abstractions that you may end up having to break apart and rebuild.</p><p>PydanticAI and Atomic Agents are generally liked by developers, but they do require you to build the orchestration yourself.</p><p>Agno and Mastra are solid choices, but you might run into issues like looping calls that can be hard to debug.</p><h3>Some notes</h3><p>The best way to get started is just to jump in and try something out. But I hope this gave you a decent overview of the open-source frameworks out there &#8212; and what might work for you.</p><p>This was a fairly shallow look at each one though, as I haven&#8217;t gone into things like enterprise-grade scalability or operational robustness. You&#8217;ll need to research those parts separately if that&#8217;s what you&#8217;re building for.</p><p>Some devs say AI agent frameworks are some of the worst forms of abstraction &#8212; that they often make things more complicated than just using the official LLM provider&#8217;s SDK directly.</p><p>I&#8217;ll leave that one up to you to decide.</p><div><hr></div><p>Remember the full list of features for each of these frameworks you can find <a href="https://docs.google.com/spreadsheets/d/1zjcww1w0vARZz9Z6GDxNMp-PKyg7iRyNYAnDo59HjzI/edit?usp=sharing">here</a>, and the repository with the full list of agentic frameworks you can find <a href="https://github.com/ilsilfverskiold/Awesome-LLM-Resources-List/blob/main/README.md#os-agenticai-workflow">here</a>.</p><p>I hope you liked it, and if so be sure to like it, comment or share it.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://howtouseai.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading AI Practical Deep Dives. Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p></p>]]></content:encoded></item><item><title><![CDATA[Work with Computer Vision: Monitor Traffic in Norway]]></title><description><![CDATA[By fine-tuning pre-trained CNN and ViT models on public traffic cameras]]></description><link>https://howtouseai.substack.com/p/work-with-computer-vision-monitor</link><guid isPermaLink="false">https://howtouseai.substack.com/p/work-with-computer-vision-monitor</guid><dc:creator><![CDATA[Ida Silfverskiold]]></dc:creator><pubDate>Wed, 24 Sep 2025 09:58:35 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!7Ady!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdc45c73-d889-4a4d-97d4-716b5de2ced2_1400x650.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!7Ady!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdc45c73-d889-4a4d-97d4-716b5de2ced2_1400x650.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!7Ady!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdc45c73-d889-4a4d-97d4-716b5de2ced2_1400x650.png 424w, /__u/substackcdn.com/image/fetch/$s_!7Ady!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdc45c73-d889-4a4d-97d4-716b5de2ced2_1400x650.png 848w, /__u/substackcdn.com/image/fetch/$s_!7Ady!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdc45c73-d889-4a4d-97d4-716b5de2ced2_1400x650.png 1272w, /__u/substackcdn.com/image/fetch/$s_!7Ady!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdc45c73-d889-4a4d-97d4-716b5de2ced2_1400x650.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!7Ady!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdc45c73-d889-4a4d-97d4-716b5de2ced2_1400x650.png" width="1400" height="650" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fdc45c73-d889-4a4d-97d4-716b5de2ced2_1400x650.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:650,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!7Ady!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdc45c73-d889-4a4d-97d4-716b5de2ced2_1400x650.png 424w, /__u/substackcdn.com/image/fetch/$s_!7Ady!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdc45c73-d889-4a4d-97d4-716b5de2ced2_1400x650.png 848w, /__u/substackcdn.com/image/fetch/$s_!7Ady!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdc45c73-d889-4a4d-97d4-716b5de2ced2_1400x650.png 1272w, /__u/substackcdn.com/image/fetch/$s_!7Ady!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffdc45c73-d889-4a4d-97d4-716b5de2ced2_1400x650.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><em>Note: this article is from May 2024 but the content still holds up well.</em></p><p>I haven&#8217;t ventured out of natural language processing much, but I have wanted to write about using computer vision for some time, specifically with a practical use case.</p><p>The task here is to <strong>classify traffic levels</strong> by public traffic cameras planted across the country in Norway. The <a href="https://www.vegvesen.no/trafikkinformasjon/reiseinformasjon/webkamera/#/">cameras</a> are almost updated in real-time, while the traffic API that is also available is sometimes updated hours after the traffic levels have been calculated.</p><p>The idea is to automate a process that checks the pictures of the cameras every minute, so we can estimate the traffic levels well before the API catches on.</p><p>The model I will build in this article was originally created for a media outlet in Norway but has been open-sourced, and you can find it <a href="https://huggingface.co/ilsilfverskiold/traffic-levels-image-classification">here.</a> It has been built using images from fifty out of the hundreds of public traffic cameras in Norway.</p><p>You&#8217;ll see me demonstrate how to use it below.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!GjNC!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc5b19744-bf07-4449-97f2-7bf9779c76ec_800x386.gif" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!GjNC!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc5b19744-bf07-4449-97f2-7bf9779c76ec_800x386.gif 424w, /__u/substackcdn.com/image/fetch/$s_!GjNC!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc5b19744-bf07-4449-97f2-7bf9779c76ec_800x386.gif 848w, /__u/substackcdn.com/image/fetch/$s_!GjNC!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc5b19744-bf07-4449-97f2-7bf9779c76ec_800x386.gif 1272w, /__u/substackcdn.com/image/fetch/$s_!GjNC!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc5b19744-bf07-4449-97f2-7bf9779c76ec_800x386.gif 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!GjNC!,w_1456,c_limit,f_auto,q_auto:good,fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc5b19744-bf07-4449-97f2-7bf9779c76ec_800x386.gif" width="800" height="386" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c5b19744-bf07-4449-97f2-7bf9779c76ec_800x386.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:386,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!GjNC!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc5b19744-bf07-4449-97f2-7bf9779c76ec_800x386.gif 424w, /__u/substackcdn.com/image/fetch/$s_!GjNC!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc5b19744-bf07-4449-97f2-7bf9779c76ec_800x386.gif 848w, /__u/substackcdn.com/image/fetch/$s_!GjNC!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc5b19744-bf07-4449-97f2-7bf9779c76ec_800x386.gif 1272w, /__u/substackcdn.com/image/fetch/$s_!GjNC!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc5b19744-bf07-4449-97f2-7bf9779c76ec_800x386.gif 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Do grab a picture from <a href="https://www.vegvesen.no/trafikkinformasjon/reiseinformasjon/webkamera/#/vis/0229009_1">this</a> or <a href="https://www.vegvesen.no/trafikkinformasjon/reiseinformasjon/webkamera/#/vis/0329034_1">this</a> camera and try it out to see how it does via the model <a href="https://huggingface.co/ilsilfverskiold/traffic-levels-image-classification">page</a>*</p><p>For due diligence though, I did check other options such as Google Traffic, which does provide us with good data for urban areas. However, we weren&#8217;t allowed to tap into it and it had very little data on areas outside of the city limits so it wasn&#8217;t a viable solution.</p><blockquote><p>For Google Traffic, they estimate levels based on data that is shared by their user&#8217;s phones whereas we would do this manually, checking the public cameras for traffic levels with computer vision by estimating how many cars there are on the road or are waiting in line.</p></blockquote><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!NWmP!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F482c9032-ff9e-476a-bd85-53c753d81bfb_1326x1000.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!NWmP!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F482c9032-ff9e-476a-bd85-53c753d81bfb_1326x1000.png 424w, /__u/substackcdn.com/image/fetch/$s_!NWmP!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F482c9032-ff9e-476a-bd85-53c753d81bfb_1326x1000.png 848w, /__u/substackcdn.com/image/fetch/$s_!NWmP!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F482c9032-ff9e-476a-bd85-53c753d81bfb_1326x1000.png 1272w, /__u/substackcdn.com/image/fetch/$s_!NWmP!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F482c9032-ff9e-476a-bd85-53c753d81bfb_1326x1000.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!NWmP!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F482c9032-ff9e-476a-bd85-53c753d81bfb_1326x1000.png" width="1326" height="1000" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/482c9032-ff9e-476a-bd85-53c753d81bfb_1326x1000.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1000,&quot;width&quot;:1326,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!NWmP!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F482c9032-ff9e-476a-bd85-53c753d81bfb_1326x1000.png 424w, /__u/substackcdn.com/image/fetch/$s_!NWmP!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F482c9032-ff9e-476a-bd85-53c753d81bfb_1326x1000.png 848w, /__u/substackcdn.com/image/fetch/$s_!NWmP!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F482c9032-ff9e-476a-bd85-53c753d81bfb_1326x1000.png 1272w, /__u/substackcdn.com/image/fetch/$s_!NWmP!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F482c9032-ff9e-476a-bd85-53c753d81bfb_1326x1000.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>To understand the data we&#8217;re working with, do take a look at the dataset we&#8217;ll be using <a href="https://huggingface.co/datasets/ilsilfverskiold/traffic-camera-norway-images">here</a>.</p><p>As you can see, the images require us to interpret not just the amount of cars but whether they are waiting in line in various directions, in various different sceneries.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!Ygz8!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02dd3803-a8af-4d31-bd06-ddea7576965f_1400x849.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!Ygz8!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02dd3803-a8af-4d31-bd06-ddea7576965f_1400x849.png 424w, /__u/substackcdn.com/image/fetch/$s_!Ygz8!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02dd3803-a8af-4d31-bd06-ddea7576965f_1400x849.png 848w, /__u/substackcdn.com/image/fetch/$s_!Ygz8!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02dd3803-a8af-4d31-bd06-ddea7576965f_1400x849.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Ygz8!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02dd3803-a8af-4d31-bd06-ddea7576965f_1400x849.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!Ygz8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02dd3803-a8af-4d31-bd06-ddea7576965f_1400x849.png" width="1400" height="849" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/02dd3803-a8af-4d31-bd06-ddea7576965f_1400x849.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:849,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!Ygz8!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02dd3803-a8af-4d31-bd06-ddea7576965f_1400x849.png 424w, /__u/substackcdn.com/image/fetch/$s_!Ygz8!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02dd3803-a8af-4d31-bd06-ddea7576965f_1400x849.png 848w, /__u/substackcdn.com/image/fetch/$s_!Ygz8!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02dd3803-a8af-4d31-bd06-ddea7576965f_1400x849.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Ygz8!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02dd3803-a8af-4d31-bd06-ddea7576965f_1400x849.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>This article will go into the different architectures for the task &#8212; image classification &#8212; trying an older CNN model like ResNet and comparing it to newer models &#8212; ViT (Vision Transformer), Swin Transformer and ConvNEXT. I will also give a CLIP model a try in the introduction section.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!oPDr!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3f3cb848-9d7d-40f3-ad73-f6995ff0b285_1400x590.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!oPDr!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3f3cb848-9d7d-40f3-ad73-f6995ff0b285_1400x590.png 424w, /__u/substackcdn.com/image/fetch/$s_!oPDr!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3f3cb848-9d7d-40f3-ad73-f6995ff0b285_1400x590.png 848w, /__u/substackcdn.com/image/fetch/$s_!oPDr!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3f3cb848-9d7d-40f3-ad73-f6995ff0b285_1400x590.png 1272w, /__u/substackcdn.com/image/fetch/$s_!oPDr!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3f3cb848-9d7d-40f3-ad73-f6995ff0b285_1400x590.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!oPDr!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3f3cb848-9d7d-40f3-ad73-f6995ff0b285_1400x590.png" width="1400" height="590" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3f3cb848-9d7d-40f3-ad73-f6995ff0b285_1400x590.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:590,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!oPDr!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3f3cb848-9d7d-40f3-ad73-f6995ff0b285_1400x590.png 424w, /__u/substackcdn.com/image/fetch/$s_!oPDr!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3f3cb848-9d7d-40f3-ad73-f6995ff0b285_1400x590.png 848w, /__u/substackcdn.com/image/fetch/$s_!oPDr!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3f3cb848-9d7d-40f3-ad73-f6995ff0b285_1400x590.png 1272w, /__u/substackcdn.com/image/fetch/$s_!oPDr!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3f3cb848-9d7d-40f3-ad73-f6995ff0b285_1400x590.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>I have not tried edge cases for this model, which you should do before you push it into production.</p><p>I&#8217;ve shared most cook books I&#8217;ve used in a Github repository that you can find <a href="https://github.com/ilsilfverskiold/smaller-models-docs/tree/main/computer-vision/cook/image-classification">here</a>. These will help you prepare your image dataset to fine-tune for image classification, as well as how to train it with a ViT or a CNN model like ResNet.</p><p>I will go through the case and the results but feel free to scroll past the introduction if you want to go directly to the training section.</p><h2>Introduction</h2><h3>The Economics of Custom Models</h3><p>If your mind is spinning right now with the question on why I would bother to build my own vision model versus just using a CLIP model or GPT-4 Vision, I&#8217;ll go through it briefly.</p><p><strong>A CLIP model allows you to do zero-shot inference</strong>, which means you will provide it with the labels and it will do the best that it can with those labels to correctly estimate what is pictured in the image. For simpler cases, you should definitely go for a CLIP model. They are small enough that it won&#8217;t cost that much to host them.</p><p>Test out a ViT based CLIP model <a href="https://huggingface.co/openai/clip-vit-large-patch14">here</a> by providing a few labels and an image it should classify.</p><p>However, with a CLIP model I wouldn&#8217;t be able to train the model to understand the nuances between medium and high traffic, and in most cases a high traffic image would get classified as medium traffic. It very much depends on how the model interprets the images.</p><p>The CLIP model I linked to is also <strong>5 times larger than the custom trained model</strong> that does better at the same task.</p><p><strong>The same would happen if I used GPT-4 Vision;</strong> it wouldn&#8217;t always classify an image correctly that I would deem as high traffic. This is not accounting for cost, considering <strong>GPT-4 </strong>is vastly larger than a CLIP or a custom model.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!D1Rw!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14c272dc-4a19-46c4-b2ae-7c711471a728_1394x878.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!D1Rw!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14c272dc-4a19-46c4-b2ae-7c711471a728_1394x878.png 424w, /__u/substackcdn.com/image/fetch/$s_!D1Rw!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14c272dc-4a19-46c4-b2ae-7c711471a728_1394x878.png 848w, /__u/substackcdn.com/image/fetch/$s_!D1Rw!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14c272dc-4a19-46c4-b2ae-7c711471a728_1394x878.png 1272w, /__u/substackcdn.com/image/fetch/$s_!D1Rw!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14c272dc-4a19-46c4-b2ae-7c711471a728_1394x878.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!D1Rw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14c272dc-4a19-46c4-b2ae-7c711471a728_1394x878.png" width="1394" height="878" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/14c272dc-4a19-46c4-b2ae-7c711471a728_1394x878.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:878,&quot;width&quot;:1394,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!D1Rw!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14c272dc-4a19-46c4-b2ae-7c711471a728_1394x878.png 424w, /__u/substackcdn.com/image/fetch/$s_!D1Rw!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14c272dc-4a19-46c4-b2ae-7c711471a728_1394x878.png 848w, /__u/substackcdn.com/image/fetch/$s_!D1Rw!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14c272dc-4a19-46c4-b2ae-7c711471a728_1394x878.png 1272w, /__u/substackcdn.com/image/fetch/$s_!D1Rw!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14c272dc-4a19-46c4-b2ae-7c711471a728_1394x878.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>If your first idea is to go for GPT-4 Vision, let&#8217;s first estimate the cost day to day if we were to monitor 50 to 150 cameras every minute, so you get an idea of the economics of using such a large model.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!lhnE!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1653606e-be34-48ab-8864-44a20abfc440_1254x736.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!lhnE!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1653606e-be34-48ab-8864-44a20abfc440_1254x736.png 424w, /__u/substackcdn.com/image/fetch/$s_!lhnE!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1653606e-be34-48ab-8864-44a20abfc440_1254x736.png 848w, /__u/substackcdn.com/image/fetch/$s_!lhnE!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1653606e-be34-48ab-8864-44a20abfc440_1254x736.png 1272w, /__u/substackcdn.com/image/fetch/$s_!lhnE!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1653606e-be34-48ab-8864-44a20abfc440_1254x736.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!lhnE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1653606e-be34-48ab-8864-44a20abfc440_1254x736.png" width="1254" height="736" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1653606e-be34-48ab-8864-44a20abfc440_1254x736.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:736,&quot;width&quot;:1254,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!lhnE!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1653606e-be34-48ab-8864-44a20abfc440_1254x736.png 424w, /__u/substackcdn.com/image/fetch/$s_!lhnE!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1653606e-be34-48ab-8864-44a20abfc440_1254x736.png 848w, /__u/substackcdn.com/image/fetch/$s_!lhnE!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1653606e-be34-48ab-8864-44a20abfc440_1254x736.png 1272w, /__u/substackcdn.com/image/fetch/$s_!lhnE!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1653606e-be34-48ab-8864-44a20abfc440_1254x736.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>It seems dumb to visualize it, as most would never use these bigger models at such a high frequency <strong>but sometimes the economics of it isn&#8217;t taken into account.</strong> You need to consider the resources you are using and how to optimize those resources for the task at hand.</p><p>Let&#8217;s also look at the cost difference for hosting a CLIP at 428M parameters, or this custom built model at 85M parameters. The cost difference here is less, so in most cases it makes sense to use a CLIP.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!_IpJ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcdcf0ff0-d0ba-4393-9e15-bbc399b7694b_1400x695.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!_IpJ!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcdcf0ff0-d0ba-4393-9e15-bbc399b7694b_1400x695.png 424w, /__u/substackcdn.com/image/fetch/$s_!_IpJ!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcdcf0ff0-d0ba-4393-9e15-bbc399b7694b_1400x695.png 848w, /__u/substackcdn.com/image/fetch/$s_!_IpJ!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcdcf0ff0-d0ba-4393-9e15-bbc399b7694b_1400x695.png 1272w, /__u/substackcdn.com/image/fetch/$s_!_IpJ!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcdcf0ff0-d0ba-4393-9e15-bbc399b7694b_1400x695.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!_IpJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcdcf0ff0-d0ba-4393-9e15-bbc399b7694b_1400x695.png" width="1400" height="695" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/cdcf0ff0-d0ba-4393-9e15-bbc399b7694b_1400x695.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:695,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!_IpJ!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcdcf0ff0-d0ba-4393-9e15-bbc399b7694b_1400x695.png 424w, /__u/substackcdn.com/image/fetch/$s_!_IpJ!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcdcf0ff0-d0ba-4393-9e15-bbc399b7694b_1400x695.png 848w, /__u/substackcdn.com/image/fetch/$s_!_IpJ!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcdcf0ff0-d0ba-4393-9e15-bbc399b7694b_1400x695.png 1272w, /__u/substackcdn.com/image/fetch/$s_!_IpJ!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fcdcf0ff0-d0ba-4393-9e15-bbc399b7694b_1400x695.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3>Computer Vision</h3><p>Computer vision is a subset of AI that trains computers to interpret and understand what we can &#8220;see.&#8221; Just as with natural language processing, we sort different areas into tasks, where we can choose different models and architectures to work with the task.</p><p>The most popular tasks within vision is image classification, object detection and segmentation.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!UIiU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2f5cf1c-ac2a-48d5-8290-3906fde8753c_1400x613.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!UIiU!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2f5cf1c-ac2a-48d5-8290-3906fde8753c_1400x613.png 424w, /__u/substackcdn.com/image/fetch/$s_!UIiU!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2f5cf1c-ac2a-48d5-8290-3906fde8753c_1400x613.png 848w, /__u/substackcdn.com/image/fetch/$s_!UIiU!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2f5cf1c-ac2a-48d5-8290-3906fde8753c_1400x613.png 1272w, /__u/substackcdn.com/image/fetch/$s_!UIiU!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2f5cf1c-ac2a-48d5-8290-3906fde8753c_1400x613.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!UIiU!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2f5cf1c-ac2a-48d5-8290-3906fde8753c_1400x613.png" width="1400" height="613" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d2f5cf1c-ac2a-48d5-8290-3906fde8753c_1400x613.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:613,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!UIiU!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2f5cf1c-ac2a-48d5-8290-3906fde8753c_1400x613.png 424w, /__u/substackcdn.com/image/fetch/$s_!UIiU!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2f5cf1c-ac2a-48d5-8290-3906fde8753c_1400x613.png 848w, /__u/substackcdn.com/image/fetch/$s_!UIiU!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2f5cf1c-ac2a-48d5-8290-3906fde8753c_1400x613.png 1272w, /__u/substackcdn.com/image/fetch/$s_!UIiU!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd2f5cf1c-ac2a-48d5-8290-3906fde8753c_1400x613.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The task I&#8217;m working with here is image classification, where I want a model to assign a label or a class to an image. If you&#8217;ve worked with text classification, it follows the same principles.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!yp_L!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf9cfc30-9d3f-405d-a32f-1a063b52b98e_1400x681.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!yp_L!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf9cfc30-9d3f-405d-a32f-1a063b52b98e_1400x681.png 424w, /__u/substackcdn.com/image/fetch/$s_!yp_L!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf9cfc30-9d3f-405d-a32f-1a063b52b98e_1400x681.png 848w, /__u/substackcdn.com/image/fetch/$s_!yp_L!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf9cfc30-9d3f-405d-a32f-1a063b52b98e_1400x681.png 1272w, /__u/substackcdn.com/image/fetch/$s_!yp_L!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf9cfc30-9d3f-405d-a32f-1a063b52b98e_1400x681.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!yp_L!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf9cfc30-9d3f-405d-a32f-1a063b52b98e_1400x681.png" width="1400" height="681" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/af9cfc30-9d3f-405d-a32f-1a063b52b98e_1400x681.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:681,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!yp_L!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf9cfc30-9d3f-405d-a32f-1a063b52b98e_1400x681.png 424w, /__u/substackcdn.com/image/fetch/$s_!yp_L!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf9cfc30-9d3f-405d-a32f-1a063b52b98e_1400x681.png 848w, /__u/substackcdn.com/image/fetch/$s_!yp_L!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf9cfc30-9d3f-405d-a32f-1a063b52b98e_1400x681.png 1272w, /__u/substackcdn.com/image/fetch/$s_!yp_L!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf9cfc30-9d3f-405d-a32f-1a063b52b98e_1400x681.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>So for image classification, as with other tasks, you have several pre-trained models to choose from. So which one should you pick? Vision Transformers are fairly new and if you Google a bit you&#8217;ll find that most have been working with Convolutional Neural Networks (CNNs) such as ResNet.</p><p>The idea is that <strong>ViTs should be good at capturing global context</strong> and dependencies between distant parts of an image because of their self-attention mechanisms. However, the catch is that <strong>ViTs need a lot of data to perform</strong>. <strong>CNNs </strong>on the other hand, should be <strong>more efficient on smaller models and datasets.</strong> CNNs also have a proven track record.</p><blockquote><p><em>A ViT will need a lot of data to perform, but it is a bit unclear if it is enough that the pre-trained model has been trained on enough data or if it needs a hefty amount for fine-tuning as well. If you listen to some people, it&#8217;s enough if it has been pre-trained on enough data.</em></p></blockquote><p>The ideal thing is to test the different models, using a CNN &#8212; like ResNet &#8212; and a ViT model along with the newer models such as ConvNEXT and Swin Transformer on your dataset to see which does better.</p><p>I have done exactly this below, and you&#8217;ll see the metrics I achieved for each model.</p><p>If you are muddling through the same process, you&#8217;ll find cook books <a href="https://github.com/ilsilfverskiold/smaller-models-docs/tree/main/computer-vision/cook/image-classification">here</a> to fine-tune for all different models.</p><h3>The Use Case: Estimating Traffic Levels</h3><p>Like I mentioned before, the use case we&#8217;re working with here is to <strong>classify the level of traffic</strong> from images from public road cameras in Norway.</p><p>These cameras are accessible to the public. You can <strong><a href="https://www.vegvesen.no/om-oss/presse/foto-film/">freely download</a></strong> and use any photos and illustrations from the Norwegian Public Roads Administration.</p><p>I set up a script that would fetch images for certain times during the day and then I collected them into a folder that I later speed sorted whenever I had a few hours available.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!xnaB!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fedb2b891-75e4-4ae9-8a78-005328c72834_1400x712.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!xnaB!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fedb2b891-75e4-4ae9-8a78-005328c72834_1400x712.png 424w, /__u/substackcdn.com/image/fetch/$s_!xnaB!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fedb2b891-75e4-4ae9-8a78-005328c72834_1400x712.png 848w, /__u/substackcdn.com/image/fetch/$s_!xnaB!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fedb2b891-75e4-4ae9-8a78-005328c72834_1400x712.png 1272w, /__u/substackcdn.com/image/fetch/$s_!xnaB!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fedb2b891-75e4-4ae9-8a78-005328c72834_1400x712.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!xnaB!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fedb2b891-75e4-4ae9-8a78-005328c72834_1400x712.png" width="1400" height="712" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/edb2b891-75e4-4ae9-8a78-005328c72834_1400x712.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:712,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!xnaB!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fedb2b891-75e4-4ae9-8a78-005328c72834_1400x712.png 424w, /__u/substackcdn.com/image/fetch/$s_!xnaB!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fedb2b891-75e4-4ae9-8a78-005328c72834_1400x712.png 848w, /__u/substackcdn.com/image/fetch/$s_!xnaB!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fedb2b891-75e4-4ae9-8a78-005328c72834_1400x712.png 1272w, /__u/substackcdn.com/image/fetch/$s_!xnaB!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fedb2b891-75e4-4ae9-8a78-005328c72834_1400x712.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>I did not have an unlimited amount of time, but <strong>in most cases you need more varied data to train with.</strong></p><p>The finished dataset I used you&#8217;ll find <a href="https://huggingface.co/datasets/ilsilfverskiold/traffic-camera-norway-images">here</a>.</p><p>The problem with these images is that<strong> some roads</strong> consistently <strong>experience high traffic whereas other roads don&#8217;t.</strong> This then means that we&#8217;ll get a very skewed dataset as it&#8217;s rare that the roads will be packed with cars during all hours of the day. As you&#8217;ll notice, we have <strong>4,200 images </strong>with<strong> low traffic</strong> and <strong>only</strong> <strong>800 images</strong> with<strong> high traffic.</strong></p><p>For this specific case, I <strong>did not </strong>need it to perform perfectly but I couldn&#8217;t have an image being classified as high traffic when it is clearly low traffic.</p><p>If it started generalizing it would be useless.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!TtGz!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbdeeb400-5cfe-4e46-9492-4d121e44ecfd_1400x611.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!TtGz!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbdeeb400-5cfe-4e46-9492-4d121e44ecfd_1400x611.png 424w, /__u/substackcdn.com/image/fetch/$s_!TtGz!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbdeeb400-5cfe-4e46-9492-4d121e44ecfd_1400x611.png 848w, /__u/substackcdn.com/image/fetch/$s_!TtGz!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbdeeb400-5cfe-4e46-9492-4d121e44ecfd_1400x611.png 1272w, /__u/substackcdn.com/image/fetch/$s_!TtGz!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbdeeb400-5cfe-4e46-9492-4d121e44ecfd_1400x611.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!TtGz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbdeeb400-5cfe-4e46-9492-4d121e44ecfd_1400x611.png" width="1400" height="611" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/bdeeb400-5cfe-4e46-9492-4d121e44ecfd_1400x611.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:611,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!TtGz!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbdeeb400-5cfe-4e46-9492-4d121e44ecfd_1400x611.png 424w, /__u/substackcdn.com/image/fetch/$s_!TtGz!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbdeeb400-5cfe-4e46-9492-4d121e44ecfd_1400x611.png 848w, /__u/substackcdn.com/image/fetch/$s_!TtGz!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbdeeb400-5cfe-4e46-9492-4d121e44ecfd_1400x611.png 1272w, /__u/substackcdn.com/image/fetch/$s_!TtGz!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbdeeb400-5cfe-4e46-9492-4d121e44ecfd_1400x611.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Nevertheless, if it sometimes <strong>classifies a high traffic image as medium traffic</strong> or a medium traffic image as a low traffic this would be <strong>less of a concern.</strong></p><p>I&#8217;ll go through the results from the training directly, and if you&#8217;re keen you can check how I trained the model at the next section.</p><p>To train the model, I tested several models using 5e-5 as a learning rate with 5 epochs. I didn&#8217;t find that much of an improvement by increasing the number of epochs.</p><p>Surprisingly, a pre-trained standard ViT model did well on only 6,800 images.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!oIVu!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff0125157-9650-4c63-ad33-68c39365d7bf_1400x754.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!oIVu!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff0125157-9650-4c63-ad33-68c39365d7bf_1400x754.png 424w, /__u/substackcdn.com/image/fetch/$s_!oIVu!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff0125157-9650-4c63-ad33-68c39365d7bf_1400x754.png 848w, /__u/substackcdn.com/image/fetch/$s_!oIVu!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff0125157-9650-4c63-ad33-68c39365d7bf_1400x754.png 1272w, /__u/substackcdn.com/image/fetch/$s_!oIVu!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff0125157-9650-4c63-ad33-68c39365d7bf_1400x754.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!oIVu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff0125157-9650-4c63-ad33-68c39365d7bf_1400x754.png" width="1400" height="754" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f0125157-9650-4c63-ad33-68c39365d7bf_1400x754.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:754,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!oIVu!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff0125157-9650-4c63-ad33-68c39365d7bf_1400x754.png 424w, /__u/substackcdn.com/image/fetch/$s_!oIVu!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff0125157-9650-4c63-ad33-68c39365d7bf_1400x754.png 848w, /__u/substackcdn.com/image/fetch/$s_!oIVu!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff0125157-9650-4c63-ad33-68c39365d7bf_1400x754.png 1272w, /__u/substackcdn.com/image/fetch/$s_!oIVu!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff0125157-9650-4c63-ad33-68c39365d7bf_1400x754.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The ViT model is three times as large as the ResNet I used, but still quite small at 85M parameters.</p><p>The metrics show us that a ViT performed better on all metrics. <strong>Accuracy</strong> is the one we&#8217;re looking at the most, whereas <strong>the F1</strong> will help us estimate <em>how much our skewed dataset is a problem.</em> A high F1 score means the model is not just guessing well for the common categories, but also doing a good job at correctly estimating the rare categories, such as medium and high traffic.</p><p>Furthermore, testing the model manually on new images, I found that the ResNet was more likely to classify a low traffic image as high traffic than a ViT, so this is why I primarily went with ViT along with the higher performance metrics.</p><p>The result could have primarily been because the ViT model was naturally larger but I needed a fairly large model for this case. I also wonder if a ViT is a better choice for these images where I need it to analyze the entirety of it to estimate traffic levels.</p><p>Continuing my experiment, I did not find that using a ConvNEXT or a Swin Transformer model gave me any positive change. They did surprisingly well though.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!a0PL!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff63936ba-3969-438f-b0eb-5682976da250_1400x728.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!a0PL!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff63936ba-3969-438f-b0eb-5682976da250_1400x728.png 424w, /__u/substackcdn.com/image/fetch/$s_!a0PL!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff63936ba-3969-438f-b0eb-5682976da250_1400x728.png 848w, /__u/substackcdn.com/image/fetch/$s_!a0PL!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff63936ba-3969-438f-b0eb-5682976da250_1400x728.png 1272w, /__u/substackcdn.com/image/fetch/$s_!a0PL!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff63936ba-3969-438f-b0eb-5682976da250_1400x728.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!a0PL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff63936ba-3969-438f-b0eb-5682976da250_1400x728.png" width="1400" height="728" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f63936ba-3969-438f-b0eb-5682976da250_1400x728.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:728,&quot;width&quot;:1400,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!a0PL!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff63936ba-3969-438f-b0eb-5682976da250_1400x728.png 424w, /__u/substackcdn.com/image/fetch/$s_!a0PL!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff63936ba-3969-438f-b0eb-5682976da250_1400x728.png 848w, /__u/substackcdn.com/image/fetch/$s_!a0PL!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff63936ba-3969-438f-b0eb-5682976da250_1400x728.png 1272w, /__u/substackcdn.com/image/fetch/$s_!a0PL!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff63936ba-3969-438f-b0eb-5682976da250_1400x728.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The metrics for ConvNEXT and Swin Transformer were good. However, testing the model on new images gave me inflated results where medium traffic images on high traffic roads were classified as high traffic, which was less of an issue with a standard ViT model.</p><p>This could have been a result of how my dataset had been filtered confusing the model with too many medium-traffic images in the high-traffic folder so I wouldn&#8217;t disregard these other models.</p><blockquote><p><em>The metrics did not improve with a custom trainer to account for weight imbalances in the dataset when training a ViT. It performed about the same in practice with worse metrics. But there is probably room for improvement there as well.</em></p></blockquote><p>Lastly, <strong>trying to balance the dataset manually by further introducing more high traffic images</strong> unfortunately <strong>gave me more generalized results</strong> on some high traffic roads &#8212; i.e. for high traffic roads it inflated the results regardless if the road was mostly empty &#8212; so for this case having an unbalanced dataset proved fruitful.</p><p>I did not try data augmentation to increase the size of the imbalanced dataset as I didn&#8217;t believe it would do any good.</p><blockquote><p><em>Nevertheless, continuing to work on the dataset by setting up a script to pick up images over a longer period of time is a good idea. I would still keep it skewed to reflect the real thing though as I found it did better if it reflected reality.</em></p></blockquote><p>Traffic images are notoriously difficult, as each camera will show a different scenario and a different scenery. Because of this I would also set up an algorithm later that would check how many images have been classified with high traffic between a few minutes to estimate how bad the traffic congestion is.</p><p>I would stress that this model is not battle-tested, and it would need to get further tested and trained.</p><h2>Training the Model</h2><p>I have provided several <a href="https://github.com/ilsilfverskiold/smaller-models-docs/tree/main/computer-vision/cook/image-classification">cook books</a> so you can test both a CNN and ViT model using the HuggingFace trainer. You can tweak them as you go along, if needed.</p><p>The process to train this model, is the following.</p><ol><li><p><strong>Prepare</strong> the dataset</p></li><li><p><strong>Preprocess</strong> the dataset</p></li><li><p>Decide on your <strong>metrics</strong></p></li><li><p><strong>Train</strong> the model</p></li><li><p><strong>Evaluate</strong> the model</p></li></ol><h3>Preparing a Dataset</h3><p>When you work with image classification, you want to sort your images into folders. The folders will act as your labels, or categories.</p><p>From here it is easy enough to prepare and load your dataset so it can be pushed to the HuggingFace hub.</p><p>I usually mount my Google Drive in Colab and then simply load the dataset.</p><pre><code>from google.colab import drive
drive.mount(&#8217;/content/drive&#8217;)

from datasets import load_dataset  dataset = load_dataset(&#8217;imagefolder&#8217;, data_dir=dataset_path)   </code></pre><p>You may want to check that the folders don&#8217;t have any corrupt files that will later be a problem when you train the model.</p><pre><code>from PIL import Image
import os

dataset_path = &#8216;/content/drive/MyDrive/your-image-folder&#8217;

def verify_images(folder_path):
    for subdir, dirs, files in os.walk(folder_path):
        for file in files:
            filepath = os.path.join(subdir, file)
            try:
                with Image.open(filepath) as img:
                    img.verify()
            except (IOError, SyntaxError) as e:
                print(f&#8217;Corrupt image: {filepath} | Error: {e}&#8217;)
                os.remove(filepath)

verify_images(dataset_path)</code></pre><p>If you first do the check above, and delete any corrupt files, load the dataset after this.</p><p>Once you&#8217;ve loaded it, you can continue to split it into a training and validation set.</p><pre><code>from datasets import load_dataset, DatasetDict

train_dataset = dataset[&#8221;train&#8221;]

split_datasets = train_dataset.train_test_split(test_size=0.1, seed=42, stratify_by_column=&#8217;label&#8217;)

train_dataset = split_datasets[&#8217;train&#8217;]
val_dataset = split_datasets[&#8217;test&#8217;]

dataset_dict = DatasetDict({
    &#8216;train&#8217;: train_dataset,
    &#8216;validation&#8217;: val_dataset
})</code></pre><p>We always use a training set to fit the model and a validation set to evaluate its performance.</p><p>When you&#8217;re done you can train it directly or you can push it to the HuggingFace hub to store for later training.</p><p>To push it to the hub you login like so.</p><pre><code>!huggingface-cli login</code></pre><p>You&#8217;ll need an access token that you can find in your HuggingFace account under Settings.</p><p>Then you simply push it.</p><pre><code>repo_name = &#8220;username/traffic-camera-norway-images&#8221; 
dataset_dict.push_to_hub(repo_name)</code></pre><p>See the full script <a href="https://github.com/ilsilfverskiold/smaller-models-docs/blob/main/computer-vision/cook/image-classification/dataset/Image_dataset_push_huggingface.ipynb">here</a> to prepare your image data.</p><h3>Find a Pre-Trained Model</h3><p>Now as mentioned you&#8217;ll have to decide which pre-trained model you want to train. I will use a ViT here because it did best, but if you want to go for a ConvNEXT or a CNN model see other cook books <a href="https://github.com/ilsilfverskiold/smaller-models-docs/tree/main/computer-vision/cook/image-classification">here</a>.</p><p>I&#8217;ll be going with the model <a href="https://huggingface.co/google/vit-base-patch16-224">vit-base-patch16&#8211;224</a> but you can try another one but just make sure you match the input specifications of each model, particularly the image size, when you preprocess your data later. I&#8217;ll explain it once we get to this part.</p><p>The <a href="https://huggingface.co/google/vit-base-patch16-224">vit-base-patch16&#8211;224</a> model has been pre-trained on the ImageNet-21k which should be enough data.</p><h3>Open the Colab Notebook</h3><p>If you&#8217;re good to go you can open <a href="https://github.com/ilsilfverskiold/smaller-models-docs/blob/main/computer-vision/cook/image-classification/fine-tune/ViT_Huggingface_Trainer.ipynb">this</a> Colab notebook that I have already prepared.</p><p>At the start you&#8217;ll be able to set a few variables, your dataset url in HuggingFace, the pre-trained model you&#8217;ll fine-tune, the new model name as well as the learning rate, epochs and batch size.</p><pre><code>dataset_url = &#8220;ilsilfverskiold/traffic-camera-norway-images&#8221; 
model_checkpoint = &#8220;google/vit-base-patch16-224&#8221; 
new_model_name = &#8216;traffic-image-classification&#8217;
learning_rate = 5e-5
epochs = 5
batch_size= 32</code></pre><p>The standard <strong>learning rate is 5e-5</strong>, and with 6800 images<strong> something like 5&#8211;10 epochs should be ideal</strong>. I didn&#8217;t find that it better after 5 epochs but it is up to you if you&#8217;d like to test it on more or less.</p><p>There should be a ton of information on this out there if you&#8217;d like to dig deeper.</p><p>I will skip a few sections, but do make sure you follow along in the Colab <a href="https://github.com/ilsilfverskiold/smaller-models-docs/tree/main/computer-vision/cook/image-classification">notebook</a>.</p><h3>Preprocess Dataset</h3><p>This pre-trained model we&#8217;re using has expectations on what the data should look like when we train it. Different models may have been trained with different image normalization standards.</p><p>So we load something called an image processor.</p><pre><code>from transformers import AutoImageProcessor

image_processor  = AutoImageProcessor.from_pretrained(model_checkpoint)</code></pre><p>We&#8217;ll use this one to normalize the images we&#8217;ll be training with so it will adjust the color channels to the same range and scale that the model was originally trained on.</p><p>We also resize the images to 256 pixels and then crop to the center to achieve a final size of 224x224 pixels, to follow the model&#8217;s input requirements.</p><p><em>Remember we picked a model with a resolution of 224x224 with <a href="https://huggingface.co/google/vit-base-patch16-224">vit-base-patch16&#8211;224</a>.</em></p><pre><code>from torchvision.transforms import (
    Compose,
    Resize,
    Normalize,
    CenterCrop,
    RandomHorizontalFlip,
    RandomResizedCrop,
    ToTensor,
)

normalize = Normalize(mean=image_processor.image_mean, std=image_processor.image_std)

train_transform = Compose([
    Resize(256),
    CenterCrop(224),
    RandomHorizontalFlip(),
    ToTensor(),
    normalize,
])

val_transform = Compose([
    Resize(256),
    CenterCrop(224),
    ToTensor(),
    normalize,
])

def apply_transform(examples, transform):
    examples[&#8217;pixel_values&#8217;] = [transform(image.convert(&#8217;RGB&#8217;)) for image in examples[&#8217;image&#8217;]]
    return examples

def set_dataset_transform(dataset, transform):
    dataset.set_transform(lambda examples: apply_transform(examples, transform))

set_dataset_transform(dataset[&#8217;train&#8217;], train_transform)
set_dataset_transform(dataset[&#8217;validation&#8217;], val_transform)</code></pre><p>The other random stuff we do, like random flips and other similar augmentation techniques are used to improve the robustness and generalization ability of machine learning models.</p><p>Lastly, the <code>ToTensor()</code> transformation converts PIL images to PyTorch tensors, formatting the data that is required by PyTorch models.</p><p>After this it is good to check that you have another field called pixel_values for each item with tensors.</p><pre><code>dataset[&#8217;train&#8217;][0]</code></pre><p>Remember to follow along in the Colab <a href="https://github.com/ilsilfverskiold/smaller-models-docs/blob/main/computer-vision/cook/image-classification/fine-tune/ViT_Huggingface_Trainer.ipynb">notebook</a> for the entire script.</p><h3>Evaluation Metrics</h3><p>You&#8217;ll also want to set up some evaluation metrics. You saw earlier that I was evaluating the model based on a few metrics although you always have to test it manually as well to see how it does with new data.</p><p>The most important metric you&#8217;re interested in is <strong>Accuracy,</strong> which measures the amount of predictions the model got right across all categories. This one you&#8217;ll see everywhere but there are other metrics you may want to use as well.</p><p><strong>Precision </strong>measures how often predictions for a specific category are correct. So for my case, with traffic levels, high precision in a traffic category like &#8216;high traffic&#8217; means that when the model predicts high traffic, it is usually right.</p><p><strong>Recall</strong> tells us how well the model can identify all instances within a specific category, such as &#8216;low traffic&#8217;. High recall means the model is good at recognizing most low traffic situations.</p><p>The <strong>F1 Score</strong> is the weighted average of <strong>Precision</strong> and <strong>Recall</strong>.</p><p>When you have a skewed dataset like I have where some categories don&#8217;t show up as much as others, a high F1 Score is really good. It means the model is not just guessing well for the common categories, but also doing a great job at picking up the rare ones correctly.</p><p>To tweak the metrics I&#8217;ve set up for this use case, you can navigate to this part of the Colab notebook.</p><pre><code>import numpy as np
from datasets import load_metric

accuracy_metric = load_metric(&#8221;accuracy&#8221;)
precision_metric = load_metric(&#8221;precision&#8221;)
recall_metric = load_metric(&#8221;recall&#8221;)
f1_metric = load_metric(&#8221;f1&#8221;)

def compute_metrics(eval_pred):
    logits, labels = eval_pred
    predictions = np.argmax(logits, axis=1)

    accuracy = accuracy_metric.compute(predictions=predictions, references=labels)
    precision = precision_metric.compute(predictions=predictions, references=labels, average=&#8217;macro&#8217;)
    recall = recall_metric.compute(predictions=predictions, references=labels, average=&#8217;macro&#8217;)
    f1 = f1_metric.compute(predictions=predictions, references=labels, average=&#8217;macro&#8217;)

    metrics = {
        &#8220;accuracy&#8221;: accuracy[&#8217;accuracy&#8217;],
        &#8220;precision&#8221;: precision[&#8217;precision&#8217;],
        &#8220;recall&#8221;: recall[&#8217;recall&#8217;],
        &#8220;f1&#8221;: f1[&#8217;f1&#8217;]
    }
    return metrics</code></pre><h3>Model Training</h3><p>From here we can prepare to train the model. Remember not to skip any parts in the <a href="https://github.com/ilsilfverskiold/smaller-models-docs/blob/main/computer-vision/cook/image-classification/fine-tune/ViT_Huggingface_Trainer.ipynb">notebook</a> as I&#8217;m not going through all the steps here.</p><p>I&#8217;m using an L4 in Colab to train as I have a pro membership but this should work with a T4 as well, only the training may be slightly slower. Just remember to use a GPU.</p><p>Ideally you can keep the training arguments as is, and tweak epochs and learning_rate at the start.</p><pre><code>args = TrainingArguments(
    f&#8221;{new_model_name}&#8221;,
    remove_unused_columns=False,
    evaluation_strategy = &#8220;epoch&#8221;,
    save_strategy = &#8220;epoch&#8221;,
    learning_rate=learning_rate,
    per_device_train_batch_size=batch_size,
    gradient_accumulation_steps=4,
    per_device_eval_batch_size=batch_size,
    num_train_epochs=epochs,
    warmup_ratio=0.1,
    logging_steps=10,
    weight_decay=weight_decay,
    load_best_model_at_end=True,
    metric_for_best_model=&#8221;accuracy&#8221;,
    push_to_hub=False,
)</code></pre><p>Do not remove remove_unused_columns as you&#8217;ll get an error. I have specified to not push it to the hub as I want to evaluate the model first.</p><p>We also set up the trainer with the prepared train and validation datasets.</p><pre><code>trainer = Trainer(
    model,
    args,
    train_dataset=dataset[&#8217;train&#8217;],
    eval_dataset=dataset[&#8217;validation&#8217;],
    tokenizer=image_processor,
    compute_metrics=compute_metrics,
    data_collator=collate_fn,
)</code></pre><p>If you&#8217;re satisfied you can go ahead and train the model.</p><pre><code>train_results = trainer.train()

trainer.save_model()
trainer.log_metrics(&#8221;train&#8221;, train_results.metrics)
trainer.save_metrics(&#8221;train&#8221;, train_results.metrics)
trainer.save_state()</code></pre><p>You&#8217;ll see the metrics for each epoch once it is training. What you&#8217;re looking for here is the training loss, which should consistently going down, while validation loss should do the same. I sometimes see it fluctuating, though it shouldn&#8217;t so do keep an eye on it.</p><p>Accuracy should obviously increase, as I mentioned when I talked about the evaluation metrics earlier.</p><h3>Evaluate Model</h3><p>Once it has finished training you can evaluate the final metrics.</p><pre><code>metrics = trainer.evaluate()
trainer.log_metrics(&#8221;eval&#8221;, metrics)
trainer.save_metrics(&#8221;eval&#8221;, metrics)</code></pre><p>My metrics weren&#8217;t stellar for this first run, but good enough for this case.</p><pre><code>***** eval metrics *****
  epoch                   =     4.9215
  eval_accuracy           =     0.8292
  eval_f1                 =     0.7721 # good enough
  eval_loss               =     0.4394
  eval_precision          =     0.8232
  eval_recall             =     0.7366</code></pre><p>The loss was quite high while the training loss a bit lower, which could indicate overfitting. I had better metrics for a few other models.</p><p>This is where you&#8217;ll want to also test the model on new data to see how it does. This model did better at new images than the other models that inflated the labels from medium to high traffic.</p><p>This could have been an issue with my validation set just not being large enough, and not representing real use cases.</p><p>So, I would also test it on various images to see how it does. For me this was easy enough as I ran it through a few new traffic images that I had stored in my Google Drive.</p><p>To do this save the model and then set up the pipeline with the new model.</p><pre><code>trainer.save_model(&#8217;new_model&#8217;)</code></pre><pre><code>from transformers import pipeline

pipe = pipeline(&#8217;image-classification&#8217;, model=&#8217;new_model&#8217;)</code></pre><p>Connect your Google Drive.</p><pre><code>from google.colab import drive
drive.mount(&#8217;/content/drive&#8217;)</code></pre><p>Then do some inference on the images you want.</p><pre><code>from PIL import Image

image_path = &#8216;/content/drive/MyDrive/image_to_test.jpg&#8217; # path to your image

image = Image.open(image_path)

results = pipe(image)
results</code></pre><h3>Push to Hub (Optional)</h3><p>If you&#8217;re decently satisfied, you can push the model to the Hub to use from there or to deploy it as an inference endpoint so you can use it in production.</p><p>You&#8217;ll see me doing this in the Colab <a href="https://github.com/ilsilfverskiold/smaller-models-docs/blob/main/computer-vision/cook/image-classification/fine-tune/ViT_Huggingface_Trainer.ipynb">notebook</a> at the end. The notebook should help you from start to finish.</p><div><hr></div><p>Now we can test the model in the hub using the Inference API and see how it does.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!QYo0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9077cc39-5f1c-4000-860b-9b485846241f_800x386.gif" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!QYo0!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9077cc39-5f1c-4000-860b-9b485846241f_800x386.gif 424w, /__u/substackcdn.com/image/fetch/$s_!QYo0!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9077cc39-5f1c-4000-860b-9b485846241f_800x386.gif 848w, /__u/substackcdn.com/image/fetch/$s_!QYo0!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9077cc39-5f1c-4000-860b-9b485846241f_800x386.gif 1272w, /__u/substackcdn.com/image/fetch/$s_!QYo0!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9077cc39-5f1c-4000-860b-9b485846241f_800x386.gif 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!QYo0!,w_1456,c_limit,f_auto,q_auto:good,fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9077cc39-5f1c-4000-860b-9b485846241f_800x386.gif" width="800" height="386" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9077cc39-5f1c-4000-860b-9b485846241f_800x386.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:386,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="/__u/substackcdn.com/image/fetch/$s_!QYo0!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9077cc39-5f1c-4000-860b-9b485846241f_800x386.gif 424w, /__u/substackcdn.com/image/fetch/$s_!QYo0!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9077cc39-5f1c-4000-860b-9b485846241f_800x386.gif 848w, /__u/substackcdn.com/image/fetch/$s_!QYo0!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9077cc39-5f1c-4000-860b-9b485846241f_800x386.gif 1272w, /__u/substackcdn.com/image/fetch/$s_!QYo0!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9077cc39-5f1c-4000-860b-9b485846241f_800x386.gif 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>I&#8217;m always terrified that I&#8217;ll find something I don&#8217;t want to see so I haven&#8217;t yet tried any edge cases yet, like a picture with a bunch of trees in the middle of the screen.</p><p>Nevertheless, we know it might miss a few high traffic images but at least it&#8217;s not classifying low traffic as high traffic.</p><p>I open sourced the model I created <a href="https://huggingface.co/ilsilfverskiold/traffic-levels-image-classification">here</a> which is probably far from perfect but you are very welcome to use it.</p><p>I also open sourced the first dataset that you can find <a href="https://huggingface.co/datasets/ilsilfverskiold/traffic-camera-norway-images">here</a>.</p><div><hr></div><p>Hopefully this proved resourceful and you get some inspiration for your next project.</p><p>To follow my writing, building and whatnot look into my <a href="https://www.ilsilfverskiold.com/">website,</a> or my <a href="https://www.linkedin.com/in/ida-silfverskiold/">LinkedIn</a> as well. </p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://howtouseai.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading AI Practical Deep Dives. Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Agentic AI Frameworks: Learn LangGraph]]></title><description><![CDATA[By building a single agent workflow with LangGraph Studio]]></description><link>https://howtouseai.substack.com/p/agentic-ai-frameworks-learning-langgraph</link><guid isPermaLink="false">https://howtouseai.substack.com/p/agentic-ai-frameworks-learning-langgraph</guid><dc:creator><![CDATA[Ida Silfverskiold]]></dc:creator><pubDate>Thu, 27 Mar 2025 17:36:12 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!AT-1!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88de697f-e6f0-4e08-a7ec-449e6d17e18e_1756x806.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><em>This piece is part of a longer series on different concepts, technologies, and frameworks within agentic AI.</em></p><p><a href="https://www.langchain.com/langgraph">LangGraph</a> is one of the more popular <strong>low-level orchestration</strong> frameworks for building agentic workflows among the developer community. It was built by the LangChain team, who hoped it would give builders better control by making the flow explicit.</p><p>It might feel overwhelming at first, but it gets easier once you understand the fundamentals&#8212;which is exactly what we&#8217;ll go through here by <strong>building a single-agent workflow</strong> that we can <strong>visualize and test in LangGraph Studio.</strong></p><p>Essentially, with LangGraph, you&#8217;re writing code that can be visualized as a graph with nodes. You won&#8217;t be working in a visual programming system; instead, you code it manually and then visualize and debug it via LangGraph Studio (though that part is optional).</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!AT-1!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88de697f-e6f0-4e08-a7ec-449e6d17e18e_1756x806.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!AT-1!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88de697f-e6f0-4e08-a7ec-449e6d17e18e_1756x806.png 424w, /__u/substackcdn.com/image/fetch/$s_!AT-1!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88de697f-e6f0-4e08-a7ec-449e6d17e18e_1756x806.png 848w, /__u/substackcdn.com/image/fetch/$s_!AT-1!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88de697f-e6f0-4e08-a7ec-449e6d17e18e_1756x806.png 1272w, /__u/substackcdn.com/image/fetch/$s_!AT-1!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88de697f-e6f0-4e08-a7ec-449e6d17e18e_1756x806.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!AT-1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88de697f-e6f0-4e08-a7ec-449e6d17e18e_1756x806.png" width="1456" height="668" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/88de697f-e6f0-4e08-a7ec-449e6d17e18e_1756x806.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:668,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:534193,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://howtouseai.substack.com/i/159820961?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88de697f-e6f0-4e08-a7ec-449e6d17e18e_1756x806.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!AT-1!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88de697f-e6f0-4e08-a7ec-449e6d17e18e_1756x806.png 424w, /__u/substackcdn.com/image/fetch/$s_!AT-1!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88de697f-e6f0-4e08-a7ec-449e6d17e18e_1756x806.png 848w, /__u/substackcdn.com/image/fetch/$s_!AT-1!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88de697f-e6f0-4e08-a7ec-449e6d17e18e_1756x806.png 1272w, /__u/substackcdn.com/image/fetch/$s_!AT-1!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F88de697f-e6f0-4e08-a7ec-449e6d17e18e_1756x806.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3>Understanding the fundamentals</h3><p>To build something simple, we still have to go through a few core pieces: <strong>graphs, nodes, edges, and state.</strong></p><p>The <strong>graph</strong> is the overall framework for setting up your agent&#8217;s workflow. See it as the environment we need to execute our workflow. We always set up the graph first.</p><p>After defining the graph, we set up our <strong>nodes</strong>&#8212;these are the <strong>core functionalities or operations</strong>. Nodes are where everything happens. In an agent workflow, a node could do anything from calling an LLM to invoking a tool (like a search function or an API), or performing some computation.</p><p>The <strong>edges</strong> are the connections between nodes. <strong>They tell the graph which node to go to next.</strong> Edges can be static (always go from A to B) or conditional (branch based on something in the state). So essentially, nodes do the work, edges decide what happens next.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!Am_U!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F51304402-03f9-49a0-bc04-d2b50f5447ee_1858x564.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!Am_U!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F51304402-03f9-49a0-bc04-d2b50f5447ee_1858x564.png 424w, /__u/substackcdn.com/image/fetch/$s_!Am_U!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F51304402-03f9-49a0-bc04-d2b50f5447ee_1858x564.png 848w, /__u/substackcdn.com/image/fetch/$s_!Am_U!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F51304402-03f9-49a0-bc04-d2b50f5447ee_1858x564.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Am_U!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F51304402-03f9-49a0-bc04-d2b50f5447ee_1858x564.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!Am_U!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F51304402-03f9-49a0-bc04-d2b50f5447ee_1858x564.png" width="1456" height="442" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/51304402-03f9-49a0-bc04-d2b50f5447ee_1858x564.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:442,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:291416,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://howtouseai.substack.com/i/159820961?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F51304402-03f9-49a0-bc04-d2b50f5447ee_1858x564.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!Am_U!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F51304402-03f9-49a0-bc04-d2b50f5447ee_1858x564.png 424w, /__u/substackcdn.com/image/fetch/$s_!Am_U!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F51304402-03f9-49a0-bc04-d2b50f5447ee_1858x564.png 848w, /__u/substackcdn.com/image/fetch/$s_!Am_U!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F51304402-03f9-49a0-bc04-d2b50f5447ee_1858x564.png 1272w, /__u/substackcdn.com/image/fetch/$s_!Am_U!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F51304402-03f9-49a0-bc04-d2b50f5447ee_1858x564.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><strong>State</strong> is the current memory or context of the workflow, where we hold all the data that flows between nodes.</p><p>You can think of state as <strong>short-term memory</strong>&#8212;it sticks around during the current run or conversation. It often holds things like conversation history between LLM calls or variables we need in multiple places. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!gsVQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a1fed97-249e-470f-855a-d836327c2daa_1614x566.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!gsVQ!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a1fed97-249e-470f-855a-d836327c2daa_1614x566.png 424w, /__u/substackcdn.com/image/fetch/$s_!gsVQ!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a1fed97-249e-470f-855a-d836327c2daa_1614x566.png 848w, /__u/substackcdn.com/image/fetch/$s_!gsVQ!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a1fed97-249e-470f-855a-d836327c2daa_1614x566.png 1272w, /__u/substackcdn.com/image/fetch/$s_!gsVQ!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a1fed97-249e-470f-855a-d836327c2daa_1614x566.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!gsVQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a1fed97-249e-470f-855a-d836327c2daa_1614x566.png" width="1456" height="511" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7a1fed97-249e-470f-855a-d836327c2daa_1614x566.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:511,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:271771,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://howtouseai.substack.com/i/159820961?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a1fed97-249e-470f-855a-d836327c2daa_1614x566.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!gsVQ!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a1fed97-249e-470f-855a-d836327c2daa_1614x566.png 424w, /__u/substackcdn.com/image/fetch/$s_!gsVQ!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a1fed97-249e-470f-855a-d836327c2daa_1614x566.png 848w, /__u/substackcdn.com/image/fetch/$s_!gsVQ!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a1fed97-249e-470f-855a-d836327c2daa_1614x566.png 1272w, /__u/substackcdn.com/image/fetch/$s_!gsVQ!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7a1fed97-249e-470f-855a-d836327c2daa_1614x566.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Technically, it&#8217;s just an object where we store relevant info and pass it along from node to node. This is especially important for LLMs, since we need to pass in the conversation history for each call (as LLMs are stateless by nature).</p><p>So in short: the <strong>graph is the canvas</strong>, <strong>nodes are the actions</strong>, <strong>edges define the flow</strong>, and <strong>state carries the data or information</strong> throughout.</p><h3>Setting up the environment</h3><p>To make this work, you&#8217;ll need to have <a href="https://www.python.org/">Python</a> installed on your computer&#8212;version 3.9 or above.</p><p>You&#8217;ll also need the latest version of <a href="https://www.docker.com/">Docker</a>. If you run into issues with an older version not updating, uninstall Docker completely and download the latest version from scratch.</p><p>Then, download <a href="https://studio.langchain.com/">LangGraph Studio</a> (desktop), since we&#8217;ll use it to visualize and debug the workflow. If you&#8217;re not on a Mac, there should be other options you can explore.</p><h3>Setting up the files</h3><p>To start, you can create a new folder to set up this workflow in:</p><pre><code>mkdir langgraph_example
cd langgraph_example</code></pre><p>Inside this folder, create another folder called <code>agent</code>:</p><pre><code>mkdir agent</code></pre><p>This is where we&#8217;ll set up the code for the agent.</p><p>Since I&#8217;m using LangGraph Studio, I&#8217;m organizing the code for that&#8212;but you don&#8217;t have to follow this exact structure if you&#8217;re not using the Studio.</p><p>Now create a <code>langgraph.json</code> file:</p><pre><code>touch langgraph.json</code></pre><p>This file tells LangGraph where to find the agent code and the <code>.env</code> file for credentials:</p><pre><code>{
  "dependencies": ["./agent"],
  "graphs": {
    "agent": "./agent/agent.py:graph"
  },
  "env": ".env"
}</code></pre><p>Next, go into the <code>agent</code> folder and create a <code>requirements.txt</code> file:</p><pre><code>langgraph
python-dotenv</code></pre><p>Also create a file called <code>agent.py</code> in the same folder&#8212;this is where we define our workflow.</p><p>So far, the structure should look like this:</p><pre><code>langgraph_example/
&#9474;
&#9500;&#9472;&#9472; langgraph.json
&#9474;
&#9492;&#9472;&#9472; agent/
    &#9500;&#9472;&#9472; agent.py
    &#9492;&#9472;&#9472; requirements.txt</code></pre><h3>Defining the graph</h3><p>Now we can start building our agent by defining the graph. As mentioned earlier, the graph is the foundational structure for the workflow. It acts as the execution environment where nodes, edges, and state work together in a coordinated way.</p><p>In <code>agent.py</code>, start with this:</p><pre><code><code>from langgraph.graph import StateGraph

# define a new graph
workflow = StateGraph('')</code></code></pre><p>This will allow us to later attach nodes as operations/functions, and edges to define transitions between them.</p><h3>Define the state</h3><p>Next, we define the custom state and pass it to <code>StateGraph</code>:</p><pre><code><code>from langgraph.graph import StateGraph
from langgraph.graph import add_messages
from langchain_core.messages import BaseMessage
from typing import TypedDict, Annotated, Sequence

class AgentState(TypedDict):
    messages: Annotated[Sequence[BaseMessage], add_messages]

workflow = StateGraph(AgentState) # Add the AgentState here</code></code></pre><p>Think of state as a schema that defines what fields can exist and flow between nodes. When a node returns data, it&#8217;ll be checked against this schema. If the key exists, it gets updated&#8212;if not, you&#8217;ll likely get an error.</p><p>Here we&#8217;re only using <code>messages</code>, but you can expand this to include any data you want passed between nodes.</p><p>If you're unfamiliar with how message history works in LLMs&#8212;remember they&#8217;re stateless. We need to store messages ourselves so the model &#8220;remembers&#8221; context between calls.</p><p>Is it necessary to have this state object in LangGraph? Yes. Without it, the model wouldn&#8217;t know what tools were called or what information it had gathered. And building this without tools defeats the purpose entirely. </p><h3>Set Up the LLM</h3><p>Next up we&#8217;ll set up the LLM model we&#8217;ll be using as the base for our agent. We&#8217;ll use LangChain to connect to Gemini 2.0 Flash:</p><pre><code><code>from langchain_google_genai import ChatGoogleGenerativeAI

def _get_model():
    model = ChatGoogleGenerativeAI(model="gemini-2.0-flash-001")
    
    return model</code></code></pre><p>To be able to import ChatGoogleGenerativeAI we need to add this dependency to <code>requirements.txt</code>:</p><pre><code><code>langchain_google_genai</code></code></pre><p>Also make sure to create a <code>.env</code> file in the root with:</p><pre><code>GOOGLE_API_KEY=your_key_here</code></pre><p>LangChain will automatically pick this up based on the path set in <code>langgraph.json</code>.</p><p><em>If you want to use another model you can look into LangChain documentation on models <a href="https://python.langchain.com/docs/integrations/chat/">here</a>.</em></p><h3>Call the Model</h3><p>Now let&#8217;s define the function that we&#8217;ll attach to a node that calls the LLM:</p><pre><code><code>system_prompt = "You are such a nice helpful bot"

def call_model(state):
    messages = state["messages"]
    
    # add the system message
    system_message = SystemMessage(content=system_prompt)
    full_messages = [system_message] + messages
    
    model = _get_model()
    response = model.invoke(full_messages)
    
    return {"messages": [response]}</code></code></pre><p>As you see here we can directly access state via the params where we store the messages of the function and then simply invoke the model. The messages are then returned to state.</p><p>Here you will ideally add some error handling in case the LLM call fails, but to keep things very simple I&#8217;ve just added the bare minimum. </p><h3>Define and Attach Nodes</h3><p>Now that we have the LLM setup we can start to define our nodes.</p><p>Remember the nodes are the functions or operations in our workflow. The first node for the agent we&#8217;ll point to the call_model function we defined earlier.</p><pre><code><code># define nodes
workflow.add_node("agent", call_model)
workflow.add_node("tools", tool_node)</code></code></pre><p>We can then setup one for the tools it should have access to. As of right now we still haven&#8217;t defined what is in the &#8216;tools&#8217; node so we need to code this as well.</p><h3>Adding tools</h3><p>To add tools, we&#8217;ll create a few mock tools (functions) just for demonstration. These should ideally do something meaningful&#8212;like calling an API and returning real information:</p><pre><code><code>from langgraph.prebuilt import ToolNode
from langchain_core.tools import tool

@tool
def weather_tool() -&gt; str:
    """Get the current weather."""
    return "Weather is 19&#176;C and Partly Cloudy"

@tool
def calendar_tool() -&gt; str:
    """Check your calendar for meetings on a specific date."""
    return "No meetings scheduled"

all_tools = [weather_tool, calendar_tool]
tool_node = ToolNode(all_tools)</code></code></pre><p>We then bundle the tools together into a ToolNode that we import from LangGraph. This one should handls the execution of tools in our workflow. </p><p>If you define parameters for the tools, the model will understand that it can use them after we bind them. It will receive the tool name, description, parameters, and return type.</p><p>We can now visualise what we&#8217;ve set up so far.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!l6oh!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6488678d-e927-409d-9ccd-904d17246024_1438x568.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!l6oh!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6488678d-e927-409d-9ccd-904d17246024_1438x568.png 424w, /__u/substackcdn.com/image/fetch/$s_!l6oh!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6488678d-e927-409d-9ccd-904d17246024_1438x568.png 848w, /__u/substackcdn.com/image/fetch/$s_!l6oh!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6488678d-e927-409d-9ccd-904d17246024_1438x568.png 1272w, /__u/substackcdn.com/image/fetch/$s_!l6oh!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6488678d-e927-409d-9ccd-904d17246024_1438x568.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!l6oh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6488678d-e927-409d-9ccd-904d17246024_1438x568.png" width="1438" height="568" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6488678d-e927-409d-9ccd-904d17246024_1438x568.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:568,&quot;width&quot;:1438,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:239040,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://howtouseai.substack.com/i/159820961?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6488678d-e927-409d-9ccd-904d17246024_1438x568.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!l6oh!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6488678d-e927-409d-9ccd-904d17246024_1438x568.png 424w, /__u/substackcdn.com/image/fetch/$s_!l6oh!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6488678d-e927-409d-9ccd-904d17246024_1438x568.png 848w, /__u/substackcdn.com/image/fetch/$s_!l6oh!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6488678d-e927-409d-9ccd-904d17246024_1438x568.png 1272w, /__u/substackcdn.com/image/fetch/$s_!l6oh!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6488678d-e927-409d-9ccd-904d17246024_1438x568.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>As noted earlier, when we bind the tools to the model, it will become aware of their existence. So we need to go back to the <code>_get_model()</code> function and update it:</p><pre><code><code>from langchain_google_genai import ChatGoogleGenerativeAI

def _get_model():
    model = ChatGoogleGenerativeAI(model="gemini-2.0-flash-001")

    model = model.bind_tools(all_tools) # add this line
    
    return model</code></code></pre><p>This part is crucial&#8212;it tells the model which tools are available and how to use them. There&#8217;s a lot of abstraction happening here where LangChain structures the interaction between your Python code and the LLM API using bind_tools.</p><p>I would say there are abstractions in this entire framework that you may want to do more proper research around later. </p><h3>Adding edges</h3><p>Now we can start connecting these nodes, but first we set the entry point for the workflow:</p><pre><code><code># entry point
workflow.set_entry_point("agent")</code></code></pre><p>Next, we&#8217;ll define some conditional logic, where we set up a function that decide if we should keep calling the LLM based on what it did last. </p><pre><code><code># define the function that determines whether to continue or not
def should_continue(state):
    messages = state["messages"]
    last_message = messages[-1]
    # if there are no tool calls, then we finish
    if not last_message.tool_calls:
        return "end"
    # if there is, we continue
    else:
        return "continue"</code></code></pre><p>This logic tells the workflow to continue if the model just used a tool, and end if it didn&#8217;t.</p><p>To support this, we set up a conditional edge (a dashed connection between the agent and tool nodes):</p><pre><code><code>from langgraph.graph import END

workflow.add_conditional_edges(
    "agent",
    should_continue,
    {
        "continue": "tools",
        "end": END,
    },
)</code></code></pre><p>If the last LLM call didn&#8217;t use any tools, we assume the workflow is complete and stop it.</p><p>Here we are also importing END from LangGraph which is a termination signal to stop node traversal and return the final result. </p><p>We also need to connect the tools back to the agent:</p><pre><code><code># connect tools back to agent
workflow.add_edge("tools", "agent")</code></code></pre><p>This is needed so that after a tool executes, its result is sent back to the LLM.</p><p>A question that might come up here is: why do we need this graph structure if the model already knows about the tools through bind_tools?</p><p>The answer is: there's a difference between the model <em>knowing</em> about the tools and actually <em>executing</em> them. The model can't execute tools on its own&#8212;we need the graph to define and control that execution.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!KtsJ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb88ee086-e880-47d6-ad7a-caa187f1b635_1622x664.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!KtsJ!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb88ee086-e880-47d6-ad7a-caa187f1b635_1622x664.png 424w, /__u/substackcdn.com/image/fetch/$s_!KtsJ!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb88ee086-e880-47d6-ad7a-caa187f1b635_1622x664.png 848w, /__u/substackcdn.com/image/fetch/$s_!KtsJ!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb88ee086-e880-47d6-ad7a-caa187f1b635_1622x664.png 1272w, /__u/substackcdn.com/image/fetch/$s_!KtsJ!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb88ee086-e880-47d6-ad7a-caa187f1b635_1622x664.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!KtsJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb88ee086-e880-47d6-ad7a-caa187f1b635_1622x664.png" width="1456" height="596" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b88ee086-e880-47d6-ad7a-caa187f1b635_1622x664.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:596,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:320835,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://howtouseai.substack.com/i/159820961?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb88ee086-e880-47d6-ad7a-caa187f1b635_1622x664.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!KtsJ!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb88ee086-e880-47d6-ad7a-caa187f1b635_1622x664.png 424w, /__u/substackcdn.com/image/fetch/$s_!KtsJ!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb88ee086-e880-47d6-ad7a-caa187f1b635_1622x664.png 848w, /__u/substackcdn.com/image/fetch/$s_!KtsJ!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb88ee086-e880-47d6-ad7a-caa187f1b635_1622x664.png 1272w, /__u/substackcdn.com/image/fetch/$s_!KtsJ!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb88ee086-e880-47d6-ad7a-caa187f1b635_1622x664.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h3>Compiling the workflow</h3><p>The last thing we need to do is compile the workflow:</p><pre><code><code>graph = workflow.compile()</code></code></pre><p>Without this step, nothing will run.</p><p>The finished code will be very simple for our agent.py file. </p><div class="github-gist" data-attrs="{&quot;innerHTML&quot;:&quot;<div id=\&quot;gist137081247\&quot; class=\&quot;gist\&quot;>\n    <div class=\&quot;gist-file\&quot; translate=\&quot;no\&quot; data-color-mode=\&quot;light\&quot; data-light-theme=\&quot;light\&quot;>\n      <div class=\&quot;gist-data\&quot;>\n        <div class=\&quot;js-gist-file-update-container js-task-list-container\&quot;>\n  <div id=\&quot;file-agent-py\&quot; class=\&quot;file my-2\&quot;>\n    \n    <div itemprop=\&quot;text\&quot;\n      class=\&quot;Box-body p-0 blob-wrapper data type-python  \&quot;\n      style=\&quot;overflow: auto\&quot; tabindex=\&quot;0\&quot; role=\&quot;region\&quot;\n      aria-label=\&quot;agent.py content, created by ilsilfverskiold on 03:55PM today.\&quot;\n    >\n\n        \n<div class=\&quot;js-check-bidi js-blob-code-container blob-code-content\&quot;>\n\n  <template class=\&quot;js-file-alert-template\&quot;>\n  <div data-view-component=\&quot;true\&quot; class=\&quot;flash flash-warn flash-full d-flex flex-items-center\&quot;>\n  <svg aria-hidden=\&quot;true\&quot; height=\&quot;16\&quot; viewBox=\&quot;0 0 16 16\&quot; version=\&quot;1.1\&quot; width=\&quot;16\&quot; data-view-component=\&quot;true\&quot; class=\&quot;octicon octicon-alert\&quot;>\n    <path d=\&quot;M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\&quot;></path>\n</svg>\n    <span>\n      This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      <a class=\&quot;Link--inTextBlock\&quot; href=/__u/howtouseai.substack.com/%22https://github.co/hiddenchars/%22 target=\&quot;_blank\&quot;>Learn more about bidirectional Unicode characters</a>\n    </span>\n\n\n  <div data-view-component=\&quot;true\&quot; class=\&quot;flash-action\&quot;>        <a href=/__u/howtouseai.substack.com/%22%7B%7B revealButtonHref }}\&quot; data-view-component=\&quot;true\&quot; class=\&quot;btn-sm btn\&quot;>    Show hidden characters\n</a>\n</div>\n</div></template>\n<template class=\&quot;js-line-alert-template\&quot;>\n  <span aria-label=\&quot;This line has hidden Unicode characters\&quot; data-view-component=\&quot;true\&quot; class=\&quot;line-alert tooltipped tooltipped-e\&quot;>\n    <svg aria-hidden=\&quot;true\&quot; height=\&quot;16\&quot; viewBox=\&quot;0 0 16 16\&quot; version=\&quot;1.1\&quot; width=\&quot;16\&quot; data-view-component=\&quot;true\&quot; class=\&quot;octicon octicon-alert\&quot;>\n    <path d=\&quot;M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\&quot;></path>\n</svg>\n</span></template>\n\n  <table data-hpc class=\&quot;highlight tab-size js-file-line-container\&quot; data-tab-size=\&quot;8\&quot; data-paste-markdown-skip data-tagsearch-path=\&quot;agent.py\&quot;>\n        <tr>\n          <td id=\&quot;file-agent-py-L1\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;1\&quot;></td>\n          <td id=\&quot;file-agent-py-LC1\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-k>from</span> <span class=pl-s1>langchain_google_genai</span> <span class=pl-k>import</span> <span class=pl-v>ChatGoogleGenerativeAI</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L2\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;2\&quot;></td>\n          <td id=\&quot;file-agent-py-LC2\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-k>from</span> <span class=pl-s1>langgraph</span>.<span class=pl-s1>graph</span> <span class=pl-k>import</span> <span class=pl-v>StateGraph</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L3\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;3\&quot;></td>\n          <td id=\&quot;file-agent-py-LC3\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-k>from</span> <span class=pl-s1>langgraph</span>.<span class=pl-s1>graph</span> <span class=pl-k>import</span> <span class=pl-s1>add_messages</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L4\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;4\&quot;></td>\n          <td id=\&quot;file-agent-py-LC4\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-k>from</span> <span class=pl-s1>langchain_core</span>.<span class=pl-s1>messages</span> <span class=pl-k>import</span> <span class=pl-v>BaseMessage</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L5\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;5\&quot;></td>\n          <td id=\&quot;file-agent-py-LC5\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-k>from</span> <span class=pl-s1>langchain_core</span>.<span class=pl-s1>messages</span> <span class=pl-k>import</span> <span class=pl-v>SystemMessage</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L6\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;6\&quot;></td>\n          <td id=\&quot;file-agent-py-LC6\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-k>from</span> <span class=pl-s1>typing</span> <span class=pl-k>import</span> <span class=pl-v>TypedDict</span>, <span class=pl-v>Annotated</span>, <span class=pl-v>Sequence</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L7\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;7\&quot;></td>\n          <td id=\&quot;file-agent-py-LC7\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-k>from</span> <span class=pl-s1>langgraph</span>.<span class=pl-s1>prebuilt</span> <span class=pl-k>import</span> <span class=pl-v>ToolNode</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L8\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;8\&quot;></td>\n          <td id=\&quot;file-agent-py-LC8\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-k>from</span> <span class=pl-s1>langchain_core</span>.<span class=pl-s1>tools</span> <span class=pl-k>import</span> <span class=pl-s1>tool</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L9\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;9\&quot;></td>\n          <td id=\&quot;file-agent-py-LC9\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-k>from</span> <span class=pl-s1>langgraph</span>.<span class=pl-s1>graph</span> <span class=pl-k>import</span> <span class=pl-c1>END</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L10\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;10\&quot;></td>\n          <td id=\&quot;file-agent-py-LC10\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L11\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;11\&quot;></td>\n          <td id=\&quot;file-agent-py-LC11\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-c># define the state schema</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L12\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;12\&quot;></td>\n          <td id=\&quot;file-agent-py-LC12\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-k>class</span> <span class=pl-v>AgentState</span>(<span class=pl-v>TypedDict</span>):</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L13\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;13\&quot;></td>\n          <td id=\&quot;file-agent-py-LC13\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s1>messages</span>: <span class=pl-v>Annotated</span>[<span class=pl-v>Sequence</span>[<span class=pl-smi>BaseMessage</span>], <span class=pl-smi>add_messages</span>]</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L14\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;14\&quot;></td>\n          <td id=\&quot;file-agent-py-LC14\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L15\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;15\&quot;></td>\n          <td id=\&quot;file-agent-py-LC15\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-c># define the tools available</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L16\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;16\&quot;></td>\n          <td id=\&quot;file-agent-py-LC16\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-en>@<span class=pl-s1>tool</span></span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L17\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;17\&quot;></td>\n          <td id=\&quot;file-agent-py-LC17\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-k>def</span> <span class=pl-en>weather_tool</span>() <span class=pl-c1>-&amp;gt;</span> <span class=pl-smi>str</span>:</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L18\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;18\&quot;></td>\n          <td id=\&quot;file-agent-py-LC18\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s>&amp;quot;&amp;quot;&amp;quot;Get the current weather.&amp;quot;&amp;quot;&amp;quot;</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L19\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;19\&quot;></td>\n          <td id=\&quot;file-agent-py-LC19\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-k>return</span> <span class=pl-s>&amp;quot;Weather is 19&#176;C and Partly Cloudy&amp;quot;</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L20\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;20\&quot;></td>\n          <td id=\&quot;file-agent-py-LC20\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L21\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;21\&quot;></td>\n          <td id=\&quot;file-agent-py-LC21\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-en>@<span class=pl-s1>tool</span></span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L22\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;22\&quot;></td>\n          <td id=\&quot;file-agent-py-LC22\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-k>def</span> <span class=pl-en>calendar_tool</span>() <span class=pl-c1>-&amp;gt;</span> <span class=pl-smi>str</span>:</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L23\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;23\&quot;></td>\n          <td id=\&quot;file-agent-py-LC23\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s>&amp;quot;&amp;quot;&amp;quot;Check your calendar for meetings on a specific date.&amp;quot;&amp;quot;&amp;quot;</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L24\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;24\&quot;></td>\n          <td id=\&quot;file-agent-py-LC24\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-k>return</span> <span class=pl-s>&amp;quot;No meetings scheduled&amp;quot;</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L25\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;25\&quot;></td>\n          <td id=\&quot;file-agent-py-LC25\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L26\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;26\&quot;></td>\n          <td id=\&quot;file-agent-py-LC26\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>all_tools</span> <span class=pl-c1>=</span> [<span class=pl-s1>weather_tool</span>, <span class=pl-s1>calendar_tool</span>]</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L27\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;27\&quot;></td>\n          <td id=\&quot;file-agent-py-LC27\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>tool_node</span> <span class=pl-c1>=</span> <span class=pl-en>ToolNode</span>(<span class=pl-s1>all_tools</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L28\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;28\&quot;></td>\n          <td id=\&quot;file-agent-py-LC28\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L29\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;29\&quot;></td>\n          <td id=\&quot;file-agent-py-LC29\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-c># define the graph</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L30\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;30\&quot;></td>\n          <td id=\&quot;file-agent-py-LC30\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>workflow</span> <span class=pl-c1>=</span> <span class=pl-en>StateGraph</span>(<span class=pl-v>AgentState</span>) </td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L31\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;31\&quot;></td>\n          <td id=\&quot;file-agent-py-LC31\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L32\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;32\&quot;></td>\n          <td id=\&quot;file-agent-py-LC32\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-c># get the model</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L33\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;33\&quot;></td>\n          <td id=\&quot;file-agent-py-LC33\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-k>def</span> <span class=pl-en>_get_model</span>():</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L34\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;34\&quot;></td>\n          <td id=\&quot;file-agent-py-LC34\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s1>model</span> <span class=pl-c1>=</span> <span class=pl-en>ChatGoogleGenerativeAI</span>(<span class=pl-s1>model</span><span class=pl-c1>=</span><span class=pl-s>&amp;quot;gemini-2.0-flash-001&amp;quot;</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L35\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;35\&quot;></td>\n          <td id=\&quot;file-agent-py-LC35\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s1>model</span> <span class=pl-c1>=</span> <span class=pl-s1>model</span>.<span class=pl-c1>bind_tools</span>(<span class=pl-s1>all_tools</span>) <span class=pl-c># bind the tools</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L36\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;36\&quot;></td>\n          <td id=\&quot;file-agent-py-LC36\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-k>return</span> <span class=pl-s1>model</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L37\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;37\&quot;></td>\n          <td id=\&quot;file-agent-py-LC37\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L38\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;38\&quot;></td>\n          <td id=\&quot;file-agent-py-LC38\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-c># invoke the model</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L39\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;39\&quot;></td>\n          <td id=\&quot;file-agent-py-LC39\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>system_prompt</span> <span class=pl-c1>=</span> <span class=pl-s>&amp;quot;You are such a nice helpful bot&amp;quot;</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L40\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;40\&quot;></td>\n          <td id=\&quot;file-agent-py-LC40\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-k>def</span> <span class=pl-en>call_model</span>(<span class=pl-s1>state</span>):</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L41\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;41\&quot;></td>\n          <td id=\&quot;file-agent-py-LC41\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s1>messages</span> <span class=pl-c1>=</span> <span class=pl-s1>state</span>[<span class=pl-s>&amp;quot;messages&amp;quot;</span>]</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L42\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;42\&quot;></td>\n          <td id=\&quot;file-agent-py-LC42\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    </td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L43\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;43\&quot;></td>\n          <td id=\&quot;file-agent-py-LC43\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-c># add the system message</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L44\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;44\&quot;></td>\n          <td id=\&quot;file-agent-py-LC44\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s1>system_message</span> <span class=pl-c1>=</span> <span class=pl-en>SystemMessage</span>(<span class=pl-s1>content</span><span class=pl-c1>=</span><span class=pl-s1>system_prompt</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L45\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;45\&quot;></td>\n          <td id=\&quot;file-agent-py-LC45\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s1>full_messages</span> <span class=pl-c1>=</span> [<span class=pl-s1>system_message</span>] <span class=pl-c1>+</span> <span class=pl-s1>messages</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L46\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;46\&quot;></td>\n          <td id=\&quot;file-agent-py-LC46\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    </td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L47\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;47\&quot;></td>\n          <td id=\&quot;file-agent-py-LC47\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s1>model</span> <span class=pl-c1>=</span> <span class=pl-en>_get_model</span>()</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L48\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;48\&quot;></td>\n          <td id=\&quot;file-agent-py-LC48\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s1>response</span> <span class=pl-c1>=</span> <span class=pl-s1>model</span>.<span class=pl-c1>invoke</span>(<span class=pl-s1>full_messages</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L49\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;49\&quot;></td>\n          <td id=\&quot;file-agent-py-LC49\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    </td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L50\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;50\&quot;></td>\n          <td id=\&quot;file-agent-py-LC50\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-k>return</span> {<span class=pl-s>&amp;quot;messages&amp;quot;</span>: [<span class=pl-s1>response</span>]}</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L51\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;51\&quot;></td>\n          <td id=\&quot;file-agent-py-LC51\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L52\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;52\&quot;></td>\n          <td id=\&quot;file-agent-py-LC52\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-c># define nodes</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L53\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;53\&quot;></td>\n          <td id=\&quot;file-agent-py-LC53\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>workflow</span>.<span class=pl-c1>add_node</span>(<span class=pl-s>&amp;quot;agent&amp;quot;</span>, <span class=pl-s1>call_model</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L54\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;54\&quot;></td>\n          <td id=\&quot;file-agent-py-LC54\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>workflow</span>.<span class=pl-c1>add_node</span>(<span class=pl-s>&amp;quot;tools&amp;quot;</span>, <span class=pl-s1>tool_node</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L55\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;55\&quot;></td>\n          <td id=\&quot;file-agent-py-LC55\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L56\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;56\&quot;></td>\n          <td id=\&quot;file-agent-py-LC56\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-c># entry point</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L57\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;57\&quot;></td>\n          <td id=\&quot;file-agent-py-LC57\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>workflow</span>.<span class=pl-c1>set_entry_point</span>(<span class=pl-s>&amp;quot;agent&amp;quot;</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L58\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;58\&quot;></td>\n          <td id=\&quot;file-agent-py-LC58\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L59\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;59\&quot;></td>\n          <td id=\&quot;file-agent-py-LC59\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-c># define the functionality that determines whether to continue or not</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L60\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;60\&quot;></td>\n          <td id=\&quot;file-agent-py-LC60\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-k>def</span> <span class=pl-en>should_continue</span>(<span class=pl-s1>state</span>):</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L61\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;61\&quot;></td>\n          <td id=\&quot;file-agent-py-LC61\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s1>messages</span> <span class=pl-c1>=</span> <span class=pl-s1>state</span>[<span class=pl-s>&amp;quot;messages&amp;quot;</span>]</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L62\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;62\&quot;></td>\n          <td id=\&quot;file-agent-py-LC62\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s1>last_message</span> <span class=pl-c1>=</span> <span class=pl-s1>messages</span>[<span class=pl-c1>-</span><span class=pl-c1>1</span>]</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L63\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;63\&quot;></td>\n          <td id=\&quot;file-agent-py-LC63\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-c># if there are no tool calls, then we finish</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L64\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;64\&quot;></td>\n          <td id=\&quot;file-agent-py-LC64\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-k>if</span> <span class=pl-c1>not</span> <span class=pl-s1>last_message</span>.<span class=pl-c1>tool_calls</span>:</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L65\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;65\&quot;></td>\n          <td id=\&quot;file-agent-py-LC65\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>        <span class=pl-k>return</span> <span class=pl-s>&amp;quot;end&amp;quot;</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L66\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;66\&quot;></td>\n          <td id=\&quot;file-agent-py-LC66\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-c># if there is, we continue</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L67\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;67\&quot;></td>\n          <td id=\&quot;file-agent-py-LC67\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-k>else</span>:</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L68\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;68\&quot;></td>\n          <td id=\&quot;file-agent-py-LC68\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>        <span class=pl-k>return</span> <span class=pl-s>&amp;quot;continue&amp;quot;</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L69\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;69\&quot;></td>\n          <td id=\&quot;file-agent-py-LC69\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L70\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;70\&quot;></td>\n          <td id=\&quot;file-agent-py-LC70\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>workflow</span>.<span class=pl-c1>add_conditional_edges</span>(</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L71\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;71\&quot;></td>\n          <td id=\&quot;file-agent-py-LC71\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s>&amp;quot;agent&amp;quot;</span>,</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L72\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;72\&quot;></td>\n          <td id=\&quot;file-agent-py-LC72\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    <span class=pl-s1>should_continue</span>,</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L73\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;73\&quot;></td>\n          <td id=\&quot;file-agent-py-LC73\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    {</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L74\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;74\&quot;></td>\n          <td id=\&quot;file-agent-py-LC74\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>        <span class=pl-s>&amp;quot;continue&amp;quot;</span>: <span class=pl-s>&amp;quot;tools&amp;quot;</span>,</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L75\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;75\&quot;></td>\n          <td id=\&quot;file-agent-py-LC75\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>        <span class=pl-s>&amp;quot;end&amp;quot;</span>: <span class=pl-c1>END</span>,</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L76\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;76\&quot;></td>\n          <td id=\&quot;file-agent-py-LC76\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>    },</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L77\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;77\&quot;></td>\n          <td id=\&quot;file-agent-py-LC77\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L78\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;78\&quot;></td>\n          <td id=\&quot;file-agent-py-LC78\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L79\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;79\&quot;></td>\n          <td id=\&quot;file-agent-py-LC79\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-c># connect tools back to agent</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L80\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;80\&quot;></td>\n          <td id=\&quot;file-agent-py-LC80\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>workflow</span>.<span class=pl-c1>add_edge</span>(<span class=pl-s>&amp;quot;tools&amp;quot;</span>, <span class=pl-s>&amp;quot;agent&amp;quot;</span>)</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L81\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;81\&quot;></td>\n          <td id=\&quot;file-agent-py-LC81\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;>\n</td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L82\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;82\&quot;></td>\n          <td id=\&quot;file-agent-py-LC82\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-c># compile</span></td>\n        </tr>\n        <tr>\n          <td id=\&quot;file-agent-py-L83\&quot; class=\&quot;blob-num js-line-number js-blob-rnum\&quot; data-line-number=\&quot;83\&quot;></td>\n          <td id=\&quot;file-agent-py-LC83\&quot; class=\&quot;blob-code blob-code-inner js-file-line\&quot;><span class=pl-s1>graph</span> <span class=pl-c1>=</span> <span class=pl-s1>workflow</span>.<span class=pl-c1>compile</span>()</td>\n        </tr>\n  </table>\n</div>\n\n\n    </div>\n\n  </div>\n</div>\n\n      </div>\n      <div class=\&quot;gist-meta\&quot;>\n        <a href=/__u/howtouseai.substack.com/%22https://gist.github.com/ilsilfverskiold/df86af37010feeea8223fec0039ea6ff/raw/ee12f9e6d18156f61214bf8b97848ee79d456e29/agent.py/%22 style=\&quot;float:right\&quot; class=\&quot;Link--inTextBlock\&quot;>view raw</a>\n        <a href=/__u/howtouseai.substack.com/%22https://gist.github.com/ilsilfverskiold/df86af37010feeea8223fec0039ea6ff#file-agent-py\%22 class=\&quot;Link--inTextBlock\&quot;>\n          agent.py\n        </a>\n        hosted with &amp;#10084; by <a class=\&quot;Link--inTextBlock\&quot; href=/__u/howtouseai.substack.com/%22https://github.com/%22>GitHub</a>\n      </div>\n    </div>\n</div>\n&quot;,&quot;stylesheet&quot;:&quot;https://github.githubassets.com/assets/gist-embed-04c27bb90e5b.css&quot;}" data-component-name="GitgistToDOM"><link rel="stylesheet" href="https://github.githubassets.com/assets/gist-embed-04c27bb90e5b.css"><div id="gist137081247" class="gist">
    <div class="gist-file" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        <div class="js-gist-file-update-container js-task-list-container">
  <div id="file-agent-py" class="file my-2">
    
    <div itemprop="text" class="Box-body p-0 blob-wrapper data type-python  " style="overflow:auto">

        
<div class="js-check-bidi js-blob-code-container blob-code-content">

  
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  
    

    <span>
      This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="/__u/howtouseai.substack.com/%7B%7B%20revealButtonHref%20%7D%7D" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div>

  <span data-view-component="true" class="line-alert tooltipped tooltipped-e">
    
    

</span>

  <table data-hpc="" class="highlight tab-size js-file-line-container" data-tab-size="8" data-paste-markdown-skip="" data-tagsearch-path="agent.py">
        <tbody><tr>
          <td id="file-agent-py-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-agent-py-LC1" class="blob-code blob-code-inner js-file-line"><span class="pl-k">from</span> <span class="pl-s1">langchain_google_genai</span> <span class="pl-k">import</span> <span class="pl-v">ChatGoogleGenerativeAI</span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-agent-py-LC2" class="blob-code blob-code-inner js-file-line"><span class="pl-k">from</span> <span class="pl-s1">langgraph</span>.<span class="pl-s1">graph</span> <span class="pl-k">import</span> <span class="pl-v">StateGraph</span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-agent-py-LC3" class="blob-code blob-code-inner js-file-line"><span class="pl-k">from</span> <span class="pl-s1">langgraph</span>.<span class="pl-s1">graph</span> <span class="pl-k">import</span> <span class="pl-s1">add_messages</span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-agent-py-LC4" class="blob-code blob-code-inner js-file-line"><span class="pl-k">from</span> <span class="pl-s1">langchain_core</span>.<span class="pl-s1">messages</span> <span class="pl-k">import</span> <span class="pl-v">BaseMessage</span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-agent-py-LC5" class="blob-code blob-code-inner js-file-line"><span class="pl-k">from</span> <span class="pl-s1">langchain_core</span>.<span class="pl-s1">messages</span> <span class="pl-k">import</span> <span class="pl-v">SystemMessage</span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
          <td id="file-agent-py-LC6" class="blob-code blob-code-inner js-file-line"><span class="pl-k">from</span> <span class="pl-s1">typing</span> <span class="pl-k">import</span> <span class="pl-v">TypedDict</span>, <span class="pl-v">Annotated</span>, <span class="pl-v">Sequence</span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
          <td id="file-agent-py-LC7" class="blob-code blob-code-inner js-file-line"><span class="pl-k">from</span> <span class="pl-s1">langgraph</span>.<span class="pl-s1">prebuilt</span> <span class="pl-k">import</span> <span class="pl-v">ToolNode</span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
          <td id="file-agent-py-LC8" class="blob-code blob-code-inner js-file-line"><span class="pl-k">from</span> <span class="pl-s1">langchain_core</span>.<span class="pl-s1">tools</span> <span class="pl-k">import</span> <span class="pl-s1">tool</span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
          <td id="file-agent-py-LC9" class="blob-code blob-code-inner js-file-line"><span class="pl-k">from</span> <span class="pl-s1">langgraph</span>.<span class="pl-s1">graph</span> <span class="pl-k">import</span> <span class="pl-c1">END</span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
          <td id="file-agent-py-LC10" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-agent-py-L11" class="blob-num js-line-number js-blob-rnum" data-line-number="11"></td>
          <td id="file-agent-py-LC11" class="blob-code blob-code-inner js-file-line"><span class="pl-c"># define the state schema</span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L12" class="blob-num js-line-number js-blob-rnum" data-line-number="12"></td>
          <td id="file-agent-py-LC12" class="blob-code blob-code-inner js-file-line"><span class="pl-k">class</span> <span class="pl-v">AgentState</span>(<span class="pl-v">TypedDict</span>):</td>
        </tr>
        <tr>
          <td id="file-agent-py-L13" class="blob-num js-line-number js-blob-rnum" data-line-number="13"></td>
          <td id="file-agent-py-LC13" class="blob-code blob-code-inner js-file-line">    <span class="pl-s1">messages</span>: <span class="pl-v">Annotated</span>[<span class="pl-v">Sequence</span>[<span class="pl-smi">BaseMessage</span>], <span class="pl-smi">add_messages</span>]</td>
        </tr>
        <tr>
          <td id="file-agent-py-L14" class="blob-num js-line-number js-blob-rnum" data-line-number="14"></td>
          <td id="file-agent-py-LC14" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-agent-py-L15" class="blob-num js-line-number js-blob-rnum" data-line-number="15"></td>
          <td id="file-agent-py-LC15" class="blob-code blob-code-inner js-file-line"><span class="pl-c"># define the tools available</span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L16" class="blob-num js-line-number js-blob-rnum" data-line-number="16"></td>
          <td id="file-agent-py-LC16" class="blob-code blob-code-inner js-file-line"><span class="pl-en">@<span class="pl-s1">tool</span></span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L17" class="blob-num js-line-number js-blob-rnum" data-line-number="17"></td>
          <td id="file-agent-py-LC17" class="blob-code blob-code-inner js-file-line"><span class="pl-k">def</span> <span class="pl-en">weather_tool</span>() <span class="pl-c1">-&gt;</span> <span class="pl-smi">str</span>:</td>
        </tr>
        <tr>
          <td id="file-agent-py-L18" class="blob-num js-line-number js-blob-rnum" data-line-number="18"></td>
          <td id="file-agent-py-LC18" class="blob-code blob-code-inner js-file-line">    <span class="pl-s">"""Get the current weather."""</span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L19" class="blob-num js-line-number js-blob-rnum" data-line-number="19"></td>
          <td id="file-agent-py-LC19" class="blob-code blob-code-inner js-file-line">    <span class="pl-k">return</span> <span class="pl-s">"Weather is 19&#176;C and Partly Cloudy"</span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L20" class="blob-num js-line-number js-blob-rnum" data-line-number="20"></td>
          <td id="file-agent-py-LC20" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-agent-py-L21" class="blob-num js-line-number js-blob-rnum" data-line-number="21"></td>
          <td id="file-agent-py-LC21" class="blob-code blob-code-inner js-file-line"><span class="pl-en">@<span class="pl-s1">tool</span></span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L22" class="blob-num js-line-number js-blob-rnum" data-line-number="22"></td>
          <td id="file-agent-py-LC22" class="blob-code blob-code-inner js-file-line"><span class="pl-k">def</span> <span class="pl-en">calendar_tool</span>() <span class="pl-c1">-&gt;</span> <span class="pl-smi">str</span>:</td>
        </tr>
        <tr>
          <td id="file-agent-py-L23" class="blob-num js-line-number js-blob-rnum" data-line-number="23"></td>
          <td id="file-agent-py-LC23" class="blob-code blob-code-inner js-file-line">    <span class="pl-s">"""Check your calendar for meetings on a specific date."""</span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L24" class="blob-num js-line-number js-blob-rnum" data-line-number="24"></td>
          <td id="file-agent-py-LC24" class="blob-code blob-code-inner js-file-line">    <span class="pl-k">return</span> <span class="pl-s">"No meetings scheduled"</span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L25" class="blob-num js-line-number js-blob-rnum" data-line-number="25"></td>
          <td id="file-agent-py-LC25" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-agent-py-L26" class="blob-num js-line-number js-blob-rnum" data-line-number="26"></td>
          <td id="file-agent-py-LC26" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">all_tools</span> <span class="pl-c1">=</span> [<span class="pl-s1">weather_tool</span>, <span class="pl-s1">calendar_tool</span>]</td>
        </tr>
        <tr>
          <td id="file-agent-py-L27" class="blob-num js-line-number js-blob-rnum" data-line-number="27"></td>
          <td id="file-agent-py-LC27" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">tool_node</span> <span class="pl-c1">=</span> <span class="pl-en">ToolNode</span>(<span class="pl-s1">all_tools</span>)</td>
        </tr>
        <tr>
          <td id="file-agent-py-L28" class="blob-num js-line-number js-blob-rnum" data-line-number="28"></td>
          <td id="file-agent-py-LC28" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-agent-py-L29" class="blob-num js-line-number js-blob-rnum" data-line-number="29"></td>
          <td id="file-agent-py-LC29" class="blob-code blob-code-inner js-file-line"><span class="pl-c"># define the graph</span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L30" class="blob-num js-line-number js-blob-rnum" data-line-number="30"></td>
          <td id="file-agent-py-LC30" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">workflow</span> <span class="pl-c1">=</span> <span class="pl-en">StateGraph</span>(<span class="pl-v">AgentState</span>) </td>
        </tr>
        <tr>
          <td id="file-agent-py-L31" class="blob-num js-line-number js-blob-rnum" data-line-number="31"></td>
          <td id="file-agent-py-LC31" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-agent-py-L32" class="blob-num js-line-number js-blob-rnum" data-line-number="32"></td>
          <td id="file-agent-py-LC32" class="blob-code blob-code-inner js-file-line"><span class="pl-c"># get the model</span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L33" class="blob-num js-line-number js-blob-rnum" data-line-number="33"></td>
          <td id="file-agent-py-LC33" class="blob-code blob-code-inner js-file-line"><span class="pl-k">def</span> <span class="pl-en">_get_model</span>():</td>
        </tr>
        <tr>
          <td id="file-agent-py-L34" class="blob-num js-line-number js-blob-rnum" data-line-number="34"></td>
          <td id="file-agent-py-LC34" class="blob-code blob-code-inner js-file-line">    <span class="pl-s1">model</span> <span class="pl-c1">=</span> <span class="pl-en">ChatGoogleGenerativeAI</span>(<span class="pl-s1">model</span><span class="pl-c1">=</span><span class="pl-s">"gemini-2.0-flash-001"</span>)</td>
        </tr>
        <tr>
          <td id="file-agent-py-L35" class="blob-num js-line-number js-blob-rnum" data-line-number="35"></td>
          <td id="file-agent-py-LC35" class="blob-code blob-code-inner js-file-line">    <span class="pl-s1">model</span> <span class="pl-c1">=</span> <span class="pl-s1">model</span>.<span class="pl-c1">bind_tools</span>(<span class="pl-s1">all_tools</span>) <span class="pl-c"># bind the tools</span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L36" class="blob-num js-line-number js-blob-rnum" data-line-number="36"></td>
          <td id="file-agent-py-LC36" class="blob-code blob-code-inner js-file-line">    <span class="pl-k">return</span> <span class="pl-s1">model</span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L37" class="blob-num js-line-number js-blob-rnum" data-line-number="37"></td>
          <td id="file-agent-py-LC37" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-agent-py-L38" class="blob-num js-line-number js-blob-rnum" data-line-number="38"></td>
          <td id="file-agent-py-LC38" class="blob-code blob-code-inner js-file-line"><span class="pl-c"># invoke the model</span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L39" class="blob-num js-line-number js-blob-rnum" data-line-number="39"></td>
          <td id="file-agent-py-LC39" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">system_prompt</span> <span class="pl-c1">=</span> <span class="pl-s">"You are such a nice helpful bot"</span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L40" class="blob-num js-line-number js-blob-rnum" data-line-number="40"></td>
          <td id="file-agent-py-LC40" class="blob-code blob-code-inner js-file-line"><span class="pl-k">def</span> <span class="pl-en">call_model</span>(<span class="pl-s1">state</span>):</td>
        </tr>
        <tr>
          <td id="file-agent-py-L41" class="blob-num js-line-number js-blob-rnum" data-line-number="41"></td>
          <td id="file-agent-py-LC41" class="blob-code blob-code-inner js-file-line">    <span class="pl-s1">messages</span> <span class="pl-c1">=</span> <span class="pl-s1">state</span>[<span class="pl-s">"messages"</span>]</td>
        </tr>
        <tr>
          <td id="file-agent-py-L42" class="blob-num js-line-number js-blob-rnum" data-line-number="42"></td>
          <td id="file-agent-py-LC42" class="blob-code blob-code-inner js-file-line">    </td>
        </tr>
        <tr>
          <td id="file-agent-py-L43" class="blob-num js-line-number js-blob-rnum" data-line-number="43"></td>
          <td id="file-agent-py-LC43" class="blob-code blob-code-inner js-file-line">    <span class="pl-c"># add the system message</span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L44" class="blob-num js-line-number js-blob-rnum" data-line-number="44"></td>
          <td id="file-agent-py-LC44" class="blob-code blob-code-inner js-file-line">    <span class="pl-s1">system_message</span> <span class="pl-c1">=</span> <span class="pl-en">SystemMessage</span>(<span class="pl-s1">content</span><span class="pl-c1">=</span><span class="pl-s1">system_prompt</span>)</td>
        </tr>
        <tr>
          <td id="file-agent-py-L45" class="blob-num js-line-number js-blob-rnum" data-line-number="45"></td>
          <td id="file-agent-py-LC45" class="blob-code blob-code-inner js-file-line">    <span class="pl-s1">full_messages</span> <span class="pl-c1">=</span> [<span class="pl-s1">system_message</span>] <span class="pl-c1">+</span> <span class="pl-s1">messages</span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L46" class="blob-num js-line-number js-blob-rnum" data-line-number="46"></td>
          <td id="file-agent-py-LC46" class="blob-code blob-code-inner js-file-line">    </td>
        </tr>
        <tr>
          <td id="file-agent-py-L47" class="blob-num js-line-number js-blob-rnum" data-line-number="47"></td>
          <td id="file-agent-py-LC47" class="blob-code blob-code-inner js-file-line">    <span class="pl-s1">model</span> <span class="pl-c1">=</span> <span class="pl-en">_get_model</span>()</td>
        </tr>
        <tr>
          <td id="file-agent-py-L48" class="blob-num js-line-number js-blob-rnum" data-line-number="48"></td>
          <td id="file-agent-py-LC48" class="blob-code blob-code-inner js-file-line">    <span class="pl-s1">response</span> <span class="pl-c1">=</span> <span class="pl-s1">model</span>.<span class="pl-c1">invoke</span>(<span class="pl-s1">full_messages</span>)</td>
        </tr>
        <tr>
          <td id="file-agent-py-L49" class="blob-num js-line-number js-blob-rnum" data-line-number="49"></td>
          <td id="file-agent-py-LC49" class="blob-code blob-code-inner js-file-line">    </td>
        </tr>
        <tr>
          <td id="file-agent-py-L50" class="blob-num js-line-number js-blob-rnum" data-line-number="50"></td>
          <td id="file-agent-py-LC50" class="blob-code blob-code-inner js-file-line">    <span class="pl-k">return</span> {<span class="pl-s">"messages"</span>: [<span class="pl-s1">response</span>]}</td>
        </tr>
        <tr>
          <td id="file-agent-py-L51" class="blob-num js-line-number js-blob-rnum" data-line-number="51"></td>
          <td id="file-agent-py-LC51" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-agent-py-L52" class="blob-num js-line-number js-blob-rnum" data-line-number="52"></td>
          <td id="file-agent-py-LC52" class="blob-code blob-code-inner js-file-line"><span class="pl-c"># define nodes</span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L53" class="blob-num js-line-number js-blob-rnum" data-line-number="53"></td>
          <td id="file-agent-py-LC53" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">workflow</span>.<span class="pl-c1">add_node</span>(<span class="pl-s">"agent"</span>, <span class="pl-s1">call_model</span>)</td>
        </tr>
        <tr>
          <td id="file-agent-py-L54" class="blob-num js-line-number js-blob-rnum" data-line-number="54"></td>
          <td id="file-agent-py-LC54" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">workflow</span>.<span class="pl-c1">add_node</span>(<span class="pl-s">"tools"</span>, <span class="pl-s1">tool_node</span>)</td>
        </tr>
        <tr>
          <td id="file-agent-py-L55" class="blob-num js-line-number js-blob-rnum" data-line-number="55"></td>
          <td id="file-agent-py-LC55" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-agent-py-L56" class="blob-num js-line-number js-blob-rnum" data-line-number="56"></td>
          <td id="file-agent-py-LC56" class="blob-code blob-code-inner js-file-line"><span class="pl-c"># entry point</span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L57" class="blob-num js-line-number js-blob-rnum" data-line-number="57"></td>
          <td id="file-agent-py-LC57" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">workflow</span>.<span class="pl-c1">set_entry_point</span>(<span class="pl-s">"agent"</span>)</td>
        </tr>
        <tr>
          <td id="file-agent-py-L58" class="blob-num js-line-number js-blob-rnum" data-line-number="58"></td>
          <td id="file-agent-py-LC58" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-agent-py-L59" class="blob-num js-line-number js-blob-rnum" data-line-number="59"></td>
          <td id="file-agent-py-LC59" class="blob-code blob-code-inner js-file-line"><span class="pl-c"># define the functionality that determines whether to continue or not</span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L60" class="blob-num js-line-number js-blob-rnum" data-line-number="60"></td>
          <td id="file-agent-py-LC60" class="blob-code blob-code-inner js-file-line"><span class="pl-k">def</span> <span class="pl-en">should_continue</span>(<span class="pl-s1">state</span>):</td>
        </tr>
        <tr>
          <td id="file-agent-py-L61" class="blob-num js-line-number js-blob-rnum" data-line-number="61"></td>
          <td id="file-agent-py-LC61" class="blob-code blob-code-inner js-file-line">    <span class="pl-s1">messages</span> <span class="pl-c1">=</span> <span class="pl-s1">state</span>[<span class="pl-s">"messages"</span>]</td>
        </tr>
        <tr>
          <td id="file-agent-py-L62" class="blob-num js-line-number js-blob-rnum" data-line-number="62"></td>
          <td id="file-agent-py-LC62" class="blob-code blob-code-inner js-file-line">    <span class="pl-s1">last_message</span> <span class="pl-c1">=</span> <span class="pl-s1">messages</span>[<span class="pl-c1">-</span><span class="pl-c1">1</span>]</td>
        </tr>
        <tr>
          <td id="file-agent-py-L63" class="blob-num js-line-number js-blob-rnum" data-line-number="63"></td>
          <td id="file-agent-py-LC63" class="blob-code blob-code-inner js-file-line">    <span class="pl-c"># if there are no tool calls, then we finish</span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L64" class="blob-num js-line-number js-blob-rnum" data-line-number="64"></td>
          <td id="file-agent-py-LC64" class="blob-code blob-code-inner js-file-line">    <span class="pl-k">if</span> <span class="pl-c1">not</span> <span class="pl-s1">last_message</span>.<span class="pl-c1">tool_calls</span>:</td>
        </tr>
        <tr>
          <td id="file-agent-py-L65" class="blob-num js-line-number js-blob-rnum" data-line-number="65"></td>
          <td id="file-agent-py-LC65" class="blob-code blob-code-inner js-file-line">        <span class="pl-k">return</span> <span class="pl-s">"end"</span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L66" class="blob-num js-line-number js-blob-rnum" data-line-number="66"></td>
          <td id="file-agent-py-LC66" class="blob-code blob-code-inner js-file-line">    <span class="pl-c"># if there is, we continue</span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L67" class="blob-num js-line-number js-blob-rnum" data-line-number="67"></td>
          <td id="file-agent-py-LC67" class="blob-code blob-code-inner js-file-line">    <span class="pl-k">else</span>:</td>
        </tr>
        <tr>
          <td id="file-agent-py-L68" class="blob-num js-line-number js-blob-rnum" data-line-number="68"></td>
          <td id="file-agent-py-LC68" class="blob-code blob-code-inner js-file-line">        <span class="pl-k">return</span> <span class="pl-s">"continue"</span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L69" class="blob-num js-line-number js-blob-rnum" data-line-number="69"></td>
          <td id="file-agent-py-LC69" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-agent-py-L70" class="blob-num js-line-number js-blob-rnum" data-line-number="70"></td>
          <td id="file-agent-py-LC70" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">workflow</span>.<span class="pl-c1">add_conditional_edges</span>(</td>
        </tr>
        <tr>
          <td id="file-agent-py-L71" class="blob-num js-line-number js-blob-rnum" data-line-number="71"></td>
          <td id="file-agent-py-LC71" class="blob-code blob-code-inner js-file-line">    <span class="pl-s">"agent"</span>,</td>
        </tr>
        <tr>
          <td id="file-agent-py-L72" class="blob-num js-line-number js-blob-rnum" data-line-number="72"></td>
          <td id="file-agent-py-LC72" class="blob-code blob-code-inner js-file-line">    <span class="pl-s1">should_continue</span>,</td>
        </tr>
        <tr>
          <td id="file-agent-py-L73" class="blob-num js-line-number js-blob-rnum" data-line-number="73"></td>
          <td id="file-agent-py-LC73" class="blob-code blob-code-inner js-file-line">    {</td>
        </tr>
        <tr>
          <td id="file-agent-py-L74" class="blob-num js-line-number js-blob-rnum" data-line-number="74"></td>
          <td id="file-agent-py-LC74" class="blob-code blob-code-inner js-file-line">        <span class="pl-s">"continue"</span>: <span class="pl-s">"tools"</span>,</td>
        </tr>
        <tr>
          <td id="file-agent-py-L75" class="blob-num js-line-number js-blob-rnum" data-line-number="75"></td>
          <td id="file-agent-py-LC75" class="blob-code blob-code-inner js-file-line">        <span class="pl-s">"end"</span>: <span class="pl-c1">END</span>,</td>
        </tr>
        <tr>
          <td id="file-agent-py-L76" class="blob-num js-line-number js-blob-rnum" data-line-number="76"></td>
          <td id="file-agent-py-LC76" class="blob-code blob-code-inner js-file-line">    },</td>
        </tr>
        <tr>
          <td id="file-agent-py-L77" class="blob-num js-line-number js-blob-rnum" data-line-number="77"></td>
          <td id="file-agent-py-LC77" class="blob-code blob-code-inner js-file-line">)</td>
        </tr>
        <tr>
          <td id="file-agent-py-L78" class="blob-num js-line-number js-blob-rnum" data-line-number="78"></td>
          <td id="file-agent-py-LC78" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-agent-py-L79" class="blob-num js-line-number js-blob-rnum" data-line-number="79"></td>
          <td id="file-agent-py-LC79" class="blob-code blob-code-inner js-file-line"><span class="pl-c"># connect tools back to agent</span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L80" class="blob-num js-line-number js-blob-rnum" data-line-number="80"></td>
          <td id="file-agent-py-LC80" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">workflow</span>.<span class="pl-c1">add_edge</span>(<span class="pl-s">"tools"</span>, <span class="pl-s">"agent"</span>)</td>
        </tr>
        <tr>
          <td id="file-agent-py-L81" class="blob-num js-line-number js-blob-rnum" data-line-number="81"></td>
          <td id="file-agent-py-LC81" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-agent-py-L82" class="blob-num js-line-number js-blob-rnum" data-line-number="82"></td>
          <td id="file-agent-py-LC82" class="blob-code blob-code-inner js-file-line"><span class="pl-c"># compile</span></td>
        </tr>
        <tr>
          <td id="file-agent-py-L83" class="blob-num js-line-number js-blob-rnum" data-line-number="83"></td>
          <td id="file-agent-py-LC83" class="blob-code blob-code-inner js-file-line"><span class="pl-s1">graph</span> <span class="pl-c1">=</span> <span class="pl-s1">workflow</span>.<span class="pl-c1">compile</span>()</td>
        </tr>
  </tbody></table>
</div>


    </div>

  </div>
</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/ilsilfverskiold/df86af37010feeea8223fec0039ea6ff/raw/ee12f9e6d18156f61214bf8b97848ee79d456e29/agent.py" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/ilsilfverskiold/df86af37010feeea8223fec0039ea6ff#file-agent-py" class="Link--inTextBlock">
          agent.py
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>
</div><p>Now, this is a very simple workflow we&#8217;ve gone through&#8212;just for demonstration. To build a multi-agent workflow, you&#8217;ll need to add to the state and then add more nodes and conditional logic to the graph.</p><h3>Testing the workflow</h3><p>To test this workflow, open up LangGraph Studio, connect to LangSmith (free to create an account), turn on Docker Desktop (make sure it's the latest version), and then open the langgraph_example project.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!x-zH!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66425561-31f6-419c-838b-e917687c708d_2551x1610.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!x-zH!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66425561-31f6-419c-838b-e917687c708d_2551x1610.png 424w, /__u/substackcdn.com/image/fetch/$s_!x-zH!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66425561-31f6-419c-838b-e917687c708d_2551x1610.png 848w, /__u/substackcdn.com/image/fetch/$s_!x-zH!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66425561-31f6-419c-838b-e917687c708d_2551x1610.png 1272w, /__u/substackcdn.com/image/fetch/$s_!x-zH!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66425561-31f6-419c-838b-e917687c708d_2551x1610.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!x-zH!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66425561-31f6-419c-838b-e917687c708d_2551x1610.png" width="1456" height="919" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/66425561-31f6-419c-838b-e917687c708d_2551x1610.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:919,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:361630,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://howtouseai.substack.com/i/159820961?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66425561-31f6-419c-838b-e917687c708d_2551x1610.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!x-zH!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66425561-31f6-419c-838b-e917687c708d_2551x1610.png 424w, /__u/substackcdn.com/image/fetch/$s_!x-zH!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66425561-31f6-419c-838b-e917687c708d_2551x1610.png 848w, /__u/substackcdn.com/image/fetch/$s_!x-zH!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66425561-31f6-419c-838b-e917687c708d_2551x1610.png 1272w, /__u/substackcdn.com/image/fetch/$s_!x-zH!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F66425561-31f6-419c-838b-e917687c708d_2551x1610.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Once it&#8217;s loaded, you&#8217;ll be able to add a human message and submit it to see what happens.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!zZ01!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc11d8141-0fff-48d4-a1ac-b6d533b95f9b_2546x1599.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!zZ01!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc11d8141-0fff-48d4-a1ac-b6d533b95f9b_2546x1599.png 424w, /__u/substackcdn.com/image/fetch/$s_!zZ01!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc11d8141-0fff-48d4-a1ac-b6d533b95f9b_2546x1599.png 848w, /__u/substackcdn.com/image/fetch/$s_!zZ01!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc11d8141-0fff-48d4-a1ac-b6d533b95f9b_2546x1599.png 1272w, /__u/substackcdn.com/image/fetch/$s_!zZ01!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc11d8141-0fff-48d4-a1ac-b6d533b95f9b_2546x1599.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!zZ01!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc11d8141-0fff-48d4-a1ac-b6d533b95f9b_2546x1599.png" width="1456" height="914" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c11d8141-0fff-48d4-a1ac-b6d533b95f9b_2546x1599.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:914,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:444387,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://howtouseai.substack.com/i/159820961?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc11d8141-0fff-48d4-a1ac-b6d533b95f9b_2546x1599.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!zZ01!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc11d8141-0fff-48d4-a1ac-b6d533b95f9b_2546x1599.png 424w, /__u/substackcdn.com/image/fetch/$s_!zZ01!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc11d8141-0fff-48d4-a1ac-b6d533b95f9b_2546x1599.png 848w, /__u/substackcdn.com/image/fetch/$s_!zZ01!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc11d8141-0fff-48d4-a1ac-b6d533b95f9b_2546x1599.png 1272w, /__u/substackcdn.com/image/fetch/$s_!zZ01!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc11d8141-0fff-48d4-a1ac-b6d533b95f9b_2546x1599.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Remember, it only has those two mock tools available, so it won&#8217;t be terribly useful.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="/__u/substackcdn.com/image/fetch/$s_!WFMf!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe059d23-79d9-4cfd-a1b1-d47452b234f0_2866x1714.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="/__u/substackcdn.com/image/fetch/$s_!WFMf!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe059d23-79d9-4cfd-a1b1-d47452b234f0_2866x1714.png 424w, /__u/substackcdn.com/image/fetch/$s_!WFMf!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe059d23-79d9-4cfd-a1b1-d47452b234f0_2866x1714.png 848w, /__u/substackcdn.com/image/fetch/$s_!WFMf!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe059d23-79d9-4cfd-a1b1-d47452b234f0_2866x1714.png 1272w, /__u/substackcdn.com/image/fetch/$s_!WFMf!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_webp, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe059d23-79d9-4cfd-a1b1-d47452b234f0_2866x1714.png 1456w" sizes="100vw"><img src="/__u/substackcdn.com/image/fetch/$s_!WFMf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe059d23-79d9-4cfd-a1b1-d47452b234f0_2866x1714.png" width="1456" height="871" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fe059d23-79d9-4cfd-a1b1-d47452b234f0_2866x1714.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:871,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:318790,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://howtouseai.substack.com/i/159820961?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe059d23-79d9-4cfd-a1b1-d47452b234f0_2866x1714.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="/__u/substackcdn.com/image/fetch/$s_!WFMf!, /__u/howtouseai.substack.com/w_424, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe059d23-79d9-4cfd-a1b1-d47452b234f0_2866x1714.png 424w, /__u/substackcdn.com/image/fetch/$s_!WFMf!, /__u/howtouseai.substack.com/w_848, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe059d23-79d9-4cfd-a1b1-d47452b234f0_2866x1714.png 848w, /__u/substackcdn.com/image/fetch/$s_!WFMf!, /__u/howtouseai.substack.com/w_1272, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe059d23-79d9-4cfd-a1b1-d47452b234f0_2866x1714.png 1272w, /__u/substackcdn.com/image/fetch/$s_!WFMf!, /__u/howtouseai.substack.com/w_1456, /__u/howtouseai.substack.com/c_limit, /__u/howtouseai.substack.com/f_auto, /__u/howtouseai.substack.com/q_auto:good, /__u/howtouseai.substack.com/fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffe059d23-79d9-4cfd-a1b1-d47452b234f0_2866x1714.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>You can see in my example that I first asked it to check the weather and my calendar, and then got the final response at the end.</p><p>I would keep playing around with it until these concepts stick or do some research around how everything works to get more comfortable with it. As a suggestion, you can start setting up real tools that check the news (or actual weather) in the workflow above.</p><p>Hopefully, you learned something&#8212;if LangGraph is new to you. Feel free to check out more of my work on <a href="https://towardsdatascience.com/author/ilsilfverskiold/">TDS</a> and <a href="https://medium.com/@ilsilfverskiold">Medium</a> as well.</p><p>&#128536;</p><p></p>]]></content:encoded></item></channel></rss>