<?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: Tony Bob</title>
    <description>The latest articles on DEV Community by Tony Bob (@usamaqadri).</description>
    <link>https://dev.to/usamaqadri</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%2F4276508%2F5c30eada-0250-4995-af3a-154e9884977f.png</url>
      <title>DEV Community: Tony Bob</title>
      <link>https://dev.to/usamaqadri</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/usamaqadri"/>
    <language>en</language>
    <item>
      <title>Free AI API Code: Build Your Own LLM Gateway with FastAPI (Open Source)</title>
      <dc:creator>Tony Bob</dc:creator>
      <pubDate>Tue, 01 Dec 2026 13:10:04 +0000</pubDate>
      <link>https://dev.to/usamaqadri/free-ai-api-code-build-your-own-llm-gateway-with-fastapi-open-source-4lel</link>
      <guid>https://dev.to/usamaqadri/free-ai-api-code-build-your-own-llm-gateway-with-fastapi-open-source-4lel</guid>
      <description>&lt;p&gt;Want one single API that can talk to many AI models? This free open-source project does exactly that. It is called LLM Gateway — a small Python (FastAPI) server that sits in front of AI providers like OpenAI, OpenRouter, Groq or a local Ollama, and gives you one OpenAI-style address for all of them. It also has API-key login, rate limiting, automatic retries, and a live dashboard that shows requests, tokens, cost and speed. Best part: it runs fully offline with a built-in demo (mock) provider, so you can test everything without any paid API key.&lt;/p&gt;

&lt;p&gt;What you need: Python installed on your computer. That is all.&lt;/p&gt;

&lt;h2&gt;
  
  
  STEP 1 — Download and run the code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/nisarahmad78/llm-gateway.git
&lt;span class="nb"&gt;cd&lt;/span&gt; llm-gateway
python -m venv .venv
&lt;span class="nb"&gt;source&lt;/span&gt; .venv/bin/activate
pip install -r requirements.txt
cp .env.example .env
uvicorn app.main:app --port 8000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now open these in your browser:&lt;/p&gt;

&lt;p&gt;Dashboard: &lt;a href="http://localhost:8000/" rel="noopener noreferrer"&gt;http://localhost:8000/&lt;/a&gt;&lt;br&gt;
API docs: &lt;a href="http://localhost:8000/docs" rel="noopener noreferrer"&gt;http://localhost:8000/docs&lt;/a&gt;&lt;br&gt;
Health check: &lt;a href="http://localhost:8000/health" rel="noopener noreferrer"&gt;http://localhost:8000/health&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  STEP 2 — Test the API with one command
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl http://localhost:8000/v1/chat/completions -H &lt;span class="s2"&gt;"Authorization: Bearer YOUR_KEY"&lt;/span&gt; -H &lt;span class="s2"&gt;"Content-Type: application/json"&lt;/span&gt; -d &lt;span class="s1"&gt;'{"model": "mock-1", "messages": [{"role": "user", "content": "Explain RAG in two sentences."}]}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will get a normal OpenAI-style JSON reply from the demo provider.&lt;/p&gt;

&lt;h2&gt;
  
  
  STEP 3 — Use it from Python code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;openai&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;OpenAI&lt;/span&gt;
&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;OpenAI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;base_url&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"http://localhost:8000/v1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;api_key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"YOUR_KEY"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;reply&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;chat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;completions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&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="s2"&gt;"mock-1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"role"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"user"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"content"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Hello, gateway!"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&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;choices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To connect a real AI model later, put your provider key in the .env file (OPENAI_API_KEY) and use a model like gpt-4o-mini — the rest of your code stays the same.&lt;/p&gt;

&lt;h2&gt;
  
  
  FULL SOURCE CODE (FREE)
&lt;/h2&gt;

&lt;p&gt;The complete project — every file, free to use, copy and modify — is on GitHub: &lt;a href="https://github.com/nisarahmad78/llm-gateway" rel="noopener noreferrer"&gt;https://github.com/nisarahmad78/llm-gateway&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Credit and license: "LLM Gateway" is created by Nisar Ahmad (GitHub: nisarahmad78) and is released under the MIT License, which allows free use with attribution. All credit for the code goes to the original author.&lt;/p&gt;

&lt;p&gt;More free AI code and simple AI agent guides are posted here on AI Agents Hub every day. Bookmark this page and come back for the next one.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>python</category>
      <category>opensource</category>
      <category>api</category>
    </item>
  </channel>
</rss>
