<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Salima Iddrisu</title>
    <description>The latest articles on DEV Community by Salima Iddrisu (@salima_iddrisu).</description>
    <link>https://dev.to/salima_iddrisu</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4133069%2F95886df3-b76e-4f79-8b7b-bcbac536c7c4.jpg</url>
      <title>DEV Community: Salima Iddrisu</title>
      <link>https://dev.to/salima_iddrisu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/salima_iddrisu"/>
    <language>en</language>
    <item>
      <title>I built SaliBot, my own agentic ChatGPT, with LangGraph, FastAPI and Gemini</title>
      <dc:creator>Salima Iddrisu</dc:creator>
      <pubDate>Wed, 18 Nov 2026 21:38:33 +0000</pubDate>
      <link>https://dev.to/salima_iddrisu/i-built-salibot-my-own-agentic-chatgpt-with-langgraph-fastapi-and-gemini-15hd</link>
      <guid>https://dev.to/salima_iddrisu/i-built-salibot-my-own-agentic-chatgpt-with-langgraph-fastapi-and-gemini-15hd</guid>
      <description>&lt;p&gt;I wanted my own ChatGPT, so I built one and named it after myself: &lt;strong&gt;SaliBot&lt;/strong&gt;, after me, Salima. &lt;/p&gt;

&lt;p&gt;SaliBot is a ChatGPT-style assistant, but it isn't just a chat box in front of an LLM. It's an &lt;strong&gt;agent&lt;/strong&gt;: it decides by itself when to search the web, when to read a document you uploaded, when to save something about you to memory, and when to just answer.&lt;/p&gt;

&lt;p&gt;In this post I'll walk through how it works and the real problems I had to solve along the way.&lt;/p&gt;

&lt;p&gt;I wanted my own ChatGPT, so I built one and named it after myself: &lt;strong&gt;SaliBot&lt;/strong&gt;, after me, Salima. &lt;/p&gt;

&lt;p&gt;SaliBot is a ChatGPT-style assistant, but it isn't just a chat box in front of an LLM. It's an &lt;strong&gt;agent&lt;/strong&gt;: it decides by itself when to search the web, when to read a document you uploaded, when to save something about you to memory, and when to just answer.&lt;/p&gt;

&lt;p&gt;In this post I'll walk through how it works and the real problems I had to solve along the way.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Try it live:&lt;/strong&gt; &lt;a href="https://salibot.onrender.com" rel="noopener noreferrer"&gt;https://salibot.onrender.com&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Demo video:&lt;/strong&gt; &lt;a href="https://drive.google.com/file/d/1hDkR78ij9mJR7JgawCUqbo1yig924Oyk/view?usp=sharing" rel="noopener noreferrer"&gt;https://drive.google.com/file/d/1hDkR78ij9mJR7JgawCUqbo1yig924Oyk/view?usp=sharing&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Code:&lt;/strong&gt; &lt;a href="https://github.com/Iddrisusalima/SaliBot" rel="noopener noreferrer"&gt;https://github.com/Iddrisusalima/SaliBot&lt;/a&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  What SaliBot can do
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Search the web&lt;/strong&gt; for today's news and other time-sensitive questions (Tavily)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chat with your documents (RAG):&lt;/strong&gt; PDF, Word, TXT, Markdown, CSV, and my additions: &lt;strong&gt;Excel, PowerPoint and images&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Read pictures:&lt;/strong&gt; Gemini vision describes an image and copies any text in it&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Long-term memory:&lt;/strong&gt; "Remember that I teach robotics" is saved and recalled later&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Calculator:&lt;/strong&gt; a sandboxed maths tool&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Research papers&lt;/strong&gt; from arXiv, &lt;strong&gt;YouTube video summaries&lt;/strong&gt;, &lt;strong&gt;today's date&lt;/strong&gt;, and &lt;strong&gt;"forget this"&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Streaming answers&lt;/strong&gt;, word by word, with live status chips like &lt;em&gt;"Searching the web"&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Voice input&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Accounts with "Continue with Google"&lt;/strong&gt; or email and password; every user only sees their own chats&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Automatic model fallback&lt;/strong&gt; when Gemini is busy&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pin, rename and delete&lt;/strong&gt; chats from a ⋯ menu&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LangSmith tracing&lt;/strong&gt; for every run&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  The architecture
&lt;/h2&gt;

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

&lt;p&gt;The stack:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Layer&lt;/th&gt;
&lt;th&gt;Tech&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;LLM&lt;/td&gt;
&lt;td&gt;Google Gemini 3.5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Agent&lt;/td&gt;
&lt;td&gt;LangGraph&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Backend&lt;/td&gt;
&lt;td&gt;FastAPI (Server-Sent Events for streaming)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;RAG&lt;/td&gt;
&lt;td&gt;ChromaDB + Gemini embeddings&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Database&lt;/td&gt;
&lt;td&gt;PostgreSQL on Neon (SQLite when running locally) + SQLAlchemy&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Web search&lt;/td&gt;
&lt;td&gt;Tavily&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Observability&lt;/td&gt;
&lt;td&gt;LangSmith&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sign-in&lt;/td&gt;
&lt;td&gt;Google Identity Services + email/password&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hosting&lt;/td&gt;
&lt;td&gt;Docker on Render, CI with GitHub Actions&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;


&lt;h2&gt;
  
  
  The heart of it: a LangGraph agent loop
&lt;/h2&gt;

&lt;p&gt;The agent is a small graph with two nodes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;workflow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;StateGraph&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MessagesState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;workflow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"chatbot"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;chatbot_node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;workflow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"tools"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ToolNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tools&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;workflow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_edge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;START&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"chatbot"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;workflow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_conditional_edges&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"chatbot"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tools_condition&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;workflow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_edge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"tools"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"chatbot"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;The &lt;strong&gt;chatbot&lt;/strong&gt; node sends the conversation (plus a system prompt) to Gemini.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;tools_condition&lt;/code&gt; checks Gemini's reply: did it ask for a tool?&lt;/li&gt;
&lt;li&gt;If yes, the &lt;strong&gt;tools&lt;/strong&gt; node runs it and loops back, so Gemini can read the result.&lt;/li&gt;
&lt;li&gt;If no, the answer goes to the user.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here's that loop in a real run. On the second question it called &lt;strong&gt;two tools at once&lt;/strong&gt;, then wrote one answer from both results:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flj82898arydxa6b4f3pg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flj82898arydxa6b4f3pg.png" alt="Inside the agent: the tools it calls and what comes back" width="728" height="454"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A checkpointer saves the conversation state after every step, so SaliBot remembers earlier messages in the same chat even after a restart. Locally it's a &lt;code&gt;SqliteSaver&lt;/code&gt;; in production it's a &lt;code&gt;PostgresSaver&lt;/code&gt; on the same Neon database as the accounts.&lt;/p&gt;

&lt;p&gt;What I love about this pattern: the tools are just Python functions with a docstring.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nd"&gt;@tool&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;search_uploaded_documents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;RunnableConfig&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""
    Search uploaded documents for relevant information.
    Use this when the user asks about uploaded PDFs, DOCX, TXT, notes, files, or documents.
    Also use it for uploaded Excel sheets, PowerPoint slides, and images.
    """&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;retrieve_from_rag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;thread_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;thread_of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Gemini reads that docstring to decide when to call the tool.&lt;/strong&gt; I added the last line myself after realising Gemini wouldn't think to search an uploaded PowerPoint, because the original description never mentioned one.&lt;/p&gt;




&lt;h2&gt;
  
  
  RAG: chatting with your files
&lt;/h2&gt;

&lt;p&gt;When you upload a file:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Read&lt;/strong&gt; it (pypdf, docx2txt, openpyxl for Excel, python-pptx for slides, Gemini vision for images)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chunk&lt;/strong&gt; it with &lt;code&gt;RecursiveCharacterTextSplitter&lt;/code&gt; (900 characters, 150 overlap)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Embed&lt;/strong&gt; each chunk with &lt;code&gt;gemini-embedding-001&lt;/code&gt; and store it in &lt;strong&gt;ChromaDB&lt;/strong&gt;, tagged with the chat's &lt;code&gt;thread_id&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fq4km08jocjhs1x9gt8rt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fq4km08jocjhs1x9gt8rt.png" alt="Five file types uploaded to one chat, chunked and searched" width="800" height="474"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you ask a question, SaliBot searches by &lt;strong&gt;meaning&lt;/strong&gt;, not keywords. In one test, my notes said &lt;em&gt;"microcontroller"&lt;/em&gt; and I asked &lt;em&gt;"What board controls the robot?"&lt;/em&gt;. Different words, same meaning, and it found the right passage.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;thread_id&lt;/code&gt; filter matters too: documents uploaded in one chat never leak into another.&lt;/p&gt;




&lt;h2&gt;
  
  
  Problems I hit (and how I fixed them)
&lt;/h2&gt;

&lt;p&gt;This was the most valuable part of the project.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. The model I planned to use didn't exist for me
&lt;/h3&gt;

&lt;p&gt;I started with &lt;code&gt;gemini-2.5-flash&lt;/code&gt;. My first test returned:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;404 NOT_FOUND: This model models/gemini-2.5-flash is no longer available to new users.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Google had closed the 2.5 models to new accounts. I tested which models my key could actually use (the 2.5s were blocked, 3.7 and 3.8 were overloaded, &lt;strong&gt;3.5 worked&lt;/strong&gt;) and moved SaliBot to &lt;code&gt;gemini-3.5-flash-lite&lt;/code&gt;. Later I also hit the free tier's daily limit, so I added friendly error messages instead of showing users a raw &lt;code&gt;429 RESOURCE_EXHAUSTED&lt;/code&gt; dump.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Streaming printed garbage
&lt;/h3&gt;

&lt;p&gt;With Gemini 3.5, streamed chunks arrive as a list of "parts", sometimes including an &lt;strong&gt;encrypted thought signature&lt;/strong&gt;, and the raw stream also contains the web-search JSON. Printed directly, the screen filled with base64 and JSON before the actual answer. The fix was to stream only text chunks from the &lt;code&gt;chatbot&lt;/code&gt; node and pull the text out of the parts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="nf"&gt;should_stream_chunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;metadata&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;   &lt;span class="c1"&gt;# skip tool nodes, tool calls, raw outputs&lt;/span&gt;
    &lt;span class="k"&gt;continue&lt;/span&gt;
&lt;span class="n"&gt;token&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;extract_text_from_chunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;          &lt;span class="c1"&gt;# keep only the text parts&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. The voice button listened but heard nothing
&lt;/h3&gt;

&lt;p&gt;The browser's built-in speech recognition sends audio to Google's speech servers. On my network that failed silently: "Listening…" forever, no words. So I redesigned it: the browser &lt;strong&gt;records&lt;/strong&gt; the audio, a FastAPI &lt;code&gt;/transcribe&lt;/code&gt; endpoint sends it to &lt;strong&gt;Gemini&lt;/strong&gt;, and the text lands in the input box.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. One shared sidebar for everyone
&lt;/h3&gt;

&lt;p&gt;Without accounts, anyone opening the app would see everyone's chats. I added sign-up and log-in (salted PBKDF2 password hashes, login tokens), and every chat, history, upload and delete request now checks that the chat belongs to the logged-in user. Deleting a chat also cleans up its memories, its document chunks in ChromaDB and its LangGraph checkpoints.&lt;/p&gt;

&lt;p&gt;Later I added &lt;strong&gt;"Continue with Google"&lt;/strong&gt;: Google's button returns a signed token, the server verifies it with Google's library, then finds or creates the account by email. People sign in in one click, and nobody has a password to forget.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. A global variable that would mix up users
&lt;/h3&gt;

&lt;p&gt;My first version kept the current chat id in one global variable that the tools read. With one person that's fine. With two people chatting at the same moment, one person's memory could be saved into the other's chat. Now every tool receives the chat id through LangGraph's per-request &lt;code&gt;RunnableConfig&lt;/code&gt; (that's the &lt;code&gt;config&lt;/code&gt; parameter in the tool above), so each run carries its own.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Gemini is often busy on the free tier
&lt;/h3&gt;

&lt;p&gt;Free models regularly answer &lt;code&gt;503 UNAVAILABLE&lt;/code&gt;. SaliBot now switches to a backup model by itself and tells you ("Model busy, switched to…"). My first version crashed with &lt;em&gt;"Received no input"&lt;/em&gt;: it tried to resume a run that had never been saved. The fix was to ask the checkpointer whether there's a saved step to resume, and otherwise send the question again, so the conversation never holds it twice.&lt;/p&gt;

&lt;h3&gt;
  
  
  7. The calculator could run any code
&lt;/h3&gt;

&lt;p&gt;The first calculator passed the model's text to Python's &lt;code&gt;eval()&lt;/code&gt;. On a public site, someone could trick the AI into "calculating" code that reads the secret keys. I replaced it with a tiny parser built on Python's &lt;code&gt;ast&lt;/code&gt; module that only allows numbers, operators and maths functions, with limits so &lt;code&gt;9**9**9&lt;/code&gt; can't freeze the server.&lt;/p&gt;




&lt;h2&gt;
  
  
  Watching the agent think with LangSmith
&lt;/h2&gt;

&lt;p&gt;With three environment variables, every run shows up in LangSmith: which tool the agent chose, how long Gemini took, how many tokens it used, and any errors. It's the clearest way I've found to &lt;em&gt;see&lt;/em&gt; an agent's decisions, and it made debugging much easier.&lt;/p&gt;




&lt;h2&gt;
  
  
  Deployment (free, no credit card)
&lt;/h2&gt;

&lt;p&gt;I first set up deployment to AWS with GitHub Actions, but I wanted a free route with no credit card:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Render&lt;/strong&gt; runs the same &lt;code&gt;Dockerfile&lt;/code&gt; on its free plan and redeploys on every push to &lt;code&gt;main&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Render's free disk is wiped on every restart, which deleted all accounts the first time. So accounts, chats, memories and the agent's checkpoints now live in a free &lt;strong&gt;Neon PostgreSQL&lt;/strong&gt; database, set with one &lt;code&gt;DATABASE_URL&lt;/code&gt; variable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GitHub Actions&lt;/strong&gt; builds the Docker image on every push, so a broken Dockerfile fails in CI before it reaches the server.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;My first plan was Hugging Face Spaces, but when I created the Space, Docker had become a paid feature. Lesson: free tiers change, so keep the app portable.&lt;/p&gt;




&lt;h2&gt;
  
  
  What I learned
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Agents are mostly about good tool descriptions.&lt;/strong&gt; The LLM only knows what your docstrings tell it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Plans age fast.&lt;/strong&gt; Models get retired and APIs change, so reading error messages carefully is a real skill.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Observability isn't optional.&lt;/strong&gt; Without tracing, an agent is a black box.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;"It works when I test it" is a single-user test.&lt;/strong&gt; Shared state and free-tier limits only show up with real use.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The extras teach the most.&lt;/strong&gt; Accounts, Google sign-in, a real database, deployment, file types and voice taught me more than the basic chatbot did.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What's next
&lt;/h2&gt;

&lt;p&gt;Keeping uploaded documents across restarts (pgvector in the same PostgreSQL database), upload and rate limits, and maybe a Next.js frontend.&lt;/p&gt;




&lt;p&gt;If you have questions or ideas, drop them in the comments. And if you try SaliBot, tell me what you asked it!&lt;/p&gt;

&lt;h2&gt;
  
  
  What SaliBot can do
&lt;/h2&gt;

&lt;p&gt;-&lt;strong&gt;Search the web&lt;/strong&gt; for today's news and other time-sensitive questions (Tavily)&lt;br&gt;
-&lt;strong&gt;Chat with your documents (RAG):&lt;/strong&gt; PDF, Word, TXT, Markdown, CSV, and my additions: &lt;strong&gt;Excel, PowerPoint and images&lt;/strong&gt;&lt;br&gt;
-&lt;strong&gt;Read pictures:&lt;/strong&gt; Gemini vision describes an image and copies any text in it&lt;br&gt;
-&lt;strong&gt;Long-term memory:&lt;/strong&gt; "Remember that I teach robotics" is saved and recalled later&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Calculator:&lt;/strong&gt; a sandboxed maths tool&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Research papers&lt;/strong&gt; from arXiv,  &lt;strong&gt;YouTube video summaries&lt;/strong&gt;, &lt;strong&gt;today's date&lt;/strong&gt;, and  &lt;strong&gt;"forget this"&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Streaming answers&lt;/strong&gt;, word by word, with live status chips like &lt;em&gt;"Searching the web"&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Voice input&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Accounts with "Continue with Google"&lt;/strong&gt; or email and password; every user only sees their own chats&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Automatic model fallback&lt;/strong&gt; when Gemini is busy&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pin, rename and delete&lt;/strong&gt; chats from a ⋯ menu&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LangSmith tracing&lt;/strong&gt; for every run&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  The architecture
&lt;/h2&gt;

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

&lt;p&gt;The stack:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Layer&lt;/th&gt;
&lt;th&gt;Tech&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;LLM&lt;/td&gt;
&lt;td&gt;Google Gemini 3.5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Agent&lt;/td&gt;
&lt;td&gt;LangGraph&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Backend&lt;/td&gt;
&lt;td&gt;FastAPI (Server-Sent Events for streaming)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;RAG&lt;/td&gt;
&lt;td&gt;ChromaDB + Gemini embeddings&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Database&lt;/td&gt;
&lt;td&gt;PostgreSQL on Neon (SQLite when running locally) + SQLAlchemy&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Web search&lt;/td&gt;
&lt;td&gt;Tavily&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Observability&lt;/td&gt;
&lt;td&gt;LangSmith&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sign-in&lt;/td&gt;
&lt;td&gt;Google Identity Services + email/password&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hosting&lt;/td&gt;
&lt;td&gt;Docker on Render, CI with GitHub Actions&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;


&lt;h2&gt;
  
  
  The heart of it: a LangGraph agent loop
&lt;/h2&gt;

&lt;p&gt;The agent is a small graph with two nodes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;workflow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;StateGraph&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MessagesState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;workflow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"chatbot"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;chatbot_node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;workflow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"tools"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ToolNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tools&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;workflow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_edge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;START&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"chatbot"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;workflow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_conditional_edges&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"chatbot"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tools_condition&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;workflow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_edge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"tools"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"chatbot"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;The &lt;strong&gt;chatbot&lt;/strong&gt; node sends the conversation (plus a system prompt) to Gemini.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;tools_condition&lt;/code&gt; checks Gemini's reply: did it ask for a tool?&lt;/li&gt;
&lt;li&gt;If yes, the &lt;strong&gt;tools&lt;/strong&gt; node runs it and loops back, so Gemini can read the result.&lt;/li&gt;
&lt;li&gt;If no, the answer goes to the user.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here's that loop in a real run. On the second question it called &lt;strong&gt;two tools at once&lt;/strong&gt;, then wrote one answer from both results:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flj82898arydxa6b4f3pg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flj82898arydxa6b4f3pg.png" alt="Inside the agent: the tools it calls and what comes back" width="728" height="454"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A checkpointer saves the conversation state after every step, so SaliBot remembers earlier messages in the same chat even after a restart. Locally it's a &lt;code&gt;SqliteSaver&lt;/code&gt;; in production it's a &lt;code&gt;PostgresSaver&lt;/code&gt; on the same Neon database as the accounts.&lt;/p&gt;

&lt;p&gt;What I love about this pattern: the tools are just Python functions with a docstring.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nd"&gt;@tool&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;search_uploaded_documents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;RunnableConfig&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="s2"&gt;"""
    Search uploaded documents for relevant information.
    Use this when the user asks about uploaded PDFs, DOCX, TXT, notes, files, or documents.
    Also use it for uploaded Excel sheets, PowerPoint slides, and images.
    """&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;retrieve_from_rag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;thread_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;thread_of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Gemini reads that docstring to decide when to call the tool.&lt;/strong&gt; I added the last line myself after realising Gemini wouldn't think to search an uploaded PowerPoint, because the original description never mentioned one.&lt;/p&gt;




&lt;h2&gt;
  
  
  RAG: chatting with your files
&lt;/h2&gt;

&lt;p&gt;When you upload a file:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Read&lt;/strong&gt; it (pypdf, docx2txt, openpyxl for Excel, python-pptx for slides, Gemini vision for images)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chunk&lt;/strong&gt; it with &lt;code&gt;RecursiveCharacterTextSplitter&lt;/code&gt; (900 characters, 150 overlap)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Embed&lt;/strong&gt; each chunk with &lt;code&gt;gemini-embedding-001&lt;/code&gt; and store it in &lt;strong&gt;ChromaDB&lt;/strong&gt;, tagged with the chat's &lt;code&gt;thread_id&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fq4km08jocjhs1x9gt8rt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fq4km08jocjhs1x9gt8rt.png" alt="Five file types uploaded to one chat, chunked and searched" width="800" height="474"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you ask a question, SaliBot searches by &lt;strong&gt;meaning&lt;/strong&gt;, not keywords. In one test, my notes said &lt;em&gt;"microcontroller"&lt;/em&gt; and I asked &lt;em&gt;"What board controls the robot?"&lt;/em&gt;. Different words, same meaning, and it found the right passage.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;thread_id&lt;/code&gt; filter matters too: documents uploaded in one chat never leak into another.&lt;/p&gt;




&lt;h2&gt;
  
  
  Problems I hit (and how I fixed them)
&lt;/h2&gt;

&lt;p&gt;This was the most valuable part of the project.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. The model I planned to use didn't exist for me
&lt;/h3&gt;

&lt;p&gt;I started with &lt;code&gt;gemini-2.5-flash&lt;/code&gt;. My first test returned:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;404 NOT_FOUND: This model models/gemini-2.5-flash is no longer available to new users.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Google had closed the 2.5 models to new accounts. I tested which models my key could actually use (the 2.5s were blocked, 3.7 and 3.8 were overloaded, &lt;strong&gt;3.5 worked&lt;/strong&gt;) and moved SaliBot to &lt;code&gt;gemini-3.5-flash-lite&lt;/code&gt;. Later I also hit the free tier's daily limit, so I added friendly error messages instead of showing users a raw &lt;code&gt;429 RESOURCE_EXHAUSTED&lt;/code&gt; dump.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Streaming printed garbage
&lt;/h3&gt;

&lt;p&gt;With Gemini 3.5, streamed chunks arrive as a list of "parts", sometimes including an &lt;strong&gt;encrypted thought signature&lt;/strong&gt;, and the raw stream also contains the web-search JSON. Printed directly, the screen filled with base64 and JSON before the actual answer. The fix was to stream only text chunks from the &lt;code&gt;chatbot&lt;/code&gt; node and pull the text out of the parts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="nf"&gt;should_stream_chunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;metadata&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;   &lt;span class="c1"&gt;# skip tool nodes, tool calls, raw outputs&lt;/span&gt;
    &lt;span class="k"&gt;continue&lt;/span&gt;
&lt;span class="n"&gt;token&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;extract_text_from_chunk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;          &lt;span class="c1"&gt;# keep only the text parts&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. The voice button listened but heard nothing
&lt;/h3&gt;

&lt;p&gt;The browser's built-in speech recognition sends audio to Google's speech servers. On my network that failed silently: "Listening…" forever, no words. So I redesigned it: the browser &lt;strong&gt;records&lt;/strong&gt; the audio, a FastAPI &lt;code&gt;/transcribe&lt;/code&gt; endpoint sends it to &lt;strong&gt;Gemini&lt;/strong&gt;, and the text lands in the input box.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. One shared sidebar for everyone
&lt;/h3&gt;

&lt;p&gt;Without accounts, anyone opening the app would see everyone's chats. I added sign-up and log-in (salted PBKDF2 password hashes, login tokens), and every chat, history, upload and delete request now checks that the chat belongs to the logged-in user. Deleting a chat also cleans up its memories, its document chunks in ChromaDB and its LangGraph checkpoints.&lt;/p&gt;

&lt;p&gt;Later I added &lt;strong&gt;"Continue with Google"&lt;/strong&gt;: Google's button returns a signed token, the server verifies it with Google's library, then finds or creates the account by email. People sign in in one click, and nobody has a password to forget.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. A global variable that would mix up users
&lt;/h3&gt;

&lt;p&gt;My first version kept the current chat id in one global variable that the tools read. With one person that's fine. With two people chatting at the same moment, one person's memory could be saved into the other's chat. Now every tool receives the chat id through LangGraph's per-request &lt;code&gt;RunnableConfig&lt;/code&gt; (that's the &lt;code&gt;config&lt;/code&gt; parameter in the tool above), so each run carries its own.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Gemini is often busy on the free tier
&lt;/h3&gt;

&lt;p&gt;Free models regularly answer &lt;code&gt;503 UNAVAILABLE&lt;/code&gt;. SaliBot now switches to a backup model by itself and tells you ("Model busy, switched to…"). My first version crashed with &lt;em&gt;"Received no input"&lt;/em&gt;: it tried to resume a run that had never been saved. The fix was to ask the checkpointer whether there's a saved step to resume, and otherwise send the question again, so the conversation never holds it twice.&lt;/p&gt;

&lt;h3&gt;
  
  
  7. The calculator could run any code
&lt;/h3&gt;

&lt;p&gt;The first calculator passed the model's text to Python's &lt;code&gt;eval()&lt;/code&gt;. On a public site, someone could trick the AI into "calculating" code that reads the secret keys. I replaced it with a tiny parser built on Python's &lt;code&gt;ast&lt;/code&gt; module that only allows numbers, operators and maths functions, with limits so &lt;code&gt;9**9**9&lt;/code&gt; can't freeze the server.&lt;/p&gt;




&lt;h2&gt;
  
  
  Watching the agent think with LangSmith
&lt;/h2&gt;

&lt;p&gt;With three environment variables, every run shows up in LangSmith: which tool the agent chose, how long Gemini took, how many tokens it used, and any errors. It's the clearest way I've found to &lt;em&gt;see&lt;/em&gt; an agent's decisions, and it made debugging much easier.&lt;/p&gt;




&lt;h2&gt;
  
  
  Deployment (free, no credit card)
&lt;/h2&gt;

&lt;p&gt;I first set up deployment to AWS with GitHub Actions, but I wanted a free route with no credit card:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Render&lt;/strong&gt; runs the same &lt;code&gt;Dockerfile&lt;/code&gt; on its free plan and redeploys on every push to &lt;code&gt;main&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Render's free disk is wiped on every restart, which deleted all accounts the first time. So accounts, chats, memories and the agent's checkpoints now live in a free &lt;strong&gt;Neon PostgreSQL&lt;/strong&gt; database, set with one &lt;code&gt;DATABASE_URL&lt;/code&gt; variable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GitHub Actions&lt;/strong&gt; builds the Docker image on every push, so a broken Dockerfile fails in CI before it reaches the server.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;My first plan was Hugging Face Spaces, but when I created the Space, Docker had become a paid feature. Lesson: free tiers change, so keep the app portable.&lt;/p&gt;




&lt;h2&gt;
  
  
  What I learned
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Agents are mostly about good tool descriptions.&lt;/strong&gt; The LLM only knows what your docstrings tell it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Plans age fast.&lt;/strong&gt; Models get retired and APIs change, so reading error messages carefully is a real skill.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Observability isn't optional.&lt;/strong&gt; Without tracing, an agent is a black box.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;"It works when I test it" is a single-user test.&lt;/strong&gt; Shared state and free-tier limits only show up with real use.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The extras teach the most.&lt;/strong&gt; Accounts, Google sign-in, a real database, deployment, file types and voice taught me more than the basic chatbot did.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What's next
&lt;/h2&gt;

&lt;p&gt;Keeping uploaded documents across restarts (pgvector in the same PostgreSQL database), upload and rate limits, and maybe a Next.js frontend.&lt;/p&gt;




&lt;p&gt;If you have questions or ideas, drop them in the comments. And if you try SaliBot, tell me what you asked it! &lt;/p&gt;

&lt;h2&gt;
  
  
  Let's connect
&lt;/h2&gt;

&lt;p&gt;I’m Salima Iddrisu, and I build AI tools. Let’s connect:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;LinkedIn:&lt;/strong&gt; &lt;a href="https://www.linkedin.com/in/salima-iddrisu" rel="noopener noreferrer"&gt;https://www.linkedin.com/in/salima-iddrisu&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GitHub:&lt;/strong&gt; &lt;a href="https://github.com/Iddrisusalima" rel="noopener noreferrer"&gt;https://github.com/Iddrisusalima&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ai</category>
      <category>python</category>
      <category>langchain</category>
      <category>fastapi</category>
    </item>
    <item>
      <title>My RAG tool refused to answer a question my notes clearly answered</title>
      <dc:creator>Salima Iddrisu</dc:creator>
      <pubDate>Sun, 25 Oct 2026 23:22:44 +0000</pubDate>
      <link>https://dev.to/salima_iddrisu/my-rag-tool-refused-to-answer-a-question-my-notes-clearly-answered-aho</link>
      <guid>https://dev.to/salima_iddrisu/my-rag-tool-refused-to-answer-a-question-my-notes-clearly-answered-aho</guid>
      <description>&lt;p&gt;I built a retrieval-augmented generation tool over my own notes — no LangChain, no LlamaIndex, every stage by hand.&lt;/p&gt;

&lt;p&gt;Then I asked it a question my notes definitely answer, and it said:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I can't find the answer to that in your notes.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The answer was there. I could point at the line.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bug one: the passage arrived mid-word
&lt;/h2&gt;

&lt;p&gt;The question was &lt;em&gt;"why are input and output tokens priced differently?"&lt;/em&gt; My notes say:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Output tokens cost about 8x more than input tokens. $2.50 versus $0.30 per million.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here is what retrieval handed the model:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;y 8x more per token than
input**. The `/stats` command prints...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Read the first two characters. That is &lt;code&gt;...roughly 8x&lt;/code&gt; with its opening sliced into the previous chunk.&lt;/p&gt;

&lt;p&gt;My 500-character cut had landed mid-word. Half the sentence in chunk N, half in chunk N+1, and neither half complete enough to match anything. I raised top-k to 10 assuming the good chunk ranked lower — it never appeared, because no chunk contained that sentence whole.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Nothing reported a problem.&lt;/strong&gt; No exception, no warning. The fact was in my notes, unreachable, and the tool looked like it was working.&lt;/p&gt;

&lt;p&gt;The fix is to stop treating chunk size as a hard boundary — scan backwards for somewhere sensible to cut:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;_soften&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hard_end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;overlap&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lookback&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# The cut must stay far enough from start that the next chunk still
&lt;/span&gt;    &lt;span class="c1"&gt;# advances past the overlap, or the loop never terminates.
&lt;/span&gt;    &lt;span class="n"&gt;span&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hard_end&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;
    &lt;span class="n"&gt;floor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;overlap&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;span&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;lookback&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;hard_end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;floor&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;hard_end&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;hard_end&lt;/span&gt;

    &lt;span class="c1"&gt;# Paragraph break, then sentence ending, then any whitespace.
&lt;/span&gt;    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;marker&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;. &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;! &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;? &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;found&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rfind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;marker&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hard_end&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;found&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;found&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;marker&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;hard_end&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;found&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;marker&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;hard_end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I found the infinite-loop risk in that &lt;code&gt;floor&lt;/code&gt; line by feeding it a 2,000-character string with no whitespace at all.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bug two: the same bug, backwards
&lt;/h2&gt;

&lt;p&gt;So I tried bigger chunks. 900 characters. More context is better, surely.&lt;/p&gt;

&lt;p&gt;A different question broke.&lt;/p&gt;

&lt;p&gt;My notes answer &lt;em&gt;"how did I keep my API key safe?"&lt;/em&gt; in four numbered steps under a heading of almost that name. It is eight lines long. At 900 characters those eight lines get absorbed into a chunk dominated by unrelated material, and the embedding becomes an average of all of it. It stopped matching.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Failure mode&lt;/th&gt;
&lt;th&gt;What goes wrong&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Chunks too small&lt;/td&gt;
&lt;td&gt;a fact gets cut in half and survives nowhere&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Chunks too large&lt;/td&gt;
&lt;td&gt;a short section gets averaged into irrelevance&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;size / overlap&lt;/th&gt;
&lt;th&gt;pricing question&lt;/th&gt;
&lt;th&gt;key safety question&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;500 / 100&lt;/td&gt;
&lt;td&gt;refused — mid-word fragment&lt;/td&gt;
&lt;td&gt;refused&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;900 / 300&lt;/td&gt;
&lt;td&gt;correct partial answer&lt;/td&gt;
&lt;td&gt;refused — diluted&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;350 / 70&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;fine&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;answered&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;350 works because my notes are short sections with frequent headings, so ~350 characters is about one section. That is a property of my documents, not a universal constant.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The lesson is not the number.&lt;/strong&gt; It is that chunk-count statistics cannot tell you this. Only real questions can.&lt;/p&gt;

&lt;h2&gt;
  
  
  The assumption: a threshold can detect an unanswerable question
&lt;/h2&gt;

&lt;p&gt;Early on, three sentences on one topic scored &lt;strong&gt;0.617&lt;/strong&gt; against each other. Three unrelated ones scored &lt;strong&gt;0.031&lt;/strong&gt;. A twenty-fold gap — so reject anything below a cut-off and you handle unanswerable questions.&lt;/p&gt;

&lt;p&gt;Then I tried it for real.&lt;/p&gt;

&lt;p&gt;I asked &lt;em&gt;"what time does the corner shop close on Sundays?"&lt;/em&gt; Nothing in my notes about shops or opening hours. Best match: &lt;strong&gt;0.3386&lt;/strong&gt; — it matched a chunk containing &lt;code&gt;Sun Oct 4&lt;/code&gt;, because &lt;em&gt;Sundays&lt;/em&gt; and &lt;em&gt;Sun&lt;/em&gt; genuinely are related.&lt;/p&gt;

&lt;p&gt;My weakest genuinely useful answer scored &lt;strong&gt;0.3655&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A gap of &lt;strong&gt;0.027&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I nearly set the floor at 0.35. Then I swept it across all four real questions instead of just my best example:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;floor&lt;/th&gt;
&lt;th&gt;Q1&lt;/th&gt;
&lt;th&gt;Q2&lt;/th&gt;
&lt;th&gt;Q3&lt;/th&gt;
&lt;th&gt;Q4&lt;/th&gt;
&lt;th&gt;unanswerable&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;off&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0.30&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;0.35&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;2&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;2&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0.40&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;1&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;At 0.35 the bad question is silenced and two real ones drop to two passages. At 0.40 they return nothing.&lt;/p&gt;

&lt;p&gt;The scores are not calibrated across questions. A question worded like my notes scores high on everything; one phrased differently scores low on everything, even when its top hit is right. An absolute threshold compares numbers that were never on the same scale.&lt;/p&gt;

&lt;p&gt;What works instead is the prompt — giving the model an acceptable way out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3. If the passages do not contain the answer, say so plainly - for example:
   "I can't find the answer to that in your notes." Do not guess, do not fill
   gaps from general knowledge.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Told only &lt;em&gt;"answer from the context"&lt;/em&gt;, a model handed irrelevant passages still produces something, because refusing is not an option it has. Given explicit permission, refusal becomes the compliant response.&lt;/p&gt;

&lt;p&gt;My relevance floor ships switched &lt;strong&gt;off&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three takeaways
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Chunk-count statistics are not quality measurements.&lt;/strong&gt; I spent a day comparing chunk counts and mean lengths, then picked a value that failed on the second real question I asked.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Silent failures are the ones to design against.&lt;/strong&gt; Every real problem here produced no error — the severed sentence, the diluted section, a mismatched embedding model returning confident nonsense.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Test the weakest case, not the strongest.&lt;/strong&gt; My threshold looked safe until I swept it across questions that were not my best example.&lt;/p&gt;

&lt;p&gt;Code and every measurement behind these decisions, including the ones I got wrong first: &lt;a href="https://github.com/Iddrisusalima/ask-my-docs" rel="noopener noreferrer"&gt;github.com/Iddrisusalima/ask-my-docs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you have built something similar, how do you handle the unanswerable-question problem? The prompt fix works, but it feels like there is a retrieval-level signal I am missing.&lt;/p&gt;

</description>
      <category>rag</category>
      <category>python</category>
      <category>ai</category>
      <category>machinelearning</category>
    </item>
    <item>
      <title>Memory is just re-sending: what building a CLI chat tool taught me about LLM cost</title>
      <dc:creator>Salima Iddrisu</dc:creator>
      <pubDate>Sun, 20 Sep 2026 17:29:44 +0000</pubDate>
      <link>https://dev.to/salima_iddrisu/memory-is-just-re-sending-what-building-a-cli-chat-tool-taught-me-about-llm-cost-2n6i</link>
      <guid>https://dev.to/salima_iddrisu/memory-is-just-re-sending-what-building-a-cli-chat-tool-taught-me-about-llm-cost-2n6i</guid>
      <description>&lt;p&gt;I asked a chatbot my name. It told me. Ten seconds later it had no idea who I was.&lt;/p&gt;

&lt;p&gt;Nothing about the model changed between those two questions. I deleted a list on my own laptop, and the memory went with it, because that list &lt;em&gt;was&lt;/em&gt; the memory.&lt;/p&gt;

&lt;p&gt;This post is about what I found building Prompt Lab, a CLI chat tool for Google's Gemini models that prints the tokens and cost of every reply. The code is &lt;a href="https://github.com/Iddrisusalima/prompt-lab" rel="noopener noreferrer"&gt;on GitHub&lt;/a&gt;. I'm using the &lt;code&gt;google-genai&lt;/code&gt; SDK and &lt;code&gt;gemini-3.5-flash-lite&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Statelessness is the whole design constraint
&lt;/h2&gt;

&lt;p&gt;Language models retain nothing between requests. No session, no history on the server. So "memory" cannot live in the model, and it doesn't. It lives in a list in your process, and it works by re-sending the entire conversation every time.&lt;/p&gt;

&lt;p&gt;Which means memory and cost are the same thing. Every turn pays again for every turn before it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbxjo4uplhhoao95sbf6i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbxjo4uplhhoao95sbf6i.png" alt="Architecture of one conversation turn" width="800" height="815"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Commands like &lt;code&gt;/reset&lt;/code&gt; and &lt;code&gt;/stats&lt;/code&gt; are intercepted before the network call, so they cost nothing. Everything else appends to history, then the system instruction plus the &lt;strong&gt;entire&lt;/strong&gt; history goes to the API.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building the request
&lt;/h2&gt;

&lt;p&gt;The history is a list of &lt;code&gt;types.Content&lt;/code&gt;, each carrying a role:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;google.genai&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;types&lt;/span&gt;

&lt;span class="n"&gt;history&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;types&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Content&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add_turn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;role is &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;user&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; for my messages, &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;model&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; for the bot&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;s replies.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;history&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;types&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Content&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;role&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;role&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;types&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Part&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;from_text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)])&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first gotcha is here. &lt;strong&gt;Gemini has no &lt;code&gt;role="system"&lt;/code&gt;.&lt;/strong&gt; OpenAI passes the system prompt as a message inside the list; Gemini passes it as configuration &lt;em&gt;alongside&lt;/em&gt; the list:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;config&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;types&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GenerateContentConfig&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;types&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GenerateContentConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;system_instruction&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;system_prompt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;send_once&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;tuple&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;types&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GenerateContentResponseUsageMetadata&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
    &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generate_content&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;MODEL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;history&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;        &lt;span class="c1"&gt;# the whole conversation, every single time
&lt;/span&gt;        &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;config&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;strip&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;usage_metadata&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That &lt;code&gt;contents=history&lt;/code&gt; line is the entire memory mechanism. One turn looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;add_turn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;user&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user_input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;      &lt;span class="c1"&gt;# 1. append what I said
&lt;/span&gt;&lt;span class="n"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;usage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;send_once&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;        &lt;span class="c1"&gt;# 2. send the WHOLE history
&lt;/span&gt;&lt;span class="nf"&gt;add_turn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;model&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;          &lt;span class="c1"&gt;# 3. append what it said
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 3 is the one that's easy to miss. Skip it and the model never sees its own previous answers, so it starts contradicting itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reading the bill
&lt;/h2&gt;

&lt;p&gt;Usage rides along on the response object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;usage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;usage_metadata&lt;/span&gt;
&lt;span class="n"&gt;usage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prompt_token_count&lt;/span&gt;       &lt;span class="c1"&gt;# input: my messages + system prompt + history
&lt;/span&gt;&lt;span class="n"&gt;usage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;candidates_token_count&lt;/span&gt;   &lt;span class="c1"&gt;# output: what the model generated
&lt;/span&gt;&lt;span class="n"&gt;usage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;total_token_count&lt;/span&gt;        &lt;span class="c1"&gt;# what you are actually billed on
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Input and output are priced separately, so the cost function bills them at different rates. Google publishes per million tokens:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;PRICING_PER_1M&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;gemini-3.5-flash-lite&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;2.50&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;   &lt;span class="c1"&gt;# (input, output) USD per 1M tokens
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;cost_of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input_tokens&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;output_tokens&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;float&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;input_tokens&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;1_000_000&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;INPUT_PER_1M&lt;/span&gt;
        &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;output_tokens&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;1_000_000&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;OUTPUT_PER_1M&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note the ratio: &lt;code&gt;2.50 / 0.30&lt;/code&gt; means &lt;strong&gt;output costs roughly 8x more per token than input&lt;/strong&gt;. The &lt;code&gt;/stats&lt;/code&gt; command prints the running bill split by direction:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7rjpy6x5sc9yahcf4aq4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7rjpy6x5sc9yahcf4aq4.png" alt="Session totals showing input and output tokens costed separately" width="799" height="682"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In that session, 74% of the tokens were input, which is history being re-sent. But look at the cost column: 156 output tokens cost $0.000390 while 451 input tokens cost only $0.000135. A quarter of the tokens, three quarters of the price. That gives you two independent levers, not one: trim history to cut input, and ask for brevity to cut output.&lt;/p&gt;

&lt;h2&gt;
  
  
  The measurement
&lt;/h2&gt;

&lt;p&gt;The same four words, asked seconds apart, either side of a &lt;code&gt;/reset&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fpl5cqoba2p80bw2obldm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fpl5cqoba2p80bw2obldm.png" alt="The same question asked either side of a reset, with token counts visible" width="800" height="595"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;You: what is my name
Bot: Your name is Salima!
  [turn 22]  history: 44 msgs  |  tokens  in: 822   out: 6

You: /reset
History cleared. Dropped 44 messages.

You: what is my name
Bot: I don't have access to your personal information, so I don't know your name
     yet! What should I call you?
  [turn 23]  history: 2 msgs   |  tokens  in: 29    out: 27
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;822 input tokens down to 29. Twenty-eight times cheaper for an identical question. The 822 wasn't my question. It was 44 messages of history being re-sent.&lt;/p&gt;

&lt;p&gt;Across another session input went from 41 tokens on turn 1 to 654 by turn 18, while my typed messages stayed the same length and the replies got &lt;em&gt;shorter&lt;/em&gt;. Growth is closer to quadratic than linear: turn 10 pays for turns 1 through 9 again.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two more gotchas
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;total_token_count&lt;/code&gt; is not always input + output
&lt;/h3&gt;

&lt;p&gt;I assumed it was addition. Testing &lt;code&gt;gemini-3.6-flash&lt;/code&gt;, one request reported &lt;strong&gt;4 input tokens, 2 output tokens, and a total of 62&lt;/strong&gt;. The missing 56 were internal reasoning tokens, billed but never shown in the reply. Trust &lt;code&gt;total_token_count&lt;/code&gt;, not your own arithmetic.&lt;/p&gt;

&lt;h3&gt;
  
  
  With streaming, usage arrives on the final chunk
&lt;/h3&gt;

&lt;p&gt;Switching to &lt;code&gt;generate_content_stream()&lt;/code&gt; doesn't break cost tracking, but the usage data isn't there from the start. Keep the last one you're handed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generate_content_stream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;MODEL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;history&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;config&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;flush&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;usage_metadata&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;usage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;usage_metadata&lt;/span&gt;   &lt;span class="c1"&gt;# only the final chunk carries it
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Streaming changes perceived speed, not price.&lt;/p&gt;

&lt;h2&gt;
  
  
  Failing without corrupting state
&lt;/h2&gt;

&lt;p&gt;Two things mattered here. First, status codes need different handling, because retrying a rejected key is pointless while retrying a rate limit is sensible:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;401&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;403&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Your API key was rejected. Check GEMINI_API_KEY for typos.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;404&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;The model &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;MODEL&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt; was not found. Your key is fine - a 404 means &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;auth succeeded and only the model name is wrong.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;429&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Rate limit or quota exceeded. Wait a minute and try again.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That 404 branch exists because I wasted half an hour assuming my key was broken. A 404 means authentication &lt;em&gt;succeeded&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;I triggered each of these deliberately rather than coding for them and hoping. Here's an invalid key, reported and survived:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7j8zk8bnst99biz7aad9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7j8zk8bnst99biz7aad9.png" alt="An invalid API key producing a clear HTTP 401 message with zero turns in history" width="800" height="698"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Two details in that output matter. The tool keeps running instead of exiting. And the session totals report zero turns and zero messages in history, which proves the rollback below actually fired.&lt;/p&gt;

&lt;p&gt;Second, a failed request must not leave a half-finished conversation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;usage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;send_once&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;errors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;APIError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;history&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;        &lt;span class="c1"&gt;# roll back the user turn we just appended
&lt;/span&gt;    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;explain_api_error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;continue&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without that &lt;code&gt;pop()&lt;/code&gt;, history ends on an unanswered user turn, which breaks the shape of the next request.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'd fix next
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;cost_of()&lt;/code&gt; takes input and output, which means it misses thinking tokens entirely. On a model that uses them, I'm under-reporting by roughly 10x. My code contradicts my own write-up on that point.&lt;/p&gt;

&lt;p&gt;History is also never trimmed. It only grows, so a long enough conversation will stop fitting in the context window. The fix is dropping oldest turns in pairs to keep roles alternating, or summarising the old prefix into one synthetic turn.&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaway
&lt;/h2&gt;

&lt;p&gt;Making a stateless model appear to remember means re-sending everything, and that is exactly what you pay for.&lt;/p&gt;

&lt;p&gt;If you're building on an LLM, print your token counts once and watch them climb. It changes how you think about conversation length, system prompt size, and when to throw history away.&lt;/p&gt;

&lt;p&gt;Code, architecture notes and a day-by-day build log: &lt;strong&gt;&lt;a href="https://github.com/Iddrisusalima/prompt-lab" rel="noopener noreferrer"&gt;github.com/Iddrisusalima/prompt-lab&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>ai</category>
      <category>llm</category>
      <category>showdev</category>
    </item>
  </channel>
</rss>
