<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Mostafa Hanafy</title>
    <description>The latest articles on Forem by Mostafa Hanafy (@mostafa_hanafy_768dae8d65).</description>
    <link>https://forem.com/mostafa_hanafy_768dae8d65</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3790159%2F9c7f71cc-9c86-46c3-8f72-53bb8b960953.jpg</url>
      <title>Forem: Mostafa Hanafy</title>
      <link>https://forem.com/mostafa_hanafy_768dae8d65</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mostafa_hanafy_768dae8d65"/>
    <language>en</language>
    <item>
      <title>Prevent Token Cost Spikes in LLM Apps with Token Budget Guard</title>
      <dc:creator>Mostafa Hanafy</dc:creator>
      <pubDate>Wed, 11 Mar 2026 06:53:26 +0000</pubDate>
      <link>https://forem.com/mostafa_hanafy_768dae8d65/prevent-token-cost-spikes-in-llm-apps-with-token-budget-guard-1b9g</link>
      <guid>https://forem.com/mostafa_hanafy_768dae8d65/prevent-token-cost-spikes-in-llm-apps-with-token-budget-guard-1b9g</guid>
      <description>&lt;p&gt;When building LLM features, token usage directly affects three things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;cost&lt;/li&gt;
&lt;li&gt;latency&lt;/li&gt;
&lt;li&gt;reliability&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But many applications treat token usage as an afterthought until prompts grow unexpectedly or API costs spike.&lt;/p&gt;

&lt;p&gt;I recently released an open-source utility called Token Budget Guard to help solve this.&lt;/p&gt;

&lt;p&gt;The idea is simple: enforce token limits before making expensive LLM API calls.&lt;/p&gt;

&lt;p&gt;Instead of sending a request blindly to a provider, you can apply guardrails such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;fail fast if the request exceeds a limit&lt;/li&gt;
&lt;li&gt;automatically trim context&lt;/li&gt;
&lt;li&gt;warn when the request goes over budget&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;withTokenBudget&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;token-budget-guard&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;withTokenBudget&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;maxTokens&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;expectedOutputTokens&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;trim_context&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;call&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;aiClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This helps keep AI systems predictable as prompts and context grow over time.&lt;/p&gt;

&lt;p&gt;The library also includes provider adapters for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;OpenAI&lt;/li&gt;
&lt;li&gt;Anthropic&lt;/li&gt;
&lt;li&gt;Gemini&lt;/li&gt;
&lt;li&gt;AWS Bedrock&lt;/li&gt;
&lt;li&gt;Azure OpenAI&lt;/li&gt;
&lt;li&gt;Cohere&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It’s intentionally small and focused so it can fit easily into existing AI pipelines.&lt;/p&gt;

&lt;p&gt;GitHub &lt;a href="https://github.com/mostafasayed/token-budget-guard" rel="noopener noreferrer"&gt;https://github.com/mostafasayed/token-budget-guard&lt;/a&gt;&lt;br&gt;
npm &lt;a href="https://www.npmjs.com/package/token-budget-guard" rel="noopener noreferrer"&gt;https://www.npmjs.com/package/token-budget-guard&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you're building production AI systems, I'm curious how you're managing token budgets today.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>webdev</category>
      <category>node</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Stop Overengineering Your TypeScript Starters</title>
      <dc:creator>Mostafa Hanafy</dc:creator>
      <pubDate>Tue, 24 Feb 2026 19:01:21 +0000</pubDate>
      <link>https://forem.com/mostafa_hanafy_768dae8d65/stop-overengineering-your-typescript-starters-5d8m</link>
      <guid>https://forem.com/mostafa_hanafy_768dae8d65/stop-overengineering-your-typescript-starters-5d8m</guid>
      <description>&lt;p&gt;If you build TypeScript projects regularly, you’ve probably experienced this:&lt;br&gt;
You just want to start a simple project…&lt;br&gt;
But instead you get:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;30+ config files&lt;/li&gt;
&lt;li&gt;Opinionated architecture&lt;/li&gt;
&lt;li&gt;Framework-specific setup&lt;/li&gt;
&lt;li&gt;Tools you don’t need&lt;/li&gt;
&lt;li&gt;Build complexity before writing code&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What should take 10 seconds takes 10 minutes.&lt;/p&gt;


&lt;h2&gt;
  
  
  The Problem With Most TypeScript Starters
&lt;/h2&gt;

&lt;p&gt;Many templates are built with good intentions.&lt;/p&gt;

&lt;p&gt;But they often:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Assume enterprise-level needs&lt;/li&gt;
&lt;li&gt;Add premature abstraction&lt;/li&gt;
&lt;li&gt;Lock you into tooling choices&lt;/li&gt;
&lt;li&gt;Introduce unnecessary boilerplate&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For small libraries, APIs, CLI tools, or experiments, this is overkill.&lt;/p&gt;


&lt;h2&gt;
  
  
  What I Actually Needed
&lt;/h2&gt;

&lt;p&gt;Most of the time, I just want:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;TypeScript configured correctly&lt;/li&gt;
&lt;li&gt;A clean src/index.ts&lt;/li&gt;
&lt;li&gt;tsx for fast development&lt;/li&gt;
&lt;li&gt;Optional ESLint / Prettier&lt;/li&gt;
&lt;li&gt;ESM or CommonJS choice&lt;/li&gt;
&lt;li&gt;Minimal scripts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;No frameworks. No magic.&lt;/p&gt;

&lt;p&gt;Just a clean starting point.&lt;/p&gt;


&lt;h2&gt;
  
  
  So I Built a Minimal CLI
&lt;/h2&gt;

&lt;p&gt;I created &lt;code&gt;ts-package-init&lt;/code&gt;. a small TypeScript project initializer focused on simplicity.&lt;/p&gt;

&lt;p&gt;It scaffolds:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Base TypeScript projects&lt;/li&gt;
&lt;li&gt;Libraries&lt;/li&gt;
&lt;li&gt;Backends&lt;/li&gt;
&lt;li&gt;CLI tools&lt;/li&gt;
&lt;li&gt;Monorepos&lt;/li&gt;
&lt;li&gt;Minimal NestJS or Moleculer presets&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All with sensible defaults.&lt;/p&gt;


&lt;h2&gt;
  
  
  Quick Example
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx ts-package-init my-app
cd my-app
npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;That’s it.&lt;/p&gt;

&lt;p&gt;You get:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my-app/
  package.json
  tsconfig.json
  src/
    index.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Clean. Minimal. Ready to build on.&lt;/p&gt;




&lt;h2&gt;
  
  
  Optional Features
&lt;/h2&gt;

&lt;p&gt;If you need more:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx ts-package-init api --preset backend --eslint --prettier --esm

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Enable ESLint&lt;/li&gt;
&lt;li&gt;Add Prettier&lt;/li&gt;
&lt;li&gt;Use ESM&lt;/li&gt;
&lt;li&gt;Select package manager&lt;/li&gt;
&lt;li&gt;Skip install&lt;/li&gt;
&lt;li&gt;Use interactive mode&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But nothing is forced.&lt;/p&gt;




&lt;h2&gt;
  
  
  Design Philosophy
&lt;/h2&gt;

&lt;p&gt;The goal is not to compete with full frameworks.&lt;/p&gt;

&lt;p&gt;It’s to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start fast&lt;/li&gt;
&lt;li&gt;Avoid unnecessary complexity&lt;/li&gt;
&lt;li&gt;Keep structure predictable&lt;/li&gt;
&lt;li&gt;Let you scale intentionally&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Minimal first. Add complexity later.&lt;/p&gt;




&lt;h2&gt;
  
  
  v1.0.0
&lt;/h2&gt;

&lt;p&gt;The core scaffolding logic is now stable.&lt;/p&gt;

&lt;p&gt;Future improvements will focus on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Developer experience&lt;/li&gt;
&lt;li&gt;Preset refinement&lt;/li&gt;
&lt;li&gt;Performance&lt;/li&gt;
&lt;li&gt;Real-world feedback&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Try It
&lt;/h2&gt;

&lt;p&gt;GitHub:&lt;br&gt;
&lt;a href="https://github.com/yourusername/ts-package-init" rel="noopener noreferrer"&gt;https://github.com/yourusername/ts-package-init&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;npm:&lt;br&gt;
&lt;a href="https://www.npmjs.com/package/ts-package-init" rel="noopener noreferrer"&gt;https://www.npmjs.com/package/ts-package-init&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you regularly spin up TypeScript projects, I’d genuinely appreciate feedback.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>node</category>
      <category>cli</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
