<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8861905861585113921</id><updated>2026-11-22T16:35:09.492-05:00</updated><category term="Asp .net"/><category term="C #"/><category term="Web service"/><category term="sql server"/><category term=".net"/><category term="Android"/><category term="Python"/><category term="AI Agents"/><category term="Videos"/><category term="JAVA"/><category term="JavaScript"/><category term="Tutorials"/><category term="Deploy Web Sites"/><category term="FastAPI"/><category term="Gemini"/><category term="Windows Forms"/><category term="visual Studio"/><category term="C # data base"/><category term="C#"/><category term="IIS SERVER"/><category term="Projects"/><category term="Smart Phone"/><category term="Tutorial"/><category term="Windows Phone 7"/><category term="XML"/><category term="sql"/><category term="Installation"/><category term="Interview Questions"/><category term="Irrlicht 3D  Game"/><category term="NED Itec 2012"/><category term="Oracle"/><category term="jQuery"/><category term="Azure"/><category term="Blogger Tip"/><category term="Data Grid View"/><category term="DevOps"/><category term="Email"/><category term="Html5"/><category term="Iphone"/><category term="Reports in C#"/><category term="Smart Phone Apps"/><category term="Web"/><category term="Web Server"/><category term="Windows Phone 7.5"/><category term="seo"/><category term="AT commands"/><category term="Ajax"/><category term="Automation"/><category term="CI/CD"/><category term="Cloud"/><category term="Data Structure"/><category term="Database"/><category term="Docker"/><category term="Downloads"/><category term="Downloads tutorials"/><category term="Free Source Code"/><category term="Game Programming  Visual C++"/><category term="GitHub Actions"/><category term="Google"/><category term="Google Code"/><category term="Google Maps"/><category term="Google Search"/><category term="Grafana"/><category term="IT News"/><category term="Interview"/><category term="JSon"/><category term="LINQ"/><category term="MCP"/><category term="Mobile App Development Competition"/><category term="Model Context &#xa;Protocol"/><category term="Monitoring"/><category term="Objective-C"/><category term="Open Source"/><category term="PHP"/><category term="Performance"/><category term="Prometheus"/><category term="Regex"/><category term="Site map"/><category term="Small AI Apps"/><category term="Tool Calling"/><category term="Tools"/><category term="Windows Service"/><category term="css"/><category term="flash"/><category term="sql server 2008"/><category term="windows applications"/><title type='text'>Visual Studio Learn</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://www.visualstudiolearn.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://www.visualstudiolearn.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>The Loonie Lesson</name><uri>http://www.blogger.com/profile/02243798795439866634</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRY4V9cFLhqMxHWBOWCGBiGUqjan14Vda_dy5rz-C41kApceM-u6jAxS2_6AIRZtGkQeyiQolq1gCeSBhpo573lYq37MLmMGgSKDQLasowic65VoitivYCzqrmUDUqxJuqCTyzah8JuALYtW0Iz7rXIvIcj-ovhVpEuEhQJ6yG1FUQ-A/s1600/loonie-lesson-logo-profile.png'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>94</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8861905861585113921.post-3215985288393696403</id><published>2026-11-21T08:15:37.000-05:00</published><updated>2026-11-22T02:51:05.076-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="AI Agents"/><category scheme="http://www.blogger.com/atom/ns#" term="Gemini"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><category scheme="http://www.blogger.com/atom/ns#" term="Tutorial"/><title type='text'>Build Your Own Coding Agent in Python — A Beginner&#39;s Step-by-Step Guide (Every Step Explained)</title><content type='html'>&lt;p&gt;&lt;strong&gt;An AI agent is a program that thinks with an AI model and acts through tools.&lt;/strong&gt; In this beginner-friendly build, you will write one small Python program — a &lt;em&gt;coding agent&lt;/em&gt; — hand it a real failing test, and watch it find the bug, propose the exact fix, and then &lt;strong&gt;stop and wait for your permission&lt;/strong&gt; before it changes anything. By the end, a test suite that starts at &lt;strong&gt;1 failed, 3 passed&lt;/strong&gt; ends at &lt;strong&gt;4 passed&lt;/strong&gt;, and the whole run costs less than one cent. Every step is explained in plain words, with a screenshot — no experience with agents is assumed.&lt;/p&gt;

&lt;p&gt;This is the beginner-first edition of our coding-agent build. If you already know what an agent is and want the denser reference version, read the original build instead: &lt;a href=&quot;https://www.visualstudiolearn.com/2026/11/build-your-own-coding-agent-in-python.html&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;Build Your Own Coding Agent in Python (It Fixes Real Code — Behind an Approval Gate)&lt;/a&gt;. This post covers the same machine more slowly, and adds the two things beginners asked for: what every part &lt;em&gt;means&lt;/em&gt;, and what to do when it breaks.&lt;/p&gt;

&lt;iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/0BOlIGButT8&quot; title=&quot;Build Your Own Coding Agent in Python — Part 1: The Tools and the Gate&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen&gt;&lt;/iframe&gt;&lt;b&gt;&lt;/b&gt;

&lt;h2&gt;What you need (versions used in this post)&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Python 3.12&lt;/strong&gt; — the programming language everything here is written in.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;google-genai 3.0.0&lt;/strong&gt; — Google&#39;s software kit (an &quot;SDK&quot;) for talking to Gemini from Python.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;pytest 9.2.0&lt;/strong&gt; — the test runner that checks our demo code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The model: gemini-3.8-flash&lt;/strong&gt; — the current Gemini Flash model. (The older gemini-2.5-flash is being retired on December 1, 2026, so we do not build on it.)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;A free Gemini API key&lt;/strong&gt; from Google AI Studio — step-by-step below.&lt;/li&gt;
&lt;li&gt;The companion code (free): the &lt;a href=&quot;https://github.com/sajjad037/visual-studio-learn-code/tree/main/topic-09-coding-agent&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;topic-09-coding-agent folder&lt;/a&gt; in our public repo. The demo run shown in this post was recorded on November 11, 2026, with exactly this code.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;The whole machine, on one screen&lt;/h2&gt;
&lt;p&gt;Before any code, here is the map the whole build follows. Read it top to bottom:&lt;/p&gt;
&lt;figure&gt;&lt;img alt=&quot;The map of the coding agent: you hand it a task; the agent loops through three tools — read_file, run_tests, write_file — with an approval gate on the write path, all landing on your code and its tests.&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgNjOTWOmUoQFjc7Ok1PsCmZ_HRkcvH0Ond3v4kGf7zrvtIy0YLK4TxU80wU3KXLKhzdfCELJ-qQf5O0tDwSe1nGgMb0hxVZLSfDJdoZQtDmSkWzlZywMt0pW39JAuYrQUQbfzUVKG3yaV70jFb-LJ0JFIt6HQgc3JcW-gWc-MWTYbU-ocWakQl2xqS448/s600/topology-t9b.png&quot; /&gt;&lt;/figure&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;You&lt;/strong&gt; hand the agent one task, written in plain English.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The agent&lt;/strong&gt; works in a &lt;em&gt;loop&lt;/em&gt;: it runs a tool, looks at the result, and chooses the next tool — at most twelve rounds.&lt;/li&gt;
&lt;li&gt;It has exactly &lt;strong&gt;three tools&lt;/strong&gt;: &lt;code&gt;read_file&lt;/code&gt; (read a file), &lt;code&gt;run_tests&lt;/code&gt; (run the test suite), and &lt;code&gt;write_file&lt;/code&gt; (change a file). The write tool&#39;s path runs through a &lt;strong&gt;gate&lt;/strong&gt;, because writing is the one action that changes your code.&lt;/li&gt;
&lt;li&gt;Every tool lands on &lt;strong&gt;your code and its tests&lt;/strong&gt; — nothing else on your machine.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;That is the entire design. Everything below is building this map, one piece at a time — and at the end, the agent runs on it for real.&lt;/p&gt;

&lt;h2&gt;Environment setup&lt;/h2&gt;
&lt;h3&gt;1. Create the project folder and a virtual environment&lt;/h3&gt;
&lt;p&gt;A &lt;strong&gt;virtual environment&lt;/strong&gt; (a &quot;venv&quot;) is an isolated folder that holds one project&#39;s Python packages, so they never collide with the rest of your machine. Create one and step inside it:&lt;/p&gt;
&lt;pre&gt;python3.12 -m venv .venv
source .venv/bin/activate      # Windows: .venv\Scripts\activate&lt;/pre&gt;
&lt;h3&gt;2. Install the two packages (pinned versions)&lt;/h3&gt;
&lt;pre&gt;pip install google-genai==3.0.0 pytest==9.2.0&lt;/pre&gt;
&lt;p&gt;The versions are pinned — written with &lt;code&gt;==&lt;/code&gt; — so your install matches this tutorial exactly, instead of drifting to whatever is newest.&lt;/p&gt;

&lt;h3&gt;3. Get your free Gemini API key (click by click)&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;Go to &lt;a href=&quot;https://aistudio.google.com/&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;Google AI Studio&lt;/a&gt; and sign in with a Google account.&lt;/li&gt;
&lt;li&gt;Open the &lt;strong&gt;API keys&lt;/strong&gt; page (the key icon, &quot;Get API key&quot;).&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Create API key&lt;/strong&gt;, choose or create a project, and copy the key it shows you.&lt;/li&gt;
&lt;li&gt;Put the key in an &lt;strong&gt;environment variable&lt;/strong&gt; — a setting your terminal session carries — named &lt;code&gt;GEMINI_API_KEY&lt;/code&gt;:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre&gt;export GEMINI_API_KEY=&quot;paste-your-key-here&quot;     # Windows (PowerShell): $env:GEMINI_API_KEY=&quot;paste-your-key-here&quot;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Never type the key into your code.&lt;/strong&gt; Code gets shared, committed, and screenshotted; keys must not. The agent program reads the key from the environment only, and refuses to start without it. One honest warning about free keys: on Gemini&#39;s free tier, Google may use what you send to improve its products — so learn on demo code like ours, never on private code.&lt;/p&gt;
&lt;h3&gt;4. Verify your setup&lt;/h3&gt;
&lt;p&gt;Get the companion code (link above), open a terminal in the &lt;code&gt;demo&lt;/code&gt; folder, and run the test suite yourself:&lt;/p&gt;
&lt;pre&gt;python -m pytest -q&lt;/pre&gt;
&lt;p&gt;You should see &lt;strong&gt;1 failed, 3 passed&lt;/strong&gt;. That failure is deliberate — it is the bug your agent is about to fix. If you see that line, your environment is ready.&lt;/p&gt;
&lt;figure&gt;&lt;img alt=&quot;Verifying the setup: the test suite shows 1 failed, 3 passed&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBLxLh7tbObG_wLdKuj-z1Ifg49_bESR-5BgJGTRc5yTltRxmFc2NuKzc9bUhYmj4Y61IHxzJLF6AIlUBd_e_B1gcStk14zMT0PGqakSC7hVIKtliOaojicURC6fk6WrDex8K3LZfqHVXt1QmasJCx_gyXJuKFioDj-br2JlI-3jobuuQaRlOdAvV8AWM/s600/s0-setup.png&quot; /&gt;&lt;/figure&gt;

&lt;h2&gt;Step 1 — Meet the patient: a cart, a test suite, and one planted bug&lt;/h2&gt;
&lt;p&gt;Our demo project is a tiny shopping cart in one small file, &lt;code&gt;cart.py&lt;/code&gt;. Around it sits a &lt;strong&gt;test suite&lt;/strong&gt; — a set of automatic checks. A &lt;em&gt;test&lt;/em&gt; does something, then compares what happened with what &lt;em&gt;should&lt;/em&gt; have happened. Here is the check that matters, as a worked example:&lt;/p&gt;
&lt;figure&gt;&lt;img alt=&quot;Worked example of a test: add two notebooks, add three more of the same — the cart should say five, but says three.&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhdrA8e94JDm7LmBdsyN1aBXt6fvibCY51FarLpnM2TgQkRMXx5J_x1D3wynPshaTgFib9itdoKePZE0yroVTU3mt4_ECrgzBehz6refwmoJJpwtYRjD3W172U5quBjEalzp84nPcrHLoHvgAQuszl69KIwnTpuU0yeA2G6AlR87G1YLry7DYUFh3RF2oA/s600/worked-example-t9b.png&quot; /&gt;&lt;/figure&gt;
&lt;p&gt;Add two notebooks to the cart. Add three more of the same. The cart should say &lt;strong&gt;five&lt;/strong&gt;. Our cart says &lt;strong&gt;three&lt;/strong&gt; — because when the same item arrives twice, &lt;code&gt;Cart.add()&lt;/code&gt; &lt;em&gt;replaces&lt;/em&gt; the quantity instead of &lt;em&gt;adding&lt;/em&gt; to it:&lt;/p&gt;
&lt;pre&gt;def add(self, name, unit_price_cents, quantity=1):
    # BUG (planted): this replaces the quantity instead of adding to it.
    self._items[name] = [unit_price_cents, quantity]&lt;/pre&gt;
&lt;p&gt;The suite has four tests; exactly this one fails (&lt;code&gt;test_add_same_item_twice_accumulates&lt;/code&gt; — the cart reports 3, the test expects 5). Adding a new item, totalling a mixed cart, and removing an item all pass. The bug is narrow: one method, one wrong idea about what &quot;add&quot; means. That one failure is the agent&#39;s job.&lt;/p&gt;
&lt;figure&gt;&lt;img alt=&quot;The patient, before the agent: the test suite run in the terminal before the agent starts&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqqHEt6ZVqtDlY2YCC8iCfKyd0x0IjMpA-O58Bqwd4ugyHAUTXb1AbupDEzn6EiPQEAW_QAc6TXWmwMWtFJQi8iOcFWfWSgPkU4GEwzv68vEqFlltP9_KyyKH7q80ge_H-EsfoQv4X-LokDFNYhtyCyYW0RarjEQMqbReDHbvM0GFcceJLkwPiSVUAN0I/s600/s1-prerun.png&quot; /&gt;&lt;/figure&gt;
&lt;figure&gt;&lt;img alt=&quot;The demo cart code: Cart.add() replaces the quantity instead of adding to it&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinaifrA4Xlz1cSPe8bJmxDpqiA7C7XIAI0f4NAlYBbxqM2ff57z_8E4GdgvgeqN3eTbt1Z56MDjX3orY2jmoiDh6J7euueLCw7CJAQ1kHJztVagLrRlLFO1G_8dpBAu4HvRzq6FRp4zxUL6GyCJhz9o35bnYACCOIvGSocM5tlWmWqj8oWncw3nTfqElU/s600/s2-patient.png&quot; /&gt;&lt;/figure&gt;

&lt;h2&gt;Step 2 — The skeleton: a brain, a connection, and a job description&lt;/h2&gt;
&lt;p&gt;The agent is one file, &lt;code&gt;agent.py&lt;/code&gt;. Three things at the top define it:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;A client&lt;/strong&gt; — the connection to the Gemini API: &lt;code&gt;client = genai.Client()&lt;/code&gt; (it finds your key in the environment variable).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The model&lt;/strong&gt; — &lt;code&gt;MODEL = &quot;gemini-3.8-flash&quot;&lt;/code&gt;. This model can do the one trick the whole build stands on: &lt;strong&gt;function calling&lt;/strong&gt;. Function calling means the model never runs code itself. It &lt;em&gt;asks our program&lt;/em&gt; to run a named tool — &quot;read this file&quot;, &quot;run those tests&quot; — and reads the result that comes back.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The task&lt;/strong&gt;, written in plain English and handed to the model as its instructions: &lt;em&gt;&quot;Run the test suite in this project. If a test fails, read the test and the code it tests, find the bug, and fix the source file so the whole suite passes. Do not edit the tests. When every test passes, stop and summarise what the bug was and what you changed.&quot;&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;figure&gt;&lt;img alt=&quot;The agent skeleton in agent.py: the Gemini client, the model, and the task&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhnLnTz9motDoZTWWp8ugPkk9rmcnXP6i07wNaUYEJt7nSrB4CV-54z1mJmG2P8X5Al1Emhinf0d-_bFuSAg3Q5Yi1XeZkcyDqBuea4DkYHgc4PfTHFoGxzhLRD_lMLaarSP9iG4tVzTo7Ol8kyulpl-2e1Q2ksLHPpiJvuUA1Rj6fwKTgbHFd7Ukqqgc8/s600/s3-skeleton.png&quot; /&gt;&lt;/figure&gt;

&lt;h2&gt;Step 3 — Tool 1: &lt;code&gt;read_file&lt;/code&gt;, and the guard inside it&lt;/h2&gt;
&lt;p&gt;Tool one does what it sounds like: the model names a file, the tool reads it and hands back the text. But look at the guard, because the guard is the lesson. Every path the model asks for is &lt;em&gt;resolved&lt;/em&gt; — turned into its real, full location — and checked: if it lands outside the demo project folder, the request is refused.&lt;/p&gt;
&lt;pre&gt;def confine(path_text):
    &quot;&quot;&quot;Resolve a model-supplied path and refuse anything outside the project.&quot;&quot;&quot;
    path = (PROJECT_DIR / path_text).resolve()
    if not path.is_relative_to(PROJECT_DIR.resolve()):
        return None
    return path&lt;/pre&gt;
&lt;p&gt;The model can ask for anything. &lt;strong&gt;The code decides what it may touch.&lt;/strong&gt; This pattern is called &lt;em&gt;confinement&lt;/em&gt;, and every tool in this agent uses it. (One detail for later: reads are capped at 8,000 characters, so a huge file cannot flood the model.)&lt;/p&gt;
&lt;figure&gt;&lt;img alt=&quot;Tool 1 read_file with its confinement guard&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghQl-irP1jHF7KbICQ0N7ok-vTVdv0kUJ2n82mQytWlgORE7T4MUlAgISY6dKq4clZVhWJHQgIqu9aaEVDyPwiLlonNxEIJ2CrOUM6n4EvbH7W2txdBUu8OJLi9Z2M0NGUUMCot2D8-1PxjY7qgis5EftemDiphdAjlVLdlO009tXUTTrj-FG43Rszv9c/s600/s4-tool1.png&quot; /&gt;&lt;/figure&gt;

&lt;h2&gt;Step 4 — Tool 2: &lt;code&gt;run_tests&lt;/code&gt;, the agent&#39;s eyes&lt;/h2&gt;
&lt;p&gt;When the model calls tool two, it runs the pytest suite inside the project folder and returns the &lt;em&gt;tail&lt;/em&gt; of the output — the summary lines at the end, where the failures live:&lt;/p&gt;
&lt;pre&gt;def tool_run_tests():
    proc = subprocess.run(
        [sys.executable, &quot;-m&quot;, &quot;pytest&quot;, &quot;-q&quot;],
        cwd=PROJECT_DIR, capture_output=True, text=True, timeout=120,
    )
    return {&quot;exit_code&quot;: proc.returncode, &quot;output&quot;: (proc.stdout + proc.stderr)[-3000:]}&lt;/pre&gt;
&lt;p&gt;That tail is how the agent sees the world: not a screenshot, not a guess — the same &quot;1 failed, 3 passed&quot; you saw in Step 1. Like &lt;code&gt;read_file&lt;/code&gt;, this tool cannot change anything. Watching and reading are safe. The third tool is neither.&lt;/p&gt;
&lt;figure&gt;&lt;img alt=&quot;Tool 2 run_tests returning the test summary tail&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhKQURCuqdmsAN_1Rrnxzd4AuarDCJ323iWsVLucIDY_EMxo2Lu_MnP8RHi-d6F8J7KQNpL-MbfH0BVG3kpyMJWqEU2ZP2u46Vv4xJw7PPBOAXP2UO8C4W3RRCfxmh4MC_Eovf3n1U3L4gp1mqBi5_sWQlPRSZ-fCc2PakeMdWSl0gIK051rslftPMBq6s/s600/s5-tool2.png&quot; /&gt;&lt;/figure&gt;

&lt;h2&gt;Step 5 — Tool 3: &lt;code&gt;write_file&lt;/code&gt;, and the approval gate&lt;/h2&gt;
&lt;p&gt;Tool three is the dangerous one, so it is built differently. &lt;strong&gt;The model never writes. It proposes.&lt;/strong&gt; The tool prints the complete proposed file in the terminal, where a human can read every line — and then the program stops and asks one question:&lt;/p&gt;
&lt;pre&gt;def tool_write_file(path, content):
    &quot;&quot;&quot;THE GATE: the model only ever PROPOSES. A human decides.&quot;&quot;&quot;
    target = confine(path)
    if target is None:
        return {&quot;error&quot;: f&quot;refused: {path} is outside the project folder&quot;}
    if not target.parent.is_dir():
        return {&quot;error&quot;: f&quot;refused: folder for {path} does not exist&quot;}
    print(f&quot;\n--- The agent wants to write {path} ---&quot;)
    print(content)
    answer = input(&quot;Apply this change? [y/N] &quot;).strip().lower()
    if answer != &quot;y&quot;:
        print(&quot;--- Denied by the human. The file was NOT changed. ---&quot;)
        return {&quot;result&quot;: &quot;DENIED by the human approver. Do not retry this write; &quot;
                          &quot;explain in your summary what you wanted to change.&quot;}
    target.write_text(content)
    print(&quot;--- Approved and written. ---&quot;)
    return {&quot;result&quot;: f&quot;{path} written&quot;}&lt;/pre&gt;
&lt;p&gt;That pause is the &lt;strong&gt;approval gate&lt;/strong&gt;, and it is the heart of this build. Answer anything but &quot;y&quot;, and the file stays byte-for-byte untouched — in our own testing, a denied write left the file&#39;s fingerprint (its SHA-256 hash) identical — and the model is told, in plain words, that a human refused and it must not retry. Answer &quot;y&quot;, and only then does the file change. Every other tool in this agent can run all day. This one needs you.&lt;/p&gt;
&lt;figure&gt;&lt;img alt=&quot;Tool 3 write_file code showing the approval gate&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhIFxaTYxzER8llcEFMs5yQ6dzn_DOgCDUVc_jNfQOlYXBuTulCsTp1SLK0gj7MjwNxvLgZFI8VghzqfA6sLtZt_NuiF3LyfHh0qunt2XUclPQiI6sicvqHL0qDqCZRPCdS9HcwlIbMckoRS2Ks6NJeabNM6eI96be_K89qHUYtSjSb97aqLAPlQrbIEu8/s600/s6-tool3-gate-code.png&quot; /&gt;&lt;/figure&gt;
&lt;figure&gt;&lt;img alt=&quot;The approval gate live in the terminal, asking Apply this change? [y/N]&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgYOphY06eUxubqE1Br3Y-IqMVTZsaoGMJr1_o9dqE3MVH0moYsmzaGDyV_J8BbZoDIcG4G4ds-IZGJtteFR0esd0l6JY6Hwu7J8C38BcxGbOP9NjqJhjt83G0JXvtZxKD_liV3ZkSTeUxnJYc8PJCG48VJTqiwkJ_ZigdZIQpO_XjRzKWKbEjNrgm_QSo/s600/s7-gate-live.png&quot; /&gt;&lt;/figure&gt;

&lt;h2&gt;Step 6 — The loop: ask, act, look, choose again&lt;/h2&gt;
&lt;p&gt;Around the tools sits the loop — the part that decides. The program sends the model the task and three &lt;strong&gt;tool declarations&lt;/strong&gt;. A declaration is a small menu card: the tool&#39;s name, a plain-English description, and a strict &lt;em&gt;schema&lt;/em&gt; — the exact arguments it accepts and their types. That menu is all the model ever sees of your code; the functions themselves stay yours.&lt;/p&gt;
&lt;p&gt;Then the cycle repeats: the model replies with a tool call (or with words). A call runs here, in our program; its result travels back, and the model picks the next move. &lt;strong&gt;Ask, act, look, choose again&lt;/strong&gt; — that cycle is all the word &quot;loop&quot; means, and it is the entire engine of every agent you will ever meet. Two safety rails are built in:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;A step cap of 12&lt;/strong&gt; (&lt;code&gt;MAX_STEPS&lt;/code&gt;): after twelve model turns the loop stops, whatever the model believes — a confused model costs you twelve turns, never forever.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Words mean &quot;done&quot;:&lt;/strong&gt; when the model answers in words instead of a tool call, the job is finished, and it summarises.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;One wrinkle, learned the hard way&lt;/strong&gt; (it is also in the code&#39;s comments): tool results travel back to the model labelled with role &lt;code&gt;&quot;user&quot;&lt;/code&gt;. Label them &lt;code&gt;&quot;tool&quot;&lt;/code&gt; — the obvious word — and this API refuses them with a 400 error. We hit that exact wall while building this tutorial; the shipped code carries the fix.&lt;/p&gt;
&lt;figure&gt;&lt;img alt=&quot;The agent loop: ask, act, look, choose again, capped at 12 steps&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEipeIR6sZdv05WtiO3Md8ngdRBz_c4sYTlTLbV8O3JRZpnmu3_h_LqRRQyiYA9qnoeOrwgXHFMUwtJZyr5_cyly0HVGKLsLnDi2LPrtz4-6Lj-KplouZXu_35Aig2KeEYWMyyKriQemLKVuHX8EY3khxFfp6SBiCKlZBjmVypWskUrjiKhCk8Txpu9tdNA/s600/s8-loop.png&quot; /&gt;&lt;/figure&gt;

&lt;h2&gt;Step 7 — The real run&lt;/h2&gt;
&lt;p&gt;Now the agent runs — for real, exactly as it happened when we recorded it (November 11, 2026). Five steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;run_tests&lt;/code&gt;&lt;/strong&gt; — the agent&#39;s first move is to run the suite on its own. It sees the failure itself: 1 failed, 3 passed.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;read_file&lt;/code&gt; test_cart.py&lt;/strong&gt; — it reads the failing test.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;read_file&lt;/code&gt; cart.py&lt;/strong&gt; — then the code the test checks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;write_file&lt;/code&gt; cart.py → THE GATE&lt;/strong&gt; — it proposes the complete fixed file and stops. The proposal is the whole file with one idea changed: if the item is already in the cart, &lt;em&gt;add&lt;/em&gt; to its quantity — &lt;code&gt;self._items[name][1] += quantity&lt;/code&gt;. (The &lt;code&gt;[1]&lt;/code&gt; is the quantity slot: each cart entry is stored as a pair, &lt;code&gt;[unit_price_cents, quantity]&lt;/code&gt;, so slot 1 is the quantity.) The operator read the proposal — it touched only the &lt;code&gt;add&lt;/code&gt; method — and approved.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;run_tests&lt;/code&gt;&lt;/strong&gt; — the write landed, and on its own the agent ran the suite again: &lt;strong&gt;4 passed&lt;/strong&gt;. Then it summarised, in its own words, exactly what the bug was and what it changed.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Found, diagnosed, proposed, approved, fixed, proven. (We independently re-ran the suite on the agent-fixed file afterwards: 4 passed — then put the planted bug back, so the repo ships the exercise, not the answer.)&lt;/p&gt;
&lt;figure&gt;&lt;img alt=&quot;The real run, part 1: run_tests, then reading the test and cart code&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgn2WIKqSHCVkL9bj0Mkr8ZRgDYl3ugOBBvoZxPaSNQDekKbggCGT7pT6iv2caayWLOmqRW56EOZbFkAcvn7G7JjpWslrHZhyCFoPpgdRX93PJDjwuTyCu5QZX9JKjn6IqsDJJKDc7ERwEoCBZTLCy-J1toFSZmJ3voBWvoupnQzEO666jKf3mCwyaZuaw/s600/s9-run-part1.png&quot; /&gt;&lt;/figure&gt;
&lt;figure&gt;&lt;img alt=&quot;The payoff: after approval, the suite shows 4 passed&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjWhZr1zIjo2E22GdVB5BQ-oLrXc57VcQp-sVZFy6AZrxj1hMJ2j0vQ24rp98Gq0vcLf6z1X0-VuLpWk11k5qDrLFwROsDTTyUwq4N9RPe65i2a0gBUlqPCRhn10NQTnFD5aDJ7h_CmL8N8uS4Uq9KKL_lK50Jqglg4O146wjN7voQPJkeNp2qNbF6n5kI/s600/s10-payoff.png&quot; /&gt;&lt;/figure&gt;

&lt;h2&gt;Step 8 — The bill: what one fix costs&lt;/h2&gt;
&lt;p&gt;You pay for the text the model reads and writes, counted in &lt;strong&gt;tokens&lt;/strong&gt; — pieces of text, roughly word-sized. The agent counts them as it runs, and prints the total at the end. This run:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;7,017 tokens in&lt;/strong&gt; — everything the model read (the task, the files, the test output).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;1,202 tokens out&lt;/strong&gt; — everything it wrote, &lt;em&gt;including its thinking tokens&lt;/em&gt;, because the price list bills those as output too.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The prices, verified on Google&#39;s own pricing page: &lt;strong&gt;$0.75 per million input tokens&lt;/strong&gt; and &lt;strong&gt;$3.75 per million output tokens&lt;/strong&gt; for gemini-3.8-flash (standard tier, at those rates through December 31, 2026). The maths:&lt;/p&gt;
&lt;pre&gt;in:   7,017 × $0.75 / 1M  = $0.0053
out:  1,202 × $3.75 / 1M  = $0.0045
total                     = $0.0098&lt;/pre&gt;
&lt;p&gt;The whole fix cost &lt;strong&gt;less than one cent&lt;/strong&gt; — and on a free AI Studio key, the same run costs &lt;strong&gt;$0&lt;/strong&gt; (with the free-tier data warning from the setup section attached). The lesson is not that it is cheap. It is that the cost is &lt;em&gt;knowable&lt;/em&gt;, per run, from numbers the run itself prints.&lt;/p&gt;
&lt;figure&gt;&lt;img alt=&quot;The bill: 7,017 tokens in, 1,202 out, total $0.0098&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjsQjoM1eug-xG0Rd7qdmaq3TJfu8aIsLmQq6C77kRbUO-i6wnqtqN-R1mkI8QMb1dFeMmjdjKeXAT_kHUEDRI1Lu0jpnI6ES7rsw3jkjZSLtPtxtvMXQKpc5rIcoEFh6l2EP79apAkAkEjeLG-B0-B4C45LrncKZcZTixDXXWnus_SbgFuv1OmE0zGRPM/s600/s11-bill.png&quot; /&gt;&lt;/figure&gt;

&lt;h2&gt;Step 9 — The guardrails, named&lt;/h2&gt;
&lt;p&gt;Step back and name what keeps this agent safe, because these four are the difference between a tool and a liability:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;The gate&lt;/strong&gt; — no write is ever ungated; a human reads the exact change first.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Confinement&lt;/strong&gt; — every tool is locked to one project folder, enforced by the code, not requested in a prompt.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The step cap&lt;/strong&gt; — twelve turns, then it stops, whatever it believes.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;What it does not have&lt;/strong&gt; — no shell tool, so it cannot run arbitrary commands; no network tool, so it cannot send your code anywhere.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;These are not our inventions: the serious tools work the same way — Claude Code, for instance, confines its writes and asks before the dangerous ones, and well-built agent products route risky actions to a human before they execute. The principle to keep: &lt;strong&gt;prompts are suggestions; code is enforcement.&lt;/strong&gt; Build the enforcement.&lt;/p&gt;

&lt;h2&gt;Step 10 — The honest verdict&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;What it nailed:&lt;/strong&gt; the diagnosis was exact, the fix was the smallest change that could work, and it stopped the moment the suite went green instead of polishing further.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Where it wobbled — and this is the real lesson:&lt;/strong&gt; it rewrote the &lt;em&gt;whole file&lt;/em&gt; to change three lines in one method (whole-file writes are simple, but blunt), and the file it saved still carries its header comment claiming &quot;a bug lives here on purpose&quot; — which stopped being true the moment the fix landed. A human reading the proposal at the gate catches that. An auto-approver ships it. That is why the gate exists.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The limits, plainly:&lt;/strong&gt; this is a teaching agent, for small, well-tested code. A full tool like Claude Code brings a terminal, a map of your whole codebase, and years of hardening this one file does not have. Build one anyway — because once you have written the loop, the gate, and the confinement yourself, you understand what every agent product is actually doing under the hood. That understanding is the real deliverable.&lt;/p&gt;

&lt;h2&gt;Errors you may hit&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&quot;Set GEMINI_API_KEY first&quot;&lt;/strong&gt; — the key variable is missing in this terminal. Set it (setup step 3) in the &lt;em&gt;same&lt;/em&gt; terminal you launch the agent from, then run again.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;A 400 error about the role &quot;tool&quot;&lt;/strong&gt; — if you typed the loop yourself and sent tool results back with role &lt;code&gt;&quot;tool&quot;&lt;/code&gt;, this API refuses them. Send them as role &lt;code&gt;&quot;user&quot;&lt;/code&gt;, like the shipped code does (Step 6).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The model repeats the same tool call&lt;/strong&gt; — the step cap ends the loop at 12. Read the printed transcript; the fix is usually a clearer task sentence (say exactly what &quot;done&quot; looks like).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;A patch that breaks a different test&lt;/strong&gt; — the suite goes red again and the loop continues; the gate hands you a second decision. Never approve a fix you have not read.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;pytest: command not found / module not found&lt;/strong&gt; — your virtual environment is not active, or you are in the wrong folder. Activate the venv and run from the project folder.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;The encore: the agent files its own report (issue #16)&lt;/h2&gt;
&lt;p&gt;After the fix, this same agent drafted a report of its own run — the bug, the change, the result — and a human reviewed the exact draft at a gate and approved it. The report was then posted through GitHub&#39;s official MCP server (MCP is the standard way agents connect to outside tools) as &lt;a href=&quot;https://github.com/sajjad037/visual-studio-learn-code/issues/16&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;issue #16 in the companion repo&lt;/a&gt; — a public audit trail of everything the agent did. It is the same pattern as the write gate, one level up: the agent proposes, the human decides, and only then does anything leave the machine. The companion Short video shows this beat on screen.&lt;/p&gt;
&lt;figure&gt;&lt;img alt=&quot;The encore: the agent&#39;s report filed as GitHub issue #16 via MCP&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgYMdwR4iWu88hXj7Rn02j0NrvBm1ABctX_ll4TvQlWsy7lYBvDc6YLIi_N17GRj5ODh4UjkN6kA8Cf6-ny-nnBdpoKKdol0s8gBQyYdKg_3W88dDZT6ol95YU4qXKcbPvIoshw_Sc7MmXE4ejTrH4osYHaF3xI9rJhhvPBZmcI6rkya4i8fVd-o2mqJPw/s600/s12-mcp-issue.png&quot; /&gt;&lt;/figure&gt;

&lt;h2&gt;FAQ&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Do I need to pay for a Gemini API key?&lt;/strong&gt;&lt;br /&gt;No. A free AI Studio key runs this whole tutorial, and the demo run costs $0 on it. Just keep the free-tier warning in mind: do not feed the agent private code on a free key.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Why build an agent by hand instead of using a ready-made tool?&lt;/strong&gt;&lt;br /&gt;Understanding. A ready-made tool hides the loop, the tools, and the gates. Once you have written all three yourself — about 140 lines — every agent product you meet afterwards reads as variations on a machine you already know.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What stops the agent from going rogue?&lt;/strong&gt;&lt;br /&gt;Code, not promises: it can only see one folder (confinement), it cannot write without a human yes (the gate), it must stop after twelve steps (the cap), and it has no shell or network tool at all.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Can I give it more tools?&lt;/strong&gt;&lt;br /&gt;Yes — that is the natural next step. Add one tool at a time, keep each one confined, and gate anything that changes state. The moment a new tool can &lt;em&gt;change&lt;/em&gt; something, it goes behind the same approval pattern as &lt;code&gt;write_file&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Gemini API pricing (rate card verified November 7, 2026): &lt;a href=&quot;https://ai.google.dev/gemini-api/docs/pricing&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;ai.google.dev/gemini-api/docs/pricing&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;google-genai SDK documentation: &lt;a href=&quot;https://googleapis.github.io/python-genai/&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;googleapis.github.io/python-genai&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;GitHub MCP server (remote endpoint, toolsets): &lt;a href=&quot;https://github.com/github/github-mcp-server&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;github.com/github/github-mcp-server&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;The run itself: demo transcript and companion code, recorded November 11, 2026 — &lt;a href=&quot;https://github.com/sajjad037/visual-studio-learn-code/tree/main/topic-09-coding-agent&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;topic-09-coding-agent&lt;/a&gt; and &lt;a href=&quot;https://github.com/sajjad037/visual-studio-learn-code/issues/16&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;issue #16&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Get the code&lt;/h2&gt;
&lt;p&gt;The complete, free companion code — the agent, the demo project with the planted bug, and the test suite — lives in the &lt;a href=&quot;https://github.com/sajjad037/visual-studio-learn-code/tree/main/topic-09-coding-agent&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;topic-09-coding-agent folder&lt;/a&gt; of our public repo, with setup instructions in its README. Run the suite first (see the failure), then run the agent — and when the gate asks, try denying the first proposal on purpose, just to watch what the agent does with a refusal.&lt;/p&gt;

&lt;p&gt;Disclosure: Visual Studio Learn is reader-supported. Some links may be affiliate links, which means the channel may earn a small commission at no extra cost to you. Only tools actually used in the tutorials are ever linked, and affiliate relationships never change a verdict.&lt;/p&gt;

&lt;div style=&quot;background: rgb(251, 252, 253); border-radius: 8px; border: 1px solid rgb(227, 230, 234); margin: 28px 0px; padding: 18px 14px; text-align: center;&quot;&gt;&lt;h3 style=&quot;margin: 0px 0px 6px;&quot;&gt;Enjoying these tutorials?&lt;/h3&gt;&lt;p style=&quot;margin: 0px 0px 6px;&quot;&gt;Get new Visual Studio Learn posts by email — free, no spam:&lt;/p&gt;&lt;style&gt;
    .followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview {
  display: flex !important;
  flex-direction: column !important;
  justify-content: center !important;
  margin-top: 30px !important;
  padding: clamp(17px, 5%, 40px) clamp(17px, 7%, 50px) !important;
  max-width: none !important;
  border-radius: 6px !important;
  box-shadow: 0 5px 25px rgba(34, 60, 47, 0.25) !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview,
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview *{
  box-sizing: border-box !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-heading {
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-heading h5{
  margin-top: 0 !important;
  margin-bottom: 0 !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field {
  margin-top: 20px !important;
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input {
  width: 100% !important;
  height: 40px !important;
  border-radius: 6px !important;
  border: 2px solid #e9e8e8 !important;
  background-color: #fff;
  outline: none !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input {
  color: #000000 !important;
  font-family: &quot;Montserrat&quot; !important;
  font-size: 14px;
  font-weight: 400;
  line-height: 20px;
  text-align: center;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input::placeholder {
  color: #000000 !important;
  opacity: 1 !important;
}

.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input:-ms-input-placeholder {
  color: #000000 !important;
}

.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input::-ms-input-placeholder {
  color: #000000 !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button {
  margin-top: 10px !important;
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button button {
  width: 100% !important;
  height: 40px !important;
  border: 0 !important;
  border-radius: 6px !important;
  line-height: 0px !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button button:hover {
  cursor: pointer !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button {
  margin-top: 10px !important;
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .powered-by-line {
  color: #231f20 !important;
  font-family: &quot;Montserrat&quot; !important;
  font-size: 13px;
  font-weight: 400;
  line-height: 25px;
  text-align: center;
  text-decoration: none;
  display: flex;
  width: 100%;
  justify-content: center;
  align-items: center;
  margin-top: 10px;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .powered-by-line img {
  margin-left: 10px !important;
  height: 1.13em !important;
  max-height: 1.13em !important;
}

  &lt;/style&gt;
  &lt;div attr-a=&quot;&quot; attr-b=&quot;&quot; attr-c=&quot;&quot; attr-d=&quot;&quot; attr-e=&quot;&quot; attr-f=&quot;&quot; class=&quot;followit--follow-form-container&quot;&gt;
    &lt;form action=&quot;https://api.follow.it/subscription-form/SmRpWjEzV1ovU0FOK2R2Y0oxajFBUmtIdW44K1ZiUWZRUm1FZVdpVlRKUHFLSFBKWjE4M1hTSk5yRU5oNmg0RU5zelhuTllNTGU1UmwzRkN0NzZWbnd2YkV5VWFwYnptWFdtekI3eXdwMmhtamtUNit3dlNSblVmRjJxd1JGbjZ8Q05yQWR2eitvYW5HRmNMMzJkbStpM09rNmxXZ0ZjWWhyOERya2VvVXRzZz0=/8&quot; data-v-f93f6730=&quot;&quot; method=&quot;post&quot;&gt;&lt;div class=&quot;form-preview&quot; data-v-f93f6730=&quot;&quot; style=&quot;background-color: white; border-color: rgb(204, 204, 204); border-style: solid; border-width: 1px; position: relative;&quot;&gt;&lt;div class=&quot;preview-heading&quot; data-v-f93f6730=&quot;&quot;&gt;&lt;h5 data-v-f93f6730=&quot;&quot; style=&quot;color: black; font-family: Arial; font-size: 16px; font-weight: bold; text-align: center; text-transform: none;&quot;&gt;Get new posts by email:&lt;/h5&gt;&lt;/div&gt;&lt;div class=&quot;preview-input-field&quot; data-v-f93f6730=&quot;&quot;&gt;&lt;input data-v-f93f6730=&quot;&quot; name=&quot;email&quot; required=&quot;&quot; spellcheck=&quot;false&quot; style=&quot;background-color: white; color: black; font-family: Arial; font-size: 14px; font-weight: normal; text-align: center; text-transform: none;&quot; type=&quot;email&quot; /&gt;&lt;/div&gt;&lt;div class=&quot;preview-submit-button&quot; data-v-f93f6730=&quot;&quot;&gt;&lt;button data-v-f93f6730=&quot;&quot; style=&quot;background-color: black; color: black; font-family: Arial; font-size: 16px; font-weight: bold; text-align: center; text-transform: none;&quot; type=&quot;submit&quot;&gt;Subscribe&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/form&gt;&lt;a class=&quot;powered-by-line&quot; href=&quot;https://follow.it&quot;&gt;Powered by &lt;img alt=&quot;follow.it&quot; height=&quot;17px&quot; src=&quot;https://follow.it/images/colored-logo.svg&quot; /&gt;&lt;/a&gt;
  &lt;/div&gt;&lt;p style=&quot;font-size: 14px; margin: 12px 0px 0px;&quot;&gt;Follow along: &lt;a href=&quot;https://www.youtube.com/@VisualStudioLearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/youtube/FF0000&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;YouTube&quot; /&gt; YouTube&lt;/a&gt; · &lt;a href=&quot;https://www.facebook.com/VisualStudio.Learn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/facebook/1877F2&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;Facebook&quot; /&gt; Facebook&lt;/a&gt; · &lt;a href=&quot;https://www.instagram.com/visualstudiolearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/instagram/E4405F&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;Instagram&quot; /&gt; Instagram&lt;/a&gt; · &lt;a href=&quot;https://www.tiktok.com/@visualstudiolearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/tiktok/000000&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;TikTok&quot; /&gt; TikTok&lt;/a&gt; · &lt;a href=&quot;https://www.visualstudiolearn.com/feeds/posts/default?alt=rss&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/rss/FFA500&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;RSS&quot; /&gt; RSS&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.visualstudiolearn.com/feeds/3215985288393696403/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.visualstudiolearn.com/2026/11/build-your-own-coding-agent-in-python_01385216401.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/3215985288393696403'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/3215985288393696403'/><link rel='alternate' type='text/html' href='http://www.visualstudiolearn.com/2026/11/build-your-own-coding-agent-in-python_01385216401.html' title='Build Your Own Coding Agent in Python — A Beginner&#39;s Step-by-Step Guide (Every Step Explained)'/><author><name>The Loonie Lesson</name><uri>http://www.blogger.com/profile/02243798795439866634</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRY4V9cFLhqMxHWBOWCGBiGUqjan14Vda_dy5rz-C41kApceM-u6jAxS2_6AIRZtGkQeyiQolq1gCeSBhpo573lYq37MLmMGgSKDQLasowic65VoitivYCzqrmUDUqxJuqCTyzah8JuALYtW0Iz7rXIvIcj-ovhVpEuEhQJ6yG1FUQ-A/s1600/loonie-lesson-logo-profile.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://img.youtube.com/vi/0BOlIGButT8/default.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8861905861585113921.post-3589815936243328349</id><published>2026-11-19T14:18:10.000-05:00</published><updated>2026-11-22T02:51:03.831-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="DevOps"/><category scheme="http://www.blogger.com/atom/ns#" term="Docker"/><category scheme="http://www.blogger.com/atom/ns#" term="FastAPI"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>Dockerize a FastAPI App: Dockerfile to Running Container</title><content type='html'>&lt;h1&gt;Dockerize a FastAPI App: Dockerfile to Running Container&lt;/h1&gt;
&lt;p&gt;&quot;It works on my machine.&quot; Every developer has said it — usually right before the same app breaks on a server, because something there is different: another Python version, a library missing, a file that never got copied.&lt;/p&gt;
&lt;p&gt;Docker ends that argument. You pack the app together with everything it needs into one box — an &lt;strong&gt;image&lt;/strong&gt; — and the box runs the same everywhere. In this post we dockerize a FastAPI app — from a plain &lt;strong&gt;Dockerfile&lt;/strong&gt; to a running &lt;strong&gt;container&lt;/strong&gt; — then pack it &lt;em&gt;properly&lt;/em&gt;: the careless first try weighs &lt;strong&gt;1.65 GB&lt;/strong&gt;; the proper pack weighs &lt;strong&gt;198 MB&lt;/strong&gt;. Same app. Smaller box.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Get the code:&lt;/strong&gt; all files from this post are free in the companion repo — &lt;a href=&quot;https://github.com/sajjad037/visual-studio-learn-code/tree/main/topic-14-docker-fastapi&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;topic-14-docker-fastapi folder&lt;/a&gt;&lt;/p&gt;
&lt;div style=&quot;text-align:center;margin:20px 0;&quot;&gt;&lt;iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/sdtXMMXrXMI&quot; title=&quot;Dockerize a FastAPI App: Dockerfile to Running Container&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen&gt;&lt;/iframe&gt;&lt;/div&gt;

&lt;h2&gt;The map&lt;/h2&gt;
&lt;p&gt;Four stops, and everything below is one of them:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;The recipe&lt;/strong&gt; — a &lt;code&gt;Dockerfile&lt;/code&gt;: a short text file that describes the box.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The packed box&lt;/strong&gt; — Docker reads the recipe and builds an &lt;strong&gt;image&lt;/strong&gt;: your app, frozen together with Python and every library it needs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The running box&lt;/strong&gt; — start the image and it becomes a &lt;strong&gt;container&lt;/strong&gt;: your app, running, sealed off from the machine.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The proof&lt;/strong&gt; — we ask the app a question over the network, and it answers.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;Step 1 — The app: a tiny notes API&lt;/h2&gt;
&lt;p&gt;Our app is deliberately small — one file, &lt;code&gt;main.py&lt;/code&gt;, with three endpoints (an endpoint is one address the app answers on):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;GET /health&lt;/code&gt; — answers &quot;I am alive&quot;. Remember this one; Docker itself will use it later.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;GET /notes&lt;/code&gt; — lists the notes.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;POST /notes&lt;/code&gt; — adds a note.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiYrklkRUFpYJISn1IWXkd-jBgoNb8J0OiJo5Rwo6uCo4Ibiumg-pKhfaMlvQEVzPjL1HHhgibTFOcV843KhqGBQaHNNvCSvqKYLtxKpl0W3FAnQ02xwgMrtSRMeLJW-uVVcxyWWcTwHYRwpT-7MRlDqw_zfat3h3zPFzEV_FG531Pgsb-3uPUy7WTztXQ/s600/01-main-py.png&quot; alt=&quot;The whole app — main.py, a tiny FastAPI notes API with three endpoints: health, list notes, add note&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Here is the whole file, to copy:&lt;/p&gt;
&lt;pre&gt;&quot;&quot;&quot;Tiny notes API — the demo app we dockerize in Topic #14.

Three endpoints are enough to prove a container works:
  GET  /health  -&amp;gt; {&quot;status&quot;: &quot;ok&quot;}        (used by the Docker HEALTHCHECK)
  GET  /notes   -&amp;gt; list of notes
  POST /notes   -&amp;gt; add a note  {&quot;text&quot;: &quot;...&quot;}
&quot;&quot;&quot;
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI(title=&quot;Notes API&quot;)

notes: list[dict] = [{&quot;id&quot;: 1, &quot;text&quot;: &quot;Ship it in a container&quot;}]


class NoteIn(BaseModel):
    text: str


@app.get(&quot;/health&quot;)
def health():
    return {&quot;status&quot;: &quot;ok&quot;}


@app.get(&quot;/notes&quot;)
def list_notes():
    return notes


@app.post(&quot;/notes&quot;, status_code=201)
def add_note(note: NoteIn):
    new = {&quot;id&quot;: len(notes) + 1, &quot;text&quot;: note.text}
    notes.append(new)
    return new&lt;/pre&gt;
&lt;p&gt;And &lt;code&gt;requirements.txt&lt;/code&gt; — the three libraries, pinned:&lt;/p&gt;
&lt;pre&gt;fastapi==0.115.6
uvicorn==0.34.0
pydantic==2.10.4&lt;/pre&gt;

&lt;h2&gt;Step 2 — The first recipe: the naive Dockerfile&lt;/h2&gt;
&lt;p&gt;&quot;Naive&quot; just means the obvious first try — five working lines:&lt;/p&gt;
&lt;pre&gt;# NAIVE FIRST DRAFT — it works, but wait for the size.
FROM python:3.12
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD [&quot;uvicorn&quot;, &quot;main:app&quot;, &quot;--host&quot;, &quot;0.0.0.0&quot;, &quot;--port&quot;, &quot;8000&quot;]&lt;/pre&gt;
&lt;p&gt;Line by line:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;FROM python:3.12&lt;/code&gt; — start from a ready-made box that already has Python inside.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;WORKDIR /app&lt;/code&gt; — the folder the app lives in, inside the box.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;COPY . .&lt;/code&gt; — copy everything from the project folder into the box.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;RUN pip install&lt;/code&gt; — install the libraries listed in &lt;code&gt;requirements.txt&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;CMD&lt;/code&gt; — the command that starts the app when the box runs.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuseUKbEWnVbjt2Gh6S3X_REPxBOtPn9A3b35O_PJ7gjC_4PVhxsPfmsUR-zGUoO0QlA7zIa4db_2Z7hiC1Jw8432byZIcOQkJHWm1C5v0K_7s_X11hmg3B0IPu82wmR5n5o9Nl9WwoRfpmrUq08GMeO2z0PgW00z4QSqldQCaLEpU-E-fARNMU1YMkTo/s600/02-dockerfile-naive.png&quot; alt=&quot;The naive Dockerfile for the FastAPI app — five lines starting from the full python:3.12 image&quot; /&gt;&lt;/p&gt;

&lt;h2&gt;Step 3 — Build it and run it&lt;/h2&gt;
&lt;p&gt;One command builds the image, following the recipe line by line (the first build also downloads that Python box, so it takes a while):&lt;/p&gt;
&lt;pre&gt;docker build -f Dockerfile.naive -t notes-api:naive .&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj8TOcco9MR7ZZ3Ljg3tpKHyGTLv4LS6QK5FQMQAUkgVgMgDCPMt8T0RGm0zcBsICMpU1jvuYexVmrOd3cU-eVeuBq8kLPYplqLrZqUy3sXrJnHRuUDAyUv2goe4ShREOzza8tqazuvYe23JGdjLEW1GHx_oTJ_M7H-SiDBAiXHtVQg8_lwkvbz5TJ88zo/s600/03-naive-build-finished.png&quot; alt=&quot;docker build finishing the naive FastAPI image&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Now start it. &lt;code&gt;docker run&lt;/code&gt; takes the packed box and starts it as a running container. A &lt;strong&gt;port&lt;/strong&gt; is a numbered door into the app — &lt;code&gt;-p 8000:8000&lt;/code&gt; connects door 8000 on the outside to door 8000 inside:&lt;/p&gt;
&lt;pre&gt;docker run -d --name notes -p 8000:8000 notes-api:naive&lt;/pre&gt;
&lt;p&gt;And now the proof — we ask the app, over the network:&lt;/p&gt;
&lt;pre&gt;curl http://localhost:8000/health
# {&quot;status&quot;:&quot;ok&quot;}

curl http://localhost:8000/notes
# [{&quot;id&quot;:1,&quot;text&quot;:&quot;Ship it in a container&quot;}]

curl -X POST http://localhost:8000/notes -H &quot;Content-Type: application/json&quot; -d &#39;{&quot;text&quot;:&quot;Docker works&quot;}&#39;
# {&quot;id&quot;:2,&quot;text&quot;:&quot;Docker works&quot;}&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjzp5M7ENhWKp9dVE-XxuFgJHvQLxmCuOqRQ9RPAoRKVMDe5xZaJqtkZp-Tz_0bjsS5su1WSKH33o0lHDt_HY0ETLjPCtt71yO_zFnFcOomVq72ZTFyh2eYnFv28C7J-aerP3yaFyXDJNo2PNokJ9DlmgNZxv4F8W7ba0qPnfAHjwTzdl6XHOcS808Kiyo/s600/04-curl-proof.png&quot; alt=&quot;curl proof: the FastAPI health endpoint answers ok from inside the running Docker container&quot; /&gt;&lt;/p&gt;
&lt;p&gt;It works — exactly as it did on my machine. But look at the price tag:&lt;/p&gt;
&lt;pre&gt;docker images&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhU0ecSky8C8bqf5_QqAOJlRMJ2FnCUboczTiHqrARZbcnnD9YO1TiAF0r_c1tp89cXX36dz8ILLtldIIbf36rDxE329KWqccXStWl6Uejskl0R6_rJx_b_5Wi1pUdzr6pAXtIPRqVcYqbigeo40QCJrXeweTWdRcXWjM0y_nWam68Qz7-mGgwkWVJ9ZgE/s600/05-images-naive.png&quot; alt=&quot;docker images: the naive FastAPI image weighs 1.65 GB&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;1.65 GB — for a notes app of about forty lines.&lt;/strong&gt; The full Python box carries an entire operating system, compilers, and tools we never asked for — and all of it travels to every server, every deploy.&lt;/p&gt;

&lt;h2&gt;Step 4 — Pack properly: slim base, two stages&lt;/h2&gt;
&lt;p&gt;The new recipe fixes the weight in four moves:&lt;/p&gt;
&lt;pre&gt;# ---- Stage 1: install dependencies into a clean prefix ----
FROM python:3.12-slim AS builder
WORKDIR /build
COPY requirements.txt .
RUN pip install --no-cache-dir --prefix=/install -r requirements.txt

# ---- Stage 2: the small runtime image that actually ships ----
FROM python:3.12-slim
RUN useradd --create-home appuser
WORKDIR /app
COPY --from=builder /install /usr/local
COPY --chown=appuser:appuser main.py .
USER appuser
EXPOSE 8000
HEALTHCHECK --interval=30s --timeout=3s \
  CMD python -c &quot;import urllib.request; urllib.request.urlopen(&#39;http://localhost:8000/health&#39;)&quot;
CMD [&quot;uvicorn&quot;, &quot;main:app&quot;, &quot;--host&quot;, &quot;0.0.0.0&quot;, &quot;--port&quot;, &quot;8000&quot;]&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8ewtkbr-I42IoBZM5-uBfIoc00cSx7RYJzxgmSGySgQXKhDLNEz-mGFp_LWhip-KF2PIKG1tf369UoCDFI6YJrpuI2CXcl7InIxOyaKcme_o70qVHA2xKjcNE3lmUe4Wa-yqt2ti0cUyLL_gOzrIE6iIXChm5YoIbId_Ifb0ZXwLD5M14fyZnN4AphE4/s600/06-dockerfile-slim.png&quot; alt=&quot;The slim two-stage Dockerfile: builder stage plus a small python:3.12-slim runtime, non-root user, health check&quot; /&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Slim base.&lt;/strong&gt; &lt;code&gt;python:3.12-slim&lt;/code&gt; is the same Python on a diet: &lt;strong&gt;179 MB instead of 1.62 GB&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Two stages — the big move.&lt;/strong&gt; Stage 1 (&lt;code&gt;builder&lt;/code&gt;) installs the libraries into one clean folder (&lt;code&gt;/install&lt;/code&gt;). Stage 2 starts again from the fresh slim box and copies in &lt;em&gt;only that finished folder&lt;/em&gt; — the builder&#39;s tools and mess never enter the box we ship.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;.dockerignore&lt;/code&gt;&lt;/strong&gt; guards the copy. It lists what stays out of the build context: caches (&lt;code&gt;__pycache__/&lt;/code&gt;, &lt;code&gt;*.pyc&lt;/code&gt;), git history (&lt;code&gt;.git/&lt;/code&gt;), virtualenvs (&lt;code&gt;.venv/&lt;/code&gt;), and the recipes themselves. The whole file:
&lt;pre&gt;__pycache__/
*.pyc
.git/
.venv/
Dockerfile*
docker-compose.yml
README.md&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Non-root user.&lt;/strong&gt; The naive box ran everything as root, the all-powerful account. This box runs as an ordinary user (&lt;code&gt;USER appuser&lt;/code&gt;) — if someone breaks the app, they land in a much smaller room. One catch, learned while making the video: copied files must belong to that user — that is why the &lt;code&gt;COPY&lt;/code&gt; for &lt;code&gt;main.py&lt;/code&gt; names its owner with &lt;code&gt;--chown&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Health check.&lt;/strong&gt; &lt;code&gt;HEALTHCHECK&lt;/code&gt; makes Docker ask the health endpoint every thirty seconds — &lt;em&gt;still alive?&lt;/em&gt; — and report the answer beside the container&#39;s name.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;First, stop the naive container — &lt;code&gt;docker rm -f notes&lt;/code&gt; — so its name and port 8000 are free again. Then rebuild and line the two boxes up side by side:&lt;/p&gt;
&lt;pre&gt;docker build -t notes-api:slim .
docker images&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqTC_0nKmy_X9Ag-6aw4RSS6eh4aIRoT4dv4Iix0G1g196I65nbd3eYRT0_4oTC3OCQ60ZWZ3H1bh_JkSLt9fgh48A5qZtZT9-uaV9yO6DwajvoInCoeIL18ar2iKrMcIat3L2YNLHU1OH742H8FbOQZwCJq3sZd8r_iy7XlH8EZinBSFLWQz7RDZi_l8/s600/07-images-both.png&quot; alt=&quot;docker images side by side: naive FastAPI image 1.65 GB vs slim two-stage image 198 MB&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Naive: 1.65 GB. Slim: 198 MB — more than eight times smaller.&lt;/strong&gt; Same app, same answers: we ran the same three questions against the slim container — health ok, notes listed, note added — and Docker&#39;s own status now adds one word: &lt;strong&gt;healthy&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;Step 5 — The everyday workflow: docker compose&lt;/h2&gt;
&lt;p&gt;Day to day, you will not type the long commands. This compose file — six lines — describes the service once: build it here, call the image this, connect port 8000:&lt;/p&gt;
&lt;pre&gt;services:
  notes:
    build: .
    image: notes-api:slim
    ports:
      - &quot;8000:8000&quot;&lt;/pre&gt;
&lt;p&gt;Then one command builds and starts everything, and one command brings it all down:&lt;/p&gt;
&lt;pre&gt;docker compose up -d
docker compose ps
curl http://localhost:8000/health
docker compose down&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFOD1fUIoLnzW4eA2rzPEnQdsqqpledEgrJPsUI82VhByThCUQ7BY7qEl7w6mZ9mp3kxtPG1-9MNwlgYqktZYKXls7SZuCEKWY--DZFo1DKq0VhAHaaW2-Ea5dpJrtomEIA9VORvm11AtynoBurJLCi7lxfu2m7xFqqodlcoqFVY8kSFsf-fSKStuDCbY/s600/08-compose-ps.png&quot; alt=&quot;docker compose ps: the FastAPI container up and healthy&quot; /&gt;&lt;/p&gt;

&lt;h2&gt;Step 6 — Scan the box before you ship it&lt;/h2&gt;
&lt;p&gt;One last check before we call this box production-shaped: is anything inside it &lt;em&gt;known&lt;/em&gt; to be dangerous? &lt;strong&gt;Trivy&lt;/strong&gt; is a free scanner — it compares every package in the image against the public lists of known holes:&lt;/p&gt;
&lt;pre&gt;trivy image --severity HIGH,CRITICAL notes-api:slim
trivy image --severity HIGH,CRITICAL notes-api:naive&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj2quKrycC0hXBUzOAoCqG-1c_s3jV51MnhvAYe6f-ch8SDLEfR6r0ZMJqfdz29uZW2C24Eg8ZOSr9Mz3lZtXzcaqNDRVYIeHS8bLO4ATLZNn__vvr6sm5Pp5KhcQ1dqjyrxKDXzlooPpLp9ejPVzSXge8rXHtZt8oBmhqImSvZaYS9wsrSXmCJxyqrOgE/s600/09-trivy-summary.png&quot; alt=&quot;Trivy scan summary for the slim FastAPI image: 51 high and critical findings&quot; /&gt;&lt;/p&gt;
&lt;p&gt;The honest result, measured on the recording machine:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Slim image: 51 serious findings&lt;/strong&gt; (high or critical) — 48 in operating-system packages, 3 in a Python library.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Naive image: 505&lt;/strong&gt; — 502 in operating-system packages, and the &lt;em&gt;same&lt;/em&gt; 3 in the Python library.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Two caveats, in plain words. A scan only finds holes that are already known and listed. And shrinking the box does not fix your own libraries: both images carry the same three findings in one Python library (Starlette — the scanner names the fixed version). Those you fix by updating the library. Smaller box, shorter list, less to patch — that is the real win.&lt;/p&gt;

&lt;h2&gt;Save this — the small-box checklist&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Slim base&lt;/strong&gt; — 179 MB, not 1.62 GB.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Two stages&lt;/strong&gt; — build in one stage, ship only the result.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;.dockerignore&lt;/code&gt;&lt;/strong&gt; — keep the junk out.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Non-root user&lt;/strong&gt; — a smaller room if something breaks in.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Health check&lt;/strong&gt; — Docker keeps asking &quot;still alive?&quot;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Same app. Smaller box.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Your turn:&lt;/strong&gt; your image is over a gigabyte — which two changes shrink it the most, and why? Tell me in the comments; I read every one.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Everything in this post was run for real on Docker 29.1.3 (Ubuntu 24.04) on 2026-11-18; image sizes and scan counts are the measured results of that run. Your numbers will be close, not identical — base images and vulnerability lists change over time.&lt;/em&gt;&lt;/p&gt;

&lt;div style=&quot;text-align:center;border:1px solid #e3e6ea;border-radius:8px;padding:18px 14px;margin:28px 0;background:#fbfcfd;&quot;&gt;&lt;h3 style=&quot;margin:0 0 6px;&quot;&gt;Enjoying these tutorials?&lt;/h3&gt;&lt;p style=&quot;margin:0 0 6px;&quot;&gt;Get new Visual Studio Learn posts by email — free, no spam:&lt;/p&gt;&lt;style&gt;
    .followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview {
  display: flex !important;
  flex-direction: column !important;
  justify-content: center !important;
  margin-top: 30px !important;
  padding: clamp(17px, 5%, 40px) clamp(17px, 7%, 50px) !important;
  max-width: none !important;
  border-radius: 6px !important;
  box-shadow: 0 5px 25px rgba(34, 60, 47, 0.25) !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview,
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview *{
  box-sizing: border-box !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-heading {
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-heading h5{
  margin-top: 0 !important;
  margin-bottom: 0 !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field {
  margin-top: 20px !important;
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input {
  width: 100% !important;
  height: 40px !important;
  border-radius: 6px !important;
  border: 2px solid #e9e8e8 !important;
  background-color: #fff;
  outline: none !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input {
  color: #000000 !important;
  font-family: &quot;Montserrat&quot; !important;
  font-size: 14px;
  text-align: center;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input::placeholder {
  color: #000000 !important;
  opacity: 1 !important;
}

.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input:-ms-input-placeholder {
  color: #000000 !important;
}

.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input::-ms-input-placeholder {
  color: #000000 !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button {
  margin-top: 10px !important;
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button button {
  width: 100% !important;
  height: 40px !important;
  border: 0 !important;
  border-radius: 6px !important;
  line-height: 0px !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button button:hover {
  cursor: pointer !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .powered-by-line {
  color: #231f20 !important;
  font-family: &quot;Montserrat&quot; !important;
  font-size: 13px;
  font-weight: 400;
  line-height: 25px;
  text-align: center;
  text-decoration: none !important;
  display: flex;
  width: 100% !important;
  justify-content: center !important;
  align-items: center !important;
  margin-top: 10px !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .powered-by-line img {
  margin-left: 10px;
  height: 1.13em !important;
  max-height: 1.13em !important;
}

  &lt;/style&gt;
  &lt;div class=&quot;followit--follow-form-container&quot; attr-a attr-b attr-c attr-d attr-e attr-f&gt;
    &lt;form data-v-f93f6730=&quot;&quot; action=&quot;https://api.follow.it/subscription-form/SmRpWjEzV1ovU0FOK2R2Y0oxajFBUmtIdW44K1ZiUWZRUm1FZVdpVlRKUHFLSFBKWjE4M1hTSk5yRU5oNmg0RU5zelhuTllNTGU1UmwzRkN0NzZWbnd2YkV5VWFwYnptWFdtekI3eXdwMmhtamtUNit3dlNSblVmRjJxd1JGbjZ8Q05yQWR2eitvYW5HRmNMMzJkbStpM09rNmxXZ0ZjWWhyOERya2VvVXRzZz0=/8&quot; method=&quot;post&quot;&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;form-preview&quot; style=&quot;background-color: rgb(255, 255, 255); border-style: solid; border-width: 1px; border-color: rgb(204, 204, 204); position: relative;&quot;&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;preview-heading&quot;&gt;&lt;h5 data-v-f93f6730=&quot;&quot; style=&quot;font-family: Arial; font-weight: bold; color: rgb(0, 0, 0); font-size: 16px; text-align: center; text-transform: none !important;&quot;&gt;Get new posts by email:&lt;/h5&gt;&lt;/div&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;preview-input-field&quot;&gt;&lt;input data-v-f93f6730=&quot;&quot; type=&quot;email&quot; name=&quot;email&quot; required=&quot;&quot; placeholder=&quot;Enter your email&quot; spellcheck=&quot;false&quot; style=&quot;font-family: Arial; font-weight: normal; color: rgb(0, 0, 0); font-size: 14px; text-align: center; background-color: rgb(255, 255, 255); text-transform: none !important;&quot; /&gt;&lt;/div&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;preview-submit-button&quot;&gt;&lt;button data-v-f93f6730=&quot;&quot; type=&quot;submit&quot; style=&quot;font-family: Arial; font-weight: bold; color: rgb(0, 0, 0); font-size: 16px; text-align: center; background-color: rgb(0, 0, 0); text-transform: none !important;&quot;&gt;Subscribe&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/form&gt;&lt;a href=&quot;https://follow.it&quot; class=&quot;powered-by-line&quot;&gt;Powered by &lt;img src=&quot;https://follow.it/images/colored-logo.svg&quot; alt=&quot;follow.it&quot; height=&quot;17px&quot;/&gt;&lt;/a&gt;
  &lt;/div&gt;&lt;p style=&quot;margin:12px 0 0;font-size:14px;&quot;&gt;Follow along: &lt;a href=&quot;https://www.youtube.com/@VisualStudioLearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/youtube/FF0000&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;YouTube&quot; /&gt; YouTube&lt;/a&gt; · &lt;a href=&quot;https://www.facebook.com/VisualStudio.Learn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/facebook/1877F2&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;Facebook&quot; /&gt; Facebook&lt;/a&gt; · &lt;a href=&quot;https://www.instagram.com/visualstudiolearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/instagram/E4405F&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;Instagram&quot; /&gt; Instagram&lt;/a&gt; · &lt;a href=&quot;https://www.tiktok.com/@visualstudiolearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/tiktok/000000&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;TikTok&quot; /&gt; TikTok&lt;/a&gt; · &lt;a href=&quot;https://www.visualstudiolearn.com/feeds/posts/default?alt=rss&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/rss/FFA500&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;RSS&quot; /&gt; RSS&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://www.visualstudiolearn.com/feeds/3589815936243328349/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.visualstudiolearn.com/2026/11/dockerize-fastapi-app-dockerfile-to.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/3589815936243328349'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/3589815936243328349'/><link rel='alternate' type='text/html' href='http://www.visualstudiolearn.com/2026/11/dockerize-fastapi-app-dockerfile-to.html' title='Dockerize a FastAPI App: Dockerfile to Running Container'/><author><name>The Loonie Lesson</name><uri>http://www.blogger.com/profile/02243798795439866634</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRY4V9cFLhqMxHWBOWCGBiGUqjan14Vda_dy5rz-C41kApceM-u6jAxS2_6AIRZtGkQeyiQolq1gCeSBhpo573lYq37MLmMGgSKDQLasowic65VoitivYCzqrmUDUqxJuqCTyzah8JuALYtW0Iz7rXIvIcj-ovhVpEuEhQJ6yG1FUQ-A/s1600/loonie-lesson-logo-profile.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://img.youtube.com/vi/sdtXMMXrXMI/default.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8861905861585113921.post-3975364747236655993</id><published>2026-11-17T23:09:51.000-05:00</published><updated>2026-11-22T02:51:02.619-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Azure"/><category scheme="http://www.blogger.com/atom/ns#" term="CI/CD"/><category scheme="http://www.blogger.com/atom/ns#" term="FastAPI"/><category scheme="http://www.blogger.com/atom/ns#" term="GitHub Actions"/><category scheme="http://www.blogger.com/atom/ns#" term="Tutorial"/><title type='text'>CI/CD for a FastAPI App — GitHub Actions to Azure App Service</title><content type='html'>&lt;h1&gt;CI/CD for a FastAPI App — GitHub Actions to Azure App Service&lt;/h1&gt;
&lt;p&gt;&lt;strong&gt;Direct answer:&lt;/strong&gt; Put one workflow file in &lt;code&gt;.github/workflows/&lt;/code&gt;. Run your tests on every &lt;code&gt;pull_request&lt;/code&gt;, and deploy to Azure App Service only on a push to &lt;code&gt;main&lt;/code&gt;, after the tests pass, logging in with OIDC so no Azure password is ever stored in GitHub. That is the whole pipeline — this post builds it step by step.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Versions used (November 2026): Python 3.12.3, FastAPI 0.115.6, pytest 8.3.4, actions/checkout@v4, actions/setup-python@v5, azure/login@v2, azure/webapps-deploy@v3.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;What you will build&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhbscnFvJVKYfWM2_I4R-GsoH6mdYZbbAVaO6JGFlLYsmn8hVYuAAu1UdgbANEIO_F1VJBt9RHJzSFywWl96dBhOgMnjD2uys7g1Wpwh3xHGVRALWL9OaJ9YbafOm0hRTgZrT5FMOpk3-ytPJ-Nc_z9yx5aoIw6BgBnRODRWAjupxDn6pdEeTosjTzlJrE/s1600/blog-map.png&quot; alt=&quot;The pipeline map: open a pull request, test gate, merge to main, OIDC login, deploy job, live on Azure&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Two jobs, one gate between them. The &lt;strong&gt;test job&lt;/strong&gt; runs on every pull request and holds no secrets. The &lt;strong&gt;deploy job&lt;/strong&gt; runs only when code lands on &lt;code&gt;main&lt;/code&gt; and only after the test job passes.&lt;/p&gt;
&lt;div style=&quot;text-align:center;margin:20px 0;&quot;&gt;&lt;iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/rZ3wxiJJ9TI&quot; title=&quot;CI/CD for a FastAPI App — GitHub Actions to Azure&quot; frameboarder=&quot;0&quot; allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen&gt;&lt;/iframe&gt;&lt;/div&gt;
&lt;h2&gt;Environment setup (prerequisites first)&lt;/h2&gt;
&lt;ol&gt;&lt;li&gt;A GitHub account and a repository for the app (public or private — the pipeline is identical).&lt;/li&gt;&lt;li&gt;An Azure account. The free account includes an F1 Free App Service plan; the spending limit stays ON. Creating the account: azure.microsoft.com → Free account → sign in → identity + card verification (the card is not charged while the spending limit is on) → done. If any screen asks you to switch to pay-as-you-go, stop — this tutorial never needs it.&lt;/li&gt;&lt;li&gt;Python 3.12 on your own machine for the local run.&lt;/li&gt;&lt;/ol&gt;
&lt;h2&gt;The app (kept tiny on purpose)&lt;/h2&gt;
&lt;pre&gt;import os
from fastapi import FastAPI

app = FastAPI(title=&quot;CI/CD Demo API&quot;)

@app.get(&quot;/&quot;)
def root():
    return {&quot;app&quot;: &quot;CI/CD Demo API&quot;, &quot;message&quot;: &quot;Hello from FastAPI&quot;, &quot;environment&quot;: os.getenv(&quot;APP_ENVIRONMENT&quot;, &quot;local&quot;), &quot;version&quot;: os.getenv(&quot;APP_VERSION&quot;, &quot;dev&quot;)}

@app.get(&quot;/health&quot;)
def health():
    return {&quot;status&quot;: &quot;ok&quot;}

@app.get(&quot;/greet/{name}&quot;)
def greet(name: str):
    return {&quot;greeting&quot;: f&quot;Hello, {name}!&quot;}&lt;/pre&gt;
&lt;p&gt;Pins in &lt;code&gt;requirements.txt&lt;/code&gt;: fastapi==0.115.6, uvicorn==0.34.0, gunicorn==23.0.0, uvicorn-worker==0.2.0. Dev pins (&lt;code&gt;requirements-dev.txt&lt;/code&gt;): pytest==8.3.4, httpx==0.28.1. Three tests in &lt;code&gt;tests/test_main.py&lt;/code&gt; check the three routes. Run them locally first — the exact commands the pipeline will run:&lt;/p&gt;
&lt;pre&gt;pip install -r requirements.txt -r requirements-dev.txt
pytest -v
# 3 passed in 0.35s (measured on the demo machine)&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiXhCRA324oQZKGpUPF3-HfZ7EDQfRN92wbIwEYFecVS_KBRsyAWSLKuZ9PAlBMOjNKYBADskntKBQrahI8iZCb0QwrNVFI7A5TRbnZlDQrjMRqQzqjK3loD7y4sOM17_SppF8yCcupJXgxNa8_OrzjCffmtPA_GlP43mPwu_wHtKa4gOhTJXFS8PC7fH8/s1600/blog-capture-pytest.png&quot; alt=&quot;pytest run on the demo machine: all three tests pass, 3 passed in 0.35s&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgsDaGa_GBVpouOIFxXMDL-FS8wL8bu3T8YBUCWQgWkhz1LwMtHz8mAXvRBtY36-35q6DBGE-OmQZtpAd1QOVOB8IV0x5By-VgBe33v0XS3n-swdFwZLhPEd9iaLGIB8wRLZBjmzzC1TEbru9RFi8lomtMv4BZfT6UHY-wXy_nQpfh9ftk6Mhu1IkTtL6g/s1600/blog-capture-local-replies.png&quot; alt=&quot;The app running locally: root, health and greet endpoints answering with environment local&quot; /&gt;&lt;/p&gt;
&lt;h2&gt;Step 1 — The workflow triggers: pull_request, never pull_request_target&lt;/h2&gt;
&lt;pre&gt;on:
  pull_request:
    branches: [main]
  push:
    branches: [main]&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJ7gPff3kdl2MrOLwcozrT20g88GHtXXU7zjFdh578Va8-pXNEgvE011FDva0Puefa9o4gEMRx2QXfmdL2wUoDqsUXu7kCJPbM9FlSm9uC0IudMld4Pg8AzNFyrhy2FpYz9ZUg4MCeldg_TLWvn6ORKhneZddyklDbFiLZjKHFuripEAVuXEwu-Jg167Y/s1600/blog-capture-yaml-triggers.png&quot; alt=&quot;The workflow triggers in ci-cd.yml: pull_request and push to main, highlighted&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The December 2 change:&lt;/strong&gt; &lt;code&gt;pull_request_target&lt;/code&gt; runs in the base repository&#39;s context, with secrets and a write token, even for fork code — the pattern behind &quot;pwn request&quot; attacks. GitHub&#39;s changelog of November 2, 2026 states that from &lt;strong&gt;December 2, 2026&lt;/strong&gt; it is &lt;strong&gt;blocked by default in public repositories&lt;/strong&gt; unless an Actions event policy explicitly allows it (brownouts ran November 9 and 16). Private/internal repositories and GitHub Enterprise Server are not affected. Plain &lt;code&gt;pull_request&lt;/code&gt; runs the proposed code with no secrets — exactly right for tests. This pipeline never uses &lt;code&gt;pull_request_target&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;Step 2 — The test job (the gate)&lt;/h2&gt;
&lt;pre&gt;  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: &quot;3.12&quot;
      - name: Install dependencies
        run: |
          pip install -r requirements.txt
          pip install -r requirements-dev.txt
      - name: Run tests
        run: pytest -v&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgcaPtWK9_ebJ7aBvt7sikB7ubTrjX4XyndDnb1zyBRCXwTfZJZbrfxpcaJSDHXFSmhKidNo_2KY9pp7OM74wrxFBmyBu2YyCm05ufJJ3iZYwQWSpuGF5lpinXlQELZOcSoeZ0XkGUePbw0ke4pSQtokKu7pSO3W5rxwwJcOqmE5xEfcWc62iYp1NOGw7o/s1600/blog-capture-yaml-testjob.png&quot; alt=&quot;The test job in ci-cd.yml with the pytest step highlighted&quot; /&gt;&lt;/p&gt;
&lt;p&gt;A failing test puts a red cross on the pull request. The job holds no Azure secrets, so untrusted code has nothing to steal.&lt;/p&gt;
&lt;h2&gt;Step 3 — Create the Azure Web App and the OIDC login (one-time)&lt;/h2&gt;
&lt;ol&gt;&lt;li&gt;Portal → Create a resource → Web App: Python 3.12, Linux, F1 Free plan, a globally unique name.&lt;/li&gt;&lt;li&gt;On the app: Configuration → General settings → Startup Command — paste this exact command (it also ships as &lt;code&gt;startup.txt&lt;/code&gt; in the companion code) and Save: &lt;code&gt;gunicorn --bind=0.0.0.0:8000 --timeout 600 --workers 2 -k uvicorn_worker.UvicornWorker main:app --access-logfile &#39;-&#39; --error-logfile &#39;-&#39;&lt;/code&gt;&lt;/li&gt;&lt;li&gt;Create a user-assigned managed identity (or app registration) with a federated credential trusting your GitHub repository, and the Website Contributor role on the app — Microsoft Learn&#39;s &quot;Deploy by using GitHub Actions&quot; page walks this; OIDC is its recommended authentication because the token is short-lived.&lt;/li&gt;&lt;li&gt;In GitHub: Settings → Secrets and variables → Actions → New repository secret, three times: &lt;code&gt;AZURE_CLIENT_ID&lt;/code&gt;, &lt;code&gt;AZURE_TENANT_ID&lt;/code&gt;, &lt;code&gt;AZURE_SUBSCRIPTION_ID&lt;/code&gt;. These are identifiers, not passwords. Also add a variable &lt;code&gt;AZURE_WEBAPP_NAME&lt;/code&gt; with your app&#39;s name. Never paste a publish profile or password into a workflow file.&lt;/li&gt;&lt;/ol&gt;
&lt;p&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjfKsjkUfN4rlrFTk8QntP9ooLT5JJYl2AI3rrJtXwenTvt0t1MhYo2M42MrNaXQillHIB5H1IMLNx5YKssTz81OXkT72i64DJc4oq3ZThRLmZRSkdFtUOhtZC7nt5ZAOo1JT3Uw2FphsiqjW7jzO6ext_TZYmYxbA5pxFtdxAWDtIemxB936f9j2_mGQA/s1600/portal-overview.png&quot; alt=&quot;The demo Web App in the Azure portal: Status Running, Python 3.12 runtime, F1 Free plan&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;The demo Web App created for this post (Azure portal, November 17, 2026): Status “Running”, Python 3.12 on Linux, F1 Free plan. The subscription ID is blanked out — never share yours.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;Step 4 — The deploy job (push to main only, after tests)&lt;/h2&gt;
&lt;pre&gt;  deploy:
    if: github.event_name == &#39;push&#39; &amp;amp;&amp;amp; github.ref == &#39;refs/heads/main&#39;
    needs: test
    runs-on: ubuntu-latest
    environment: production
    permissions:
      contents: read
      id-token: write
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: &quot;3.12&quot;
      - name: Install dependencies (for the package)
        run: pip install -r requirements.txt -t .
      - name: Log in to Azure with OIDC (no stored password)
        uses: azure/login@v2
        with:
          client-id: ${{ secrets.AZURE_CLIENT_ID }}
          tenant-id: ${{ secrets.AZURE_TENANT_ID }}
          subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
      - name: Deploy to Azure App Service
        uses: azure/webapps-deploy@v3
        with:
          app-name: ${{ vars.AZURE_WEBAPP_NAME }}
          package: .&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjWal-vuYEo0-00xM1zSKkQBA3_JJHymKUn_e3ayW1bGzpAKZuexLANweUg_dpOM_mr4kr3YUdlbjyubQvmlpB27TE9NxMdmKOLzKbIJiO-EVqx_A2EQrPQxayLf0ZRbYw_-LzMUBGVYwVhO3rATDwlsrb7ZaPSc7e0cB-Y7PVQqPa8lZEC18zCBbtHmCg/s1600/blog-capture-yaml-deployjob.png&quot; alt=&quot;The deploy job in ci-cd.yml with the azure/login and webapps-deploy steps highlighted&quot; /&gt;&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;pip install -r requirements.txt -t .&lt;/code&gt; step installs the app&#39;s dependencies into the package folder itself, so the zip Azure receives already contains everything the app needs to start. &lt;code&gt;permissions: id-token: write&lt;/code&gt; on the deploy job is what lets GitHub mint the OIDC token that &lt;code&gt;azure/login&lt;/code&gt; exchanges — without it the login fails every time; the test job deliberately does not have it.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;needs: test&lt;/code&gt; is the gate in code: no green tests, no deploy. &lt;code&gt;environment: production&lt;/code&gt; lets you add a required reviewer in GitHub&#39;s environment settings if you want a human tap before production changes.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhqkwGxlvDkoiGeIG1syV8E8SLLaMAKgaQ495I9F-ktTdu2_18Of07v5DDOUmn9rGyVmcpsTub7rcyjVzUfhjkPXQQtbKUD15azmy9DGT6Xy_Oso9dJfsE5QCKi5jxeA1xPuRK_MitBT8MP5neh51y9rhWnXBAFyB8bYVTKETOIlSLQU7yt5tF5piRT_2A/s1600/kudu-success-main.png&quot; alt=&quot;App Service deployment log: Deployment Successful&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6ObuEptFLPsm4gIwVhL1pJ1cn5QkEgg9-U0oAjBUs9_6EhC8Z8INOxdkls5UMU47beJA_2TuWIUaNf2JKxkMOSdonFoxP33PqksUxr9xeej_tOXlcIb8mvSpmZuaZ5P-SWB76CEZwTmBcXZpS3_XQEuQEY-mzhgVStamruxrnkm2K2QkEQwgEpXB9tOY/s1600/blog-live-endpoints.png&quot; alt=&quot;The deployed app answering live: root, health and greet endpoints with environment production&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Proof run for this post (November 17, 2026): the exact app package this workflow ships was deployed to the demo Web App, the build finished with “Deployment Successful”, and the three endpoints were verified live — note &lt;code&gt;&quot;environment&quot;:&quot;production&quot;&lt;/code&gt;, which comes from the app setting saved in Step 3. The demo resource group was deleted right after these captures were taken.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;A short, honest note on Agentic Workflows&lt;/h2&gt;
&lt;p&gt;GitHub Agentic Workflows became generally available on October 28, 2026: you describe a repository task in a Markdown file, &lt;code&gt;gh aw compile&lt;/code&gt; turns it into a normal Actions workflow, and an AI agent runs it — issue triage and CI-failure investigation are the starter uses. Its guardrails are the part worth copying: read-only access by default and actions only through approved &quot;safe outputs&quot;. The same posture exists for coding agents run unattended — for example Claude Code&#39;s &lt;code&gt;permissions.denyInsteadOfPrompting&lt;/code&gt; (added in v2.1.319): a call that would have waited for your approval is denied and the run continues, instead of being silently approved. Keep agents away from your deploy job until they have earned it.&lt;/p&gt;
&lt;h2&gt;Errors you may hit&lt;/h2&gt;
&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Deploy &quot;succeeds&quot; but you see the Azure welcome page:&lt;/strong&gt; the startup command is empty, so FastAPI never started. Set the gunicorn startup command (Step 3) and restart the app.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;azure/login fails:&lt;/strong&gt; one of the three identifiers is wrong, or the federated credential names a different repository/branch. The failure happens before anything deploys — the safe direction.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Tests pass locally, fail in Actions:&lt;/strong&gt; an unpinned dependency moved. Pin everything, exactly as in the companion code.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;A workflow using pull_request_target stops running in a public repo after December 2, 2026:&lt;/strong&gt; that is the new default block, not a bug in your YAML. Move test work to plain pull_request, or have an admin create an event policy allowing the specific workflow.&lt;/li&gt;&lt;/ul&gt;
&lt;h2&gt;Get the code&lt;/h2&gt;
&lt;p&gt;The companion code for this post — app, tests, workflow, startup command and README — ships in the public Visual Studio Learn code repository: &lt;a href=&quot;https://github.com/sajjad037/visual-studio-learn-code/tree/main/topic-13-cicd-fastapi-azure&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;the topic-13-cicd-fastapi-azure folder&lt;/a&gt;. Copy the folder&#39;s contents to the root of your own repository, so the workflow lands at &lt;code&gt;.github/workflows/&lt;/code&gt; in your repo — that placement is what Step 1 assumes.&lt;/p&gt;
&lt;h2&gt;Sources&lt;/h2&gt;
&lt;ul&gt;&lt;li&gt;GitHub Changelog, Nov 2, 2026 — pull_request_target blocked by default December 2.&lt;/li&gt;&lt;li&gt;GitHub Changelog, Oct 28, 2026 — GitHub Agentic Workflows generally available.&lt;/li&gt;&lt;li&gt;Microsoft Learn — Deploy by using GitHub Actions (Azure App Service): OIDC recommended, azure/login@v2, azure/webapps-deploy@v3, Website Contributor.&lt;/li&gt;&lt;li&gt;Claude Code changelog v2.1.319 — permissions.denyInsteadOfPrompting.&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;Disclosure: Visual Studio Learn is reader-supported. Some links may be affiliate links, which means the channel may earn a small commission at no extra cost to you. Only tools actually used in the tutorials are ever linked, and affiliate relationships never change a verdict.&lt;/p&gt;
&lt;div style=&quot;text-align:center;border:1px solid #e3e6ea;border-radius:8px;padding:18px 14px;margin:28px 0;background:#fbfcfd;&quot;&gt;&lt;h3 style=&quot;margin:0 0 6px;&quot;&gt;Enjoying these tutorials?&lt;/h3&gt;&lt;p style=&quot;margin:0 0 6px;&quot;&gt;Get new Visual Studio Learn posts by email — free, no spam:&lt;/p&gt;&lt;style&gt;
    .followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview {
  display: flex !important;
  flex-direction: column !important;
  justify-content: center !important;
  margin-top: 30px !important;
  padding: clamp(17px, 5%, 40px) clamp(17px, 7%, 50px) !important;
  max-width: none !important;
  border-radius: 6px !important;
  box-shadow: 0 5px 25px rgba(34, 60, 47, 0.25) !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview,
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview *{
  box-sizing: border-box !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-heading {
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-heading h5{
  margin-top: 0 !important;
  margin-bottom: 0 !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field {
  margin-top: 20px !important;
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input {
  width: 100% !important;
  height: 40px !important;
  border-radius: 6px !important;
  border: 2px solid #e9e8e8 !important;
  background-color: #fff;
  outline: none !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input {
  color: #000000 !important;
  font-family: &quot;Montserrat&quot; !important;
  font-size: 14px;
  font-weight: 400;
  line-height: 20px;
  text-align: center;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input::placeholder {
  color: #000000 !important;
  opacity: 1 !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input:-ms-input-placeholder {
  color: #000000 !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input::-ms-input-placeholder {
  color: #000000 !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button {
  margin-top: 10px !important;
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button button {
  width: 100% !important;
  height: 40px !important;
  border: 0 !important;
  border-radius: 6px !important;
  line-height: 0px;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button button:hover {
  cursor: pointer !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .powered-by-line {
  color: #231f20 !important;
  font-family: &quot;Montserrat&quot; !important;
  font-size: 13px !important;
  font-weight: 400;
  line-height: 20px;
  text-align: center !important;
  text-decoration: none !important;
  display: flex !important;
  width: 100% !important;
  justify-content: center !important;
  align-items: center !important;
  margin-top: 10px !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .powered-by-line img {
  margin-left: 10px !important;
  height: 1.13em !important;
  max-height: 1.13em !important;
}
  &lt;/style&gt;
  &lt;div class=&quot;followit--follow-form-container&quot; attr-a attr-b attr-c attr-d attr-e attr-f&gt;
    &lt;form data-v-f93f6730=&quot;&quot; action=&quot;https://api.follow.it/subscription-form/SmRpWjEzV1ovU0FOK2R2Y0oxajFBUmtIdW44K1ZiUWZRUm1FZVdpVlRKUHFLSFBKWjE4M1hTSk5yRU5oNmg0RU5zelhuTllNTGU1UmwzRkN0NzZWbnd2YkV5VWFwYnptWFdtekI3eXdwMmhtamtUNit3dlNSblVmRjJxd1JGbjZ8Q05yQWR2eitvYW5HRmNMMzJkbStpM09rNmxXZ0ZjWWhyOERya2VvVXRzZz0=/8&quot; method=&quot;post&quot;&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;form-preview&quot; style=&quot;background-color: rgb(255, 255, 255); border-style: solid; border-width: 1px; border-color: rgb(204, 204, 204); position: relative;&quot;&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;preview-heading&quot;&gt;&lt;h5 data-v-f93f6730=&quot;&quot; style=&quot;font-family: Arial; font-weight: bold; color: rgb(0, 0, 0); font-size: 16px; text-align: center; text-transform: none !important;&quot;&gt;Get new posts by email:&lt;/h5&gt;&lt;/div&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;preview-input-field&quot;&gt;&lt;input data-v-f93f6730=&quot;&quot; type=&quot;email&quot; name=&quot;email&quot; required=&quot;&quot; placeholder=&quot;Enter your email&quot; spellcheck=&quot;false&quot; style=&quot;font-family: Arial; font-size: 14px; text-align: center; background-color: rgb(255, 255, 255); text-transform: none !important;&quot; /&gt;&lt;/div&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;preview-submit-button&quot;&gt;&lt;button data-v-f93f6730=&quot;&quot; type=&quot;submit&quot; style=&quot;font-family: Arial; font-weight: bold; color: rgb(255, 255, 255); font-size: 16px; text-align: center; background-color: rgb(0, 0, 0); text-transform: none !important;&quot;&gt;Subscribe&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/form&gt;&lt;a href=&quot;https://follow.it&quot; class=&quot;powered-by-line&quot;&gt;Powered by &lt;img src=&quot;https://follow.it/images/colored-logo.svg&quot; alt=&quot;follow.it&quot; height=&quot;17px&quot;/&gt;&lt;/a&gt;
  &lt;/div&gt;&lt;p style=&quot;margin:12px 0 0;font-size:14px;&quot;&gt;Follow along: &lt;a href=&quot;https://www.youtube.com/@VisualStudioLearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/youtube/FF0000&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;YouTube&quot; /&gt; YouTube&lt;/a&gt; · &lt;a href=&quot;https://www.facebook.com/VisualStudio.Learn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/facebook/1877F2&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;Facebook&quot; /&gt; Facebook&lt;/a&gt; · &lt;a href=&quot;https://www.instagram.com/visualstudiolearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/instagram/E4405F&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;Instagram&quot; /&gt; Instagram&lt;/a&gt; · &lt;a href=&quot;https://www.tiktok.com/@visualstudiolearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/tiktok/000000&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;TikTok&quot; /&gt; TikTok&lt;/a&gt; · &lt;a href=&quot;https://www.visualstudiolearn.com/feeds/posts/default?alt=rss&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/rss/FFA500&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;RSS&quot; /&gt; RSS&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.visualstudiolearn.com/feeds/3975364747236655993/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.visualstudiolearn.com/2026/11/cicd-for-fastapi-app-github-actions-to.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/3975364747236655993'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/3975364747236655993'/><link rel='alternate' type='text/html' href='http://www.visualstudiolearn.com/2026/11/cicd-for-fastapi-app-github-actions-to.html' title='CI/CD for a FastAPI App — GitHub Actions to Azure App Service'/><author><name>The Loonie Lesson</name><uri>http://www.blogger.com/profile/02243798795439866634</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRY4V9cFLhqMxHWBOWCGBiGUqjan14Vda_dy5rz-C41kApceM-u6jAxS2_6AIRZtGkQeyiQolq1gCeSBhpo573lYq37MLmMGgSKDQLasowic65VoitivYCzqrmUDUqxJuqCTyzah8JuALYtW0Iz7rXIvIcj-ovhVpEuEhQJ6yG1FUQ-A/s1600/loonie-lesson-logo-profile.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhbscnFvJVKYfWM2_I4R-GsoH6mdYZbbAVaO6JGFlLYsmn8hVYuAAu1UdgbANEIO_F1VJBt9RHJzSFywWl96dBhOgMnjD2uys7g1Wpwh3xHGVRALWL9OaJ9YbafOm0hRTgZrT5FMOpk3-ytPJ-Nc_z9yx5aoIw6BgBnRODRWAjupxDn6pdEeTosjTzlJrE/s72-c/blog-map.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8861905861585113921.post-5111942251889447654</id><published>2026-11-16T16:36:25.000-05:00</published><updated>2026-11-22T02:51:01.419-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Interview"/><category scheme="http://www.blogger.com/atom/ns#" term="sql server"/><title type='text'>Interview Questions for Developers — Plain-Word Answers, One Question at a Time</title><content type='html'>&lt;p&gt;&lt;strong&gt;Interview questions stop being scary the moment someone explains them from the very beginning, in plain words.&lt;/strong&gt; That is exactly what this page does. Every Monday and Wednesday we take one real interview question for developers, explain it from first principles — no assumed background, no jargon before its time — and finish with the short version you can actually say out loud in the interview room. Every answer also comes with a two-minute video, linked right under the question.&lt;/p&gt;

&lt;p&gt;Bookmark this page and come back: it grows by one question every Monday and Wednesday, and the newest question always sits at the top. Everything here is free, in full — no sign-up, no gated answers.&lt;/p&gt;

&lt;h2&gt;The questions so far&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#q2&quot;&gt;Q2 · Walk me through RAG end to end — and if the answers are wrong, where do you look first?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#q1&quot;&gt;Q1. Clustered vs non-clustered index — what&#39;s the actual difference?&lt;/a&gt; (SQL Server)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;q2&quot;&gt;Q2 · Walk me through RAG end to end. If users say the answers are wrong, where do you look first?&lt;/h2&gt;
&lt;p&gt;&lt;em&gt;AI engineering · RAG · added with Interview Short #2&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Start from what the letters mean. &lt;strong&gt;RAG — retrieval-augmented generation — means: fetch first, then write.&lt;/strong&gt; A plain language model answers from memory, the way a candidate answers a closed-book exam: fluent, confident, and sometimes inventing. RAG works like an &lt;strong&gt;open-book researcher&lt;/strong&gt;: before writing a word, it fetches the pages that matter and answers from them — pages you can check.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Phase one — build the library, once, before any question.&lt;/strong&gt; Your documents — manuals, help pages, policies — are cut into small chunks. Each chunk becomes an &lt;strong&gt;embedding&lt;/strong&gt;: its meaning written as a list of numbers. Those numbers live in a &lt;strong&gt;vector store&lt;/strong&gt;, the index that searches by meaning, not by matching words. Two chunks about the same thing end up with numbers that sit close together, even if they share no words.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Phase two — answer a question.&lt;/strong&gt; The question is embedded the same way — the same embedding model, into numbers on the same map. The store retrieves the top few chunks whose numbers sit closest (three to five is typical). Those chunks are pasted into the prompt, and the model answers from them — not from memory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now the part that wins the interview: the answers are wrong — where do you look first?&lt;/strong&gt; At retrieval. Print the chunks the system actually fetched. If the right page is missing from that printout, the model never stood a chance — it can only use what it is handed. Then work down the chain in order: &lt;strong&gt;chunking&lt;/strong&gt; (were ideas torn in half when the pages were cut?), &lt;strong&gt;embeddings&lt;/strong&gt; (one model for documents and questions — two models means two different maps), &lt;strong&gt;the prompt&lt;/strong&gt; (are the chunks inside it, ordered to use only them?), and only then &lt;strong&gt;the model&lt;/strong&gt; — blamed last, because most &quot;model problems&quot; turn out to be fetching problems.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The one-line summary:&lt;/strong&gt; build the library once; embed the question; fetch the closest chunks; answer from the pages. Wrong answer? Check in order — retrieval, chunking, embeddings, prompt, model last.&lt;/p&gt;

&lt;div style=&quot;background:#f4f6fb;border-left:4px solid #f5b301;padding:12px 16px;margin:16px 0;&quot;&gt;
&lt;strong&gt;Say it in the room:&lt;/strong&gt;&lt;br /&gt;
&quot;RAG retrieves the passages most relevant to the question and puts them in the prompt, so the model answers from supplied evidence. When answers are wrong, I check retrieval first — then chunking, embeddings, prompt — and the model last.&quot;
&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Your turn:&lt;/strong&gt; A chatbot keeps quoting last year&#39;s prices. You print the pages it fetched — every one is from last year&#39;s catalogue. What do you fix first, the model or the retrieval? &lt;em&gt;(Answer: the retrieval — the model answered faithfully from stale pages; the fetching is what failed. Swapping the model would change nothing.)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;▶ &lt;a href=&quot;https://www.youtube.com/shorts/Z3RhPSn2zsI&quot;&gt;Watch the 2-minute Short&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;q1&quot;&gt;Q1. Clustered vs non-clustered index — what&#39;s the actual difference?&lt;/h2&gt;
&lt;p&gt;&lt;em&gt;Topic: SQL Server · From Interview Question Short #1, published 16 November 2026&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Start with the table itself. A table is rows of data, stored on pages. When the database needs one particular row, it has two choices: read every row one by one until it finds the right one — slow — or use an &lt;strong&gt;index&lt;/strong&gt;, something that helps it jump straight to the row it needs. Both index types in this question do that job. The real difference is &lt;em&gt;where the data lives&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A clustered index is the table, sorted.&lt;/strong&gt; It does not point at the rows from somewhere else — it &lt;em&gt;is&lt;/em&gt; the rows, stored in the order of the index key. Think of the pages of a book, printed in chapter order: page 40 simply comes after page 39, because that is where it physically sits. And because the data rows themselves can only be sorted in one order, a table can have &lt;strong&gt;exactly one&lt;/strong&gt; clustered index.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A non-clustered index is a separate list.&lt;/strong&gt; It holds the values it sorts by, in order — and next to each value, a pointer to the real row back in the table. That is exactly the index at the back of a book: a topic, then a page number. The book&#39;s pages never move, and one book can carry several different lists at the back. A SQL Server table can have &lt;strong&gt;up to 999&lt;/strong&gt; non-clustered indexes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now the detail interviewers love to probe: what exactly is that pointer?&lt;/strong&gt; Its proper name is a &lt;em&gt;row locator&lt;/em&gt;. If the table has a clustered index, the pointer is the row&#39;s clustering key value — the non-clustered index looks up the key, and the clustered index leads to the row. If the table has no clustered index, the table is called a &lt;em&gt;heap&lt;/em&gt;, and the pointer is a row ID — the row&#39;s exact physical address.&lt;/p&gt;

&lt;table&gt;
&lt;tr&gt;&lt;th&gt;&lt;/th&gt;&lt;th&gt;Clustered index&lt;/th&gt;&lt;th&gt;Non-clustered index&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;What it is&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;The table itself — rows stored in key order&lt;/td&gt;&lt;td&gt;A separate sorted list, apart from the table&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;How it reaches a row&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;The row is right there, in order&lt;/td&gt;&lt;td&gt;Key plus a pointer (the row locator) to the row&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;strong&gt;How many per table&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;Exactly one&lt;/td&gt;&lt;td&gt;Many — up to 999 in SQL Server&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;

&lt;div style=&quot;border:1px solid #e3e6ea;border-left:4px solid #2b6cb0;border-radius:6px;padding:12px 14px;margin:16px 0;background:#f7fafc;&quot;&gt;
&lt;p style=&quot;margin:0 0 6px;&quot;&gt;&lt;strong&gt;The interview version — say it like this (about 20 seconds):&lt;/strong&gt;&lt;/p&gt;
&lt;p style=&quot;margin:0;&quot;&gt;&quot;A clustered index is the table: its leaf level is the data rows, stored in key order, so there can be only one. A non-clustered index is separate: it stores the key plus a row locator — the clustering key, or a row ID on a heap — so a table can have many.&quot;&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Your turn:&lt;/strong&gt; a table is stored in order by order date. A second index sorts customers by name, with pointers back to the rows. Which one is the non-clustered index — and why? &lt;em&gt;Answer: the name index. The table&#39;s own storage order (by order date) is the clustered side of the story; the name index is a separate list whose entries point back to rows that live in a different order — which is exactly what makes it non-clustered.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;▶ &lt;strong&gt;Watch the 2-minute Short:&lt;/strong&gt; &lt;a href=&quot;https://www.youtube.com/shorts/4b2JWVrstq8&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Clustered vs Non-Clustered Index — What&#39;s the Actual Difference?&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Want to see indexes earn their keep in a real diagnosis? Our step-by-step tutorial &lt;a href=&quot;https://www.visualstudiolearn.com/2026/11/why-is-my-sql-server-slow-4-step.html&quot;&gt;Why Is My SQL Server Slow?&lt;/a&gt; watches a query drop from 12,388 page reads to 15 once the right index exists.&lt;/p&gt;

&lt;h2&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Microsoft Learn — &lt;a href=&quot;https://learn.microsoft.com/en-us/sql/relational-databases/indexes/clustered-and-nonclustered-indexes-described&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Clustered and nonclustered indexes&lt;/a&gt;: a clustered index sorts and stores the data rows themselves, so there can be only one per table; a table with no clustered index is a heap; a non-clustered index has a structure separate from the data rows, and its pointer to a row is called a row locator — the clustered index key for a clustered table, a pointer to the row for a heap.&lt;/li&gt;
&lt;li&gt;Microsoft Learn — &lt;a href=&quot;https://learn.microsoft.com/en-us/sql/relational-databases/indexes/create-nonclustered-indexes&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Create nonclustered indexes&lt;/a&gt;: the maximum number of nonclustered indexes per table is 999.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Disclosure: Visual Studio Learn is reader-supported. Some links may be affiliate links, which means the channel may earn a small commission at no extra cost to you. Only tools actually used in the tutorials are ever linked, and affiliate relationships never change a verdict.&lt;/p&gt;

&lt;div style=&quot;text-align:center;border:1px solid #e3e6ea;border-radius:8px;padding:18px 14px;margin:28px 0;background:#fbfcfd;&quot;&gt;&lt;h3 style=&quot;margin:0 0 6px;&quot;&gt;Enjoying these tutorials?&lt;/h3&gt;&lt;p style=&quot;margin:0 0 6px;&quot;&gt;Get new Visual Studio Learn posts by email — free, no spam:&lt;/p&gt;&lt;style&gt;
    .followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview {
  display: flex !important;
  flex-direction: column !important;
  justify-content: center !important;
  margin-top: 30px !important;
  padding: clamp(17px, 5%, 40px) clamp(17px, 7%, 50px) !important;
  max-width: none !important;
  border-radius: 6px !important;
  box-shadow: 0 5px 25px rgba(34, 60, 47, 0.25) !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview,
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview *{
  box-sizing: border-box !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-heading {
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-heading h5{
  margin-top: 0 !important;
  margin-bottom: 0 !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field {
  margin-top: 20px !important;
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input {
  width: 100% !important;
  height: 40px !important;
  border-radius: 6px !important;
  border: 2px solid #e9e8e8 !important;
  background-color: #fff;
  outline: none !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input {
  color: #000000 !important;
  font-family: &quot;Montserrat&quot; !important;
  font-size: 14px;
  font-weight: 400;
  line-height: 20px;
  text-align: center;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input::placeholder {
  color: #000000 !important;
  opacity: 1 !important;
}

.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input:-ms-input-placeholder {
  color: #000000 !important;
}

.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input::-ms-input-placeholder {
  color: #000000 !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button {
  margin-top: 10px !important;
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button button {
  width: 100% !important;
  height: 40px !important;
  border: 0 !important;
  border-radius: 6px !important;
  line-height: 0px !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button button:hover {
  cursor: pointer !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .powered-by-line {
  color: #231f20 !important;
  font-family: &quot;Montserrat&quot; !important;
  font-size: 13px !important;
  font-weight: 400;
  line-height: 25px !important;
  text-align: center !important;
  text-decoration: none !important;
  display: flex !important;
  width: 100% !important;
  justify-content: center !important;
  align-items: center !important;
  margin-top: 10px !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .powered-by-line img {
  margin-left: 10px !important;
  height: 1.13em !important;
  max-height: 1em !important;
  max-height: 1.13em !important;
}

  &lt;/style&gt;
  &lt;div class=&quot;followit--follow-form-container&quot; attr-a attr-b attr-c attr-d attr-e attr-f&gt;
    &lt;form data-v-f93f6730=&quot;&quot; action=&quot;https://api.follow.it/subscription-form/SmRpWjEzV1ovU0FOK2R2Y0oxajFBUmtIdW44K1ZiUWZRUm1FZVdpVlRKUHFLSFBKWjE4M1hTSk5yRU5oNmg0RU5zelhuTllNTGU1UmwzRkN0NzZWbnd2YkV5VWFwYnptWFdtekI3eXdwMmhtamtUNit3dlNSblVmRjJxd1JGbjZ8Q05yQWR2eitvYW5HRmNMMzJkbStpM09rNmxXZ0ZjWWhyOERya2VvVXRzZz0=/8&quot; method=&quot;post&quot;&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;form-preview&quot; style=&quot;background-color: rgb(255, 255, 255); border-style: solid; border-width: 1px; border-color: rgb(204, 204, 204); position: relative;&quot;&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;preview-heading&quot;&gt;&lt;h5 data-v-f93f6730=&quot;&quot; style=&quot;font-family: Arial; font-weight: bold; color: rgb(0, 0, 0); font-size: 16px; text-align: center; text-transform: none !important;&quot;&gt;Get new posts by email:&lt;/h5&gt;&lt;/div&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;preview-input-field&quot;&gt;&lt;input data-v-f93f6730=&quot;&quot; type=&quot;email&quot; name=&quot;email&quot; required=&quot;&quot; placeholder=&quot;Enter your email&quot; spellcheck=&quot;false&quot; style=&quot;font-family: Arial; font-weight: normal; color: rgb(0, 0, 0); font-size: 14px; text-align: center; background-color: rgb(255, 255, 255); text-transform: none !important;&quot; /&gt;&lt;/div&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;preview-submit-button&quot;&gt;&lt;button data-v-f93f6730=&quot;&quot; type=&quot;submit&quot; style=&quot;font-family: Arial; font-weight: bold; color: rgb(255, 255, 255); font-size: 16px; text-align: center; background-color: rgb(0, 0, 0); text-transform: none !important;&quot;&gt;Subscribe&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/form&gt;&lt;a href=&quot;https://follow.it&quot; class=&quot;powered-by-line&quot;&gt;Powered by &lt;img src=&quot;https://follow.it/images/colored-logo.svg&quot; alt=&quot;follow.it&quot; height=&quot;17px&quot;/&gt;&lt;/a&gt;
  &lt;/div&gt;&lt;p style=&quot;margin:12px 0 0;font-size:14px;&quot;&gt;Follow along: &lt;a href=&quot;https://www.youtube.com/@VisualStudioLearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/youtube/FF0000&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;YouTube&quot; /&gt; YouTube&lt;/a&gt; · &lt;a href=&quot;https://www.facebook.com/VisualStudio.Learn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/facebook/1877F2&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;Facebook&quot; /&gt; Facebook&lt;/a&gt; · &lt;a href=&quot;https://www.instagram.com/visualstudiolearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/instagram/E4405F&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;Instagram&quot; /&gt; Instagram&lt;/a&gt; · &lt;a href=&quot;https://www.tiktok.com/@visualstudiolearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/tiktok/000000&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;TikTok&quot; /&gt; TikTok&lt;/a&gt; · &lt;a href=&quot;https://www.visualstudiolearn.com/feeds/posts/default?alt=rss&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/rss/FFA500&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;RSS&quot; /&gt; RSS&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.visualstudiolearn.com/feeds/5111942251889447654/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.visualstudiolearn.com/2026/11/interview-questions-for-developers.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/5111942251889447654'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/5111942251889447654'/><link rel='alternate' type='text/html' href='http://www.visualstudiolearn.com/2026/11/interview-questions-for-developers.html' title='Interview Questions for Developers — Plain-Word Answers, One Question at a Time'/><author><name>The Loonie Lesson</name><uri>http://www.blogger.com/profile/02243798795439866634</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRY4V9cFLhqMxHWBOWCGBiGUqjan14Vda_dy5rz-C41kApceM-u6jAxS2_6AIRZtGkQeyiQolq1gCeSBhpo573lYq37MLmMGgSKDQLasowic65VoitivYCzqrmUDUqxJuqCTyzah8JuALYtW0Iz7rXIvIcj-ovhVpEuEhQJ6yG1FUQ-A/s1600/loonie-lesson-logo-profile.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8861905861585113921.post-628846896233274021</id><published>2026-11-15T07:09:12.000-05:00</published><updated>2026-11-22T02:51:00.147-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Database"/><category scheme="http://www.blogger.com/atom/ns#" term="Performance"/><category scheme="http://www.blogger.com/atom/ns#" term="sql server"/><category scheme="http://www.blogger.com/atom/ns#" term="Tutorial"/><title type='text'>Why Is My SQL Server Slow? A 4-Step Diagnosis with Blocking, Wait Stats, Execution Plans and Missing Indexes (2026)</title><content type='html'>&lt;p&gt;&lt;strong&gt;Your SQL Server is slow — but &quot;slow&quot; is a symptom, not a diagnosis.&lt;/strong&gt; In this tutorial you will diagnose one genuinely slow query the way a mechanic finds a car fault: with &lt;strong&gt;four checks, in a fixed order&lt;/strong&gt;, each producing evidence instead of a guess. The four checks are: &lt;strong&gt;(1) is anything blocking the query, (2) what is the server waiting on, (3) what does the execution plan say, and (4) which index is missing.&lt;/strong&gt; Our test query starts out touching &lt;strong&gt;12,388 pages&lt;/strong&gt; to return about 2,061 rows. By the end of this post it touches &lt;strong&gt;15 pages&lt;/strong&gt; — and you will know exactly which check found the problem, because every number here was measured on the demo server while writing this post.&lt;/p&gt;

&lt;div style=&quot;position:relative;padding-bottom:56.25%;height:0;overflow:hidden;margin:24px 0;&quot;&gt;&lt;iframe src=&quot;https://www.youtube.com/embed/jSroBM7mijs&quot; style=&quot;position:absolute;top:0;left:0;width:100%;height:100%;border:0;&quot; allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share&quot; title=&quot;Why Is My SQL Server Slow? Four Checks, In Order&quot;&gt;&lt;/iframe&gt;&lt;/div&gt;&lt;h2&gt;What you need&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;A SQL Server you can practise on — this post uses &lt;strong&gt;SQL Server 2025 (17.x)&lt;/strong&gt;, measured build 17.0.5065.9 (RTM-CU11) on Linux, driven from a terminal with &lt;code&gt;sqlcmd&lt;/code&gt;. Every query below also runs unchanged in a query window in &lt;strong&gt;SQL Server Management Studio (SSMS) 22&lt;/strong&gt; on Windows — a section near the end maps each step to SSMS.&lt;/li&gt;
&lt;li&gt;A throwaway database you may break. We create one below called &lt;code&gt;DemoDb&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Permission to see the server&#39;s diagnostic views: the wait and blocking views used here need the &lt;code&gt;VIEW SERVER PERFORMANCE STATE&lt;/code&gt; permission (on your own demo instance, your admin login already has it).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;No downloads beyond the database engine itself. Everything in this diagnosis is built into SQL Server.&lt;/p&gt;

&lt;h2&gt;The whole method, on one screen&lt;/h2&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiYpT-sVSb2vJPJV8tdGnaZi7O3v1F78418Qm9hZOxCshGybDIvCO43Q3q5gZ_tg7c6WwZtsq552C4iYVc8FSZGgPNeTnRm029818BbVjoJ9LLcMB_qDhItErAJbSmumlYoNNyCz8kIaNUpx-GZL2mR-WzUdl_I6asVhX_jxB7K-51TKZOlKu7xmOl8-LQ/s1600/MAP.png&quot; style=&quot;display: block; padding: 1em 0; text-align: center; &quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiYpT-sVSb2vJPJV8tdGnaZi7O3v1F78418Qm9hZOxCshGybDIvCO43Q3q5gZ_tg7c6WwZtsq552C4iYVc8FSZGgPNeTnRm029818BbVjoJ9LLcMB_qDhItErAJbSmumlYoNNyCz8kIaNUpx-GZL2mR-WzUdl_I6asVhX_jxB7K-51TKZOlKu7xmOl8-LQ/s1600/MAP.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;Keep this map in mind. Each check below answers exactly one question, and each ends with evidence on screen: a row from a diagnostic view, an operator in a plan, or a count of pages read. Nothing is fixed by guessing.&lt;/p&gt;

&lt;h2&gt;Step 0 — Build the slow scene&lt;/h2&gt;
&lt;p&gt;We need a table big enough to be slow and a query simple enough to reason about. Create the database and an &lt;code&gt;Orders&lt;/code&gt; table with 200,000 rows — orders from eight Canadian cities — with &lt;strong&gt;no index on the &lt;code&gt;City&lt;/code&gt; column&lt;/strong&gt;. That missing index is the planted fault; everything else about the setup is ordinary.&lt;/p&gt;
&lt;pre&gt;CREATE DATABASE DemoDb;
GO
USE DemoDb;
GO
CREATE TABLE dbo.Orders (
    OrderId     INT IDENTITY(1,1) CONSTRAINT PK_Orders PRIMARY KEY,
    CustomerId  INT           NOT NULL,
    City        NVARCHAR(40)  NOT NULL,
    OrderDate   DATE          NOT NULL,
    Total       DECIMAL(10,2) NOT NULL,
    Notes       NVARCHAR(300) NOT NULL
);
GO&lt;/pre&gt;
&lt;p&gt;Fill it with generated rows (the full setup script, including the row generator, is in the companion code at the end of this post). Toronto is deliberately rare — about one percent of the table — because a city report is exactly the kind of query a real application runs.&lt;/p&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNfWxHCcxh5lWA2vqg40Uu569Js20G42wbCkIeanbZyZ6mFlJUBI3HyI6MhT_px_Y_P3PReV3DBIDVLjTd0cXQHX0DVwtQw3XBDZPBD4HTuYC0BfMTtdslAiH7zmwi4m0KN_U2pLBrvwYdLQc5svZAqi5h6RI5mu01ZmxLuaD2yKessEyBlejVbf-96ms/s1600/C1.png&quot; style=&quot;display: block; padding: 1em 0; text-align: center; &quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNfWxHCcxh5lWA2vqg40Uu569Js20G42wbCkIeanbZyZ6mFlJUBI3HyI6MhT_px_Y_P3PReV3DBIDVLjTd0cXQHX0DVwtQw3XBDZPBD4HTuYC0BfMTtdslAiH7zmwi4m0KN_U2pLBrvwYdLQc5svZAqi5h6RI5mu01ZmxLuaD2yKessEyBlejVbf-96ms/s1600/C1.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;em&gt;The demo instance reporting its version, and the finished table: 200,000 orders, 2,061 of them in Toronto.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;Step 1 — Run the hero query and take its fingerprints&lt;/h2&gt;
&lt;p&gt;Our hero query is the city report: &lt;em&gt;show me the orders from Toronto.&lt;/em&gt; Before diagnosing anything, measure it. Two counters matter:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Elapsed time&lt;/strong&gt; — how long the query took, in milliseconds.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Logical reads&lt;/strong&gt; — how many 8 KB &lt;em&gt;pages&lt;/em&gt; of the table SQL Server touched while answering. Pages are the honest yardstick: they do not depend on how fast your disk happens to be.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;SET STATISTICS IO ON;
SET STATISTICS TIME ON;
SELECT OrderId, CustomerId, OrderDate, Total
FROM dbo.Orders
WHERE City = N&#39;Toronto&#39;;&lt;/pre&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj5BdU6cnjAIt67k3qch3wxXmQt09pArhwDRhUdu7hGQIpyfHL_2IPhObw-9d9Us84TLzcINmag-RSAIEs1O-LKuxDjf1gW43WGv1Ezdnii9_IOK2hgVW1ZSE1Kjv0Uxs0pySqyR7PDtbvPcWvoIovaRUgUsiVR5raRpVx7-cJnjf9Py1ABD996ignT-L4/s1600/C2.png&quot; style=&quot;display: block; padding: 1em 0; text-align: center; &quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj5BdU6cnjAIt67k3qch3wxXmQt09pArhwDRhUdu7hGQIpyfHL_2IPhObw-9d9Us84TLzcINmag-RSAIEs1O-LKuxDjf1gW43WGv1Ezdnii9_IOK2hgVW1ZSE1Kjv0Uxs0pySqyR7PDtbvPcWvoIovaRUgUsiVR5raRpVx7-cJnjf9Py1ABD996ignT-L4/s1600/C2.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;em&gt;First run: elapsed &lt;strong&gt;15 ms&lt;/strong&gt;, and &lt;strong&gt;12,388 logical reads&lt;/strong&gt; — the server touched 12,388 pages to fetch about 2,061 rows. That ratio is the smell. Now we find out why, one check at a time.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;Check 1 — Is anything blocking it?&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;The idea in plain words.&lt;/strong&gt; When a query &lt;em&gt;changes&lt;/em&gt; a row, SQL Server &lt;em&gt;locks&lt;/em&gt; that row until the change is finished — like one person editing a shared document while everyone else waits. A &lt;em&gt;transaction&lt;/em&gt; is a group of changes treated as one unit: either all of them happen, or none do. If a transaction is left open, its locks stay held, and every other query that needs those rows queues behind it. On a busy server this — not a missing index — is the most common reason a healthy query suddenly &quot;hangs&quot;.&lt;/p&gt;
&lt;p&gt;Prove it to yourself with two sessions. In session A, open a transaction, update one order, and deliberately leave the transaction open:&lt;/p&gt;
&lt;pre&gt;BEGIN TRANSACTION;
UPDATE dbo.Orders SET Total = Total + 1 WHERE OrderId = 42;
-- do NOT commit yet&lt;/pre&gt;
&lt;p&gt;In session B, try to update the same order. It will not error and it will not finish — it waits. Now ask the engine who is waiting on whom. &lt;code&gt;sys.dm_exec_requests&lt;/code&gt; is the live list of work running right now, and its &lt;code&gt;blocking_session_id&lt;/code&gt; column names the session doing the blocking:&lt;/p&gt;
&lt;pre&gt;SELECT session_id, blocking_session_id, wait_type, wait_time, status
FROM sys.dm_exec_requests
WHERE blocking_session_id &amp;lt;&amp;gt; 0;&lt;/pre&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhgaQ-lEiDSNwPfu0nM7H9VxebzOTbFj5ftDs09fveadPtdExEide3xCjnZjKjZOufx5JTjc6L0uTRR_pUdSvJ1QsJn_wyXMBoeB8c6m7eyCruVC_n2wiWQ1Y0lWS2SBzktghNda5EQlUn4LwHIDsIFE2nQHHklsvpW_NZ_uqy8DLIGofRVlLW80SNzDhg/s1600/C3.png&quot; style=&quot;display: block; padding: 1em 0; text-align: center; &quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhgaQ-lEiDSNwPfu0nM7H9VxebzOTbFj5ftDs09fveadPtdExEide3xCjnZjKjZOufx5JTjc6L0uTRR_pUdSvJ1QsJn_wyXMBoeB8c6m7eyCruVC_n2wiWQ1Y0lWS2SBzktghNda5EQlUn4LwHIDsIFE2nQHHklsvpW_NZ_uqy8DLIGofRVlLW80SNzDhg/s1600/C3.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;em&gt;Session A holding its transaction open; session B&#39;s update waiting behind it.&lt;/em&gt;&lt;/p&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvPgoKFrY80k1XPUDLhhhPVe6CDjeQ9WQcxxcOJ2RkqrAErHtU6HYaJa_ZbsVPXmkNRTH-NXzwK1fCvWl8gEIjLaXyirv5WouJBN6yX94J9jtdEO05rMkfj8HTnlvLZy6vcXWMbWDlr8IxPOGBQoW9-6r6pBewhjzgsY52KzK97TOGtukOH8ZxIVmzafk/s1600/C4.png&quot; style=&quot;display: block; padding: 1em 0; text-align: center; &quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvPgoKFrY80k1XPUDLhhhPVe6CDjeQ9WQcxxcOJ2RkqrAErHtU6HYaJa_ZbsVPXmkNRTH-NXzwK1fCvWl8gEIjLaXyirv5WouJBN6yX94J9jtdEO05rMkfj8HTnlvLZy6vcXWMbWDlr8IxPOGBQoW9-6r6pBewhjzgsY52KzK97TOGtukOH8ZxIVmzafk/s1600/C4.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;em&gt;The evidence: one row — session B, blocked by session A, waiting on a lock (an &lt;code&gt;LCK&lt;/code&gt; wait). Roll back session A&#39;s transaction and B completes instantly.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;In our hero query&#39;s case this check comes back clean — nobody is blocking it. Run this check first anyway, every time: &lt;strong&gt;no index in the world fixes a queue.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;Check 2 — What is the server waiting on?&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;The idea in plain words.&lt;/strong&gt; SQL Server keeps a diary of waiting. Every time work pauses — for the disk, for a lock, for the processor — it writes down &lt;em&gt;why&lt;/em&gt; and &lt;em&gt;for how long&lt;/em&gt;. That diary is the view &lt;code&gt;sys.dm_os_wait_stats&lt;/code&gt;, accumulated since the server started. Ask it for the largest waits, skipping the idle background waits every healthy server records (the query in the companion code carries the standard filter list):&lt;/p&gt;
&lt;pre&gt;SELECT TOP (8) wait_type, waiting_tasks_count, wait_time_ms
FROM sys.dm_os_wait_stats
WHERE wait_time_ms &amp;gt; 0
  /* ...plus the standard idle-wait filter, in the companion code... */
ORDER BY wait_time_ms DESC;&lt;/pre&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiO84mmvroGilvkaUUiXPyXL5G3PgG6mmJ89t3DXQRVv_PsRMIFX8wsQT3Y8TtmiZ3n_cg0A1__I4cXkfM_xEgKxSMeNBq2hdgqtvxQUkHlJ5dqa79ptlgec7xJjVoLJCJjiqYhzGP9A2QFLibCyFwddbPi-TJOmjB_unhwFAY3i6-M1vr-wzR8pwwmBGE/s1600/C5.png&quot; style=&quot;display: block; padding: 1em 0; text-align: center; &quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiO84mmvroGilvkaUUiXPyXL5G3PgG6mmJ89t3DXQRVv_PsRMIFX8wsQT3Y8TtmiZ3n_cg0A1__I4cXkfM_xEgKxSMeNBq2hdgqtvxQUkHlJ5dqa79ptlgec7xJjVoLJCJjiqYhzGP9A2QFLibCyFwddbPi-TJOmjB_unhwFAY3i6-M1vr-wzR8pwwmBGE/s1600/C5.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;em&gt;The top of our diary after the workload above: &lt;strong&gt;LCK_M_X — 36,044 ms of lock waiting&lt;/strong&gt; across 3 waits. Further down: PAGEIOLATCH_SH — 337 ms across 285 waits for fetching data pages from disk into memory.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Read the diary honestly. The loudest entry is &lt;strong&gt;lock waiting — the fingerprint of Check 1&lt;/strong&gt;: the blocking we staged on purpose is the biggest single use of waiting time on this young demo server (36 seconds of it, while the blocking transaction sat open). That is the diary doing its job: it confirms the symptom and timestamps it. The page-fetch wait, &lt;code&gt;PAGEIOLATCH_SH&lt;/code&gt;, is present but small — 337 ms — because this demo&#39;s disk is fast and the table fits in memory after the first read. On a busy production server with a cold cache, a full-table scan like our hero query&#39;s pushes exactly that wait to the top of the diary; here, the 12,388 logical reads from Step 1 are the harder evidence. Two cautions that make you better at reading any diary: it accumulates &lt;em&gt;since the server started&lt;/em&gt; (know what workload it covers — ours includes our own staged blocking), and waits tell you &lt;em&gt;where&lt;/em&gt; time went, not &lt;em&gt;why&lt;/em&gt;. The why is the next check.&lt;/p&gt;

&lt;h2&gt;Check 3 — What does the execution plan say?&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;The idea in plain words.&lt;/strong&gt; Before running a query, SQL Server writes an &lt;em&gt;execution plan&lt;/em&gt; — the route it will take through the data. The &lt;em&gt;actual&lt;/em&gt; execution plan shows the route it really took, operator by operator. Two operators decide today&#39;s story:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Scan&lt;/strong&gt; — read every row in the table, one by one. With 200,000 rows, that is the whole book, page by page.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Seek&lt;/strong&gt; — use an &lt;em&gt;index&lt;/em&gt; (a sorted lookup list, like the index at the back of a book) and jump straight to the matching rows.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Ask for the actual plan of the hero query (in SSMS this is the &lt;em&gt;Include Actual Execution Plan&lt;/em&gt; button, or Ctrl+M; in a terminal, &lt;code&gt;SET STATISTICS XML ON&lt;/code&gt; returns the same plan):&lt;/p&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgR1oFn2cG-cajHaiGRMq4EvY1Lh6emXbM6voYNL95L_No4jmPd48qRiTzKiI2de4ae74gVnG72-Enkv04wY14kZXQSH_C7j4oqnxjS8Bh-BIO7edGE_k_mOm4fdPskY8o8F5z7eki-hfdq9p1RqtOHxlDkbZj2pST2iLYpnYPLRFtD4JL2RUvIIhSfv_s/s1600/C6.png&quot; style=&quot;display: block; padding: 1em 0; text-align: center; &quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgR1oFn2cG-cajHaiGRMq4EvY1Lh6emXbM6voYNL95L_No4jmPd48qRiTzKiI2de4ae74gVnG72-Enkv04wY14kZXQSH_C7j4oqnxjS8Bh-BIO7edGE_k_mOm4fdPskY8o8F5z7eki-hfdq9p1RqtOHxlDkbZj2pST2iLYpnYPLRFtD4JL2RUvIIhSfv_s/s1600/C6.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;em&gt;The plan&#39;s heavy operator: &lt;strong&gt;Clustered Index Scan&lt;/strong&gt;. The server read the entire table because, with no index on &lt;code&gt;City&lt;/code&gt;, a scan was the only route it had.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;Check 4 — Which index is missing?&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;The idea in plain words.&lt;/strong&gt; Every time the planner wanted an index that did not exist, it wrote a note: the table, the columns it wanted, and how much it estimated the index would help. Those notes live in three views whose names start with &lt;code&gt;sys.dm_db_missing_index&lt;/code&gt;. Ask for the notes about our database:&lt;/p&gt;
&lt;pre&gt;SELECT migs.avg_total_user_cost, migs.avg_user_impact, migs.user_seeks,
       mid.statement, mid.equality_columns,
       mid.inequality_columns, mid.included_columns
FROM sys.dm_db_missing_index_group_stats AS migs
JOIN sys.dm_db_missing_index_groups  AS mig
  ON migs.group_handle = mig.index_group_handle
JOIN sys.dm_db_missing_index_details AS mid
  ON mig.index_handle  = mid.index_handle
WHERE mid.database_id = DB_ID(&#39;DemoDb&#39;)
ORDER BY migs.avg_user_impact DESC;&lt;/pre&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhN6bXr0UkzUoAEYRWExba8AlHZ_t2azuA_bCLoBNVp6tqZaJvwGOrRex464TwMCGdQhRHEESDKQPEGj8W9cZUFQVCHH2TNE3PnHtjq9n0ypXrvatTGIBW1LVFcC4qb4BMFN-TCLdnkCUFh3Iu0A3Nj9MsV6POuwr6TntpRCJztaFOy_3_bOpY9THqdoeU/s1600/C7.png&quot; style=&quot;display: block; padding: 1em 0; text-align: center; &quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhN6bXr0UkzUoAEYRWExba8AlHZ_t2azuA_bCLoBNVp6tqZaJvwGOrRex464TwMCGdQhRHEESDKQPEGj8W9cZUFQVCHH2TNE3PnHtjq9n0ypXrvatTGIBW1LVFcC4qb4BMFN-TCLdnkCUFh3Iu0A3Nj9MsV6POuwr6TntpRCJztaFOy_3_bOpY9THqdoeU/s1600/C7.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;em&gt;SQL Server&#39;s own note: for &lt;code&gt;dbo.Orders&lt;/code&gt;, an index whose key is &lt;code&gt;[City]&lt;/code&gt;, with included columns [CustomerId], [OrderDate], [Total] — estimated improvement &lt;strong&gt;98.19%&lt;/strong&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The honesty rule&lt;/strong&gt; (Microsoft&#39;s own documentation says it plainly): a missing-index suggestion is &lt;em&gt;a hint, not a prescription&lt;/em&gt;. The estimates are made when the query is planned, before it runs, and the suggestions are forgotten when the server restarts. So read the note critically. Ours passes the common-sense test — the suggested key is exactly our query&#39;s filter column — so we build it.&lt;/p&gt;

&lt;h2&gt;Step 5 — Create the index the server asked for&lt;/h2&gt;
&lt;pre&gt;CREATE NONCLUSTERED INDEX IX_Orders_City
ON dbo.Orders (City)
INCLUDE (CustomerId, OrderDate, Total);&lt;/pre&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi2F5A1PfmVDzlN7Avn9dWjjKMsbM0yTshHKSqEI1dW9qViHBLjowATatWX9R7NhMkFcLlC3QYlsIBxHCUUNxeadaQYLr9sDx7o61E9ZCiXsP5yxmxZkayEAEtmpxxEDCBZgzpcqkjxXAMsJyNdcHISlnGpL5uj2jBT0LIlwDcU37LaFrCzpDhggdLPdGc/s1600/C8.png&quot; style=&quot;display: block; padding: 1em 0; text-align: center; &quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi2F5A1PfmVDzlN7Avn9dWjjKMsbM0yTshHKSqEI1dW9qViHBLjowATatWX9R7NhMkFcLlC3QYlsIBxHCUUNxeadaQYLr9sDx7o61E9ZCiXsP5yxmxZkayEAEtmpxxEDCBZgzpcqkjxXAMsJyNdcHISlnGpL5uj2jBT0LIlwDcU37LaFrCzpDhggdLPdGc/s1600/C8.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;em&gt;The index builds in 524 ms. One line of SQL — chosen from evidence, not from a guess.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;Step 6 — Run the same query again (the proof)&lt;/h2&gt;
&lt;pre&gt;SET STATISTICS IO ON;
SET STATISTICS TIME ON;
SELECT OrderId, CustomerId, OrderDate, Total
FROM dbo.Orders
WHERE City = N&#39;Toronto&#39;;&lt;/pre&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiS15UM2EmaSH-tdb_WXFZq9TNT5YF436Bi-7sc25ep19dFY1kXPBCiWxJs6S4Dq4aeXWP-hjAAQVxrs8UC99I2XOwCvcYLAGaw_TT8cCgC5gFCA7yV-dQNDx1RXcs6YtQe-dNSa2Ua-AOxLeB1HVaFTs8pRJ57VG9fHZVL0JMnXMgvNeDbpF37_YrQ2Vc/s1600/C9.png&quot; style=&quot;display: block; padding: 1em 0; text-align: center; &quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiS15UM2EmaSH-tdb_WXFZq9TNT5YF436Bi-7sc25ep19dFY1kXPBCiWxJs6S4Dq4aeXWP-hjAAQVxrs8UC99I2XOwCvcYLAGaw_TT8cCgC5gFCA7yV-dQNDx1RXcs6YtQe-dNSa2Ua-AOxLeB1HVaFTs8pRJ57VG9fHZVL0JMnXMgvNeDbpF37_YrQ2Vc/s1600/C9.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhPAF3RgC2MifsXaxgPXYzIKgUIbW-BWyXxVbXb3DJOi73eZxijZ4Ct1XupbeJpLt7IFuE-ls2p1-5_ChnMJq2kFPOaiZz6m9wWlQ5OET5xVZAx4Irvohl_828Oidvg_mN67pIltaoili_ZjLZesr5RJ_I-ufT4NENhogovMiIk_OQIB_hFMa04mHDZUg0/s1600/C10.png&quot; style=&quot;display: block; padding: 1em 0; text-align: center; &quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhPAF3RgC2MifsXaxgPXYzIKgUIbW-BWyXxVbXb3DJOi73eZxijZ4Ct1XupbeJpLt7IFuE-ls2p1-5_ChnMJq2kFPOaiZz6m9wWlQ5OET5xVZAx4Irvohl_828Oidvg_mN67pIltaoili_ZjLZesr5RJ_I-ufT4NENhogovMiIk_OQIB_hFMa04mHDZUg0/s1600/C10.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;em&gt;After: the plan&#39;s heavy operator is now &lt;strong&gt;Index Seek (on the new IX_Orders_City index)&lt;/strong&gt; — the server opens the book at the right page.&lt;/em&gt;&lt;/p&gt;
&lt;table&gt;
&lt;tr&gt;&lt;th&gt;Measure&lt;/th&gt;&lt;th&gt;Before&lt;/th&gt;&lt;th&gt;After&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Elapsed time&lt;/td&gt;&lt;td&gt;15 ms&lt;/td&gt;&lt;td&gt;1 ms&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Logical reads (pages touched)&lt;/td&gt;&lt;td&gt;12,388&lt;/td&gt;&lt;td&gt;15&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Plan&#39;s main operator&lt;/td&gt;&lt;td&gt;Clustered Index Scan&lt;/td&gt;&lt;td&gt;Index Seek (on the new IX_Orders_City index)&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;Same data. Same query, word for word. The only change is the index — added because four checks, in order, pointed at it.&lt;/p&gt;

&lt;h2&gt;Doing this in SSMS&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Plans:&lt;/strong&gt; press &lt;strong&gt;Ctrl+M&lt;/strong&gt; (Include Actual Execution Plan), run the query, and read the Execution Plan tab — the operators you met above appear by name. A plan can also carry a green &quot;Missing Index&quot; suggestion line; right-clicking it offers the same CREATE INDEX the DMVs described.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Blocking:&lt;/strong&gt; Activity Monitor (right-click the server → Activity Monitor) shows blocked sessions in its Processes grid; the DMV query in Check 1 is the same information, scriptable.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reads:&lt;/strong&gt; &lt;code&gt;SET STATISTICS IO ON&lt;/code&gt; prints its lines on the Messages tab, exactly as captured here.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Common mistakes&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Adding indexes by vibes.&lt;/strong&gt; Every index speeds some reads and slows every write to the table. Evidence first (Checks 1–4), then one index at a time, then re-measure.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Creating every suggested index.&lt;/strong&gt; Suggestions overlap and pile up; Microsoft calls them hints for a reason. Consolidate, and check what your queries actually filter on.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Blaming the index for a queue.&lt;/strong&gt; If Check 1 shows a blocker, the fix is the blocking transaction — not an index.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tuning from a cold diary.&lt;/strong&gt; Wait stats and missing-index notes reset when the server restarts. Diagnose on a server that has been running the real workload.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;The method, on one card&lt;/h2&gt;
&lt;p&gt;Blocked? Ask &lt;code&gt;sys.dm_exec_requests&lt;/code&gt;. Waiting? Ask &lt;code&gt;sys.dm_os_wait_stats&lt;/code&gt;. Why? Read the actual execution plan. Missing? Ask the &lt;code&gt;sys.dm_db_missing_index&lt;/code&gt; views. Then fix &lt;em&gt;one&lt;/em&gt; thing and measure again. Four questions, in order, evidence at every step.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Your exercise:&lt;/strong&gt; on a test database, run a query you believe is slow. Take its fingerprints (Step 1), run the four checks, write down what each one says &lt;em&gt;before&lt;/em&gt; you change anything — then make the one fix the evidence supports and compare the numbers.&lt;/p&gt;

&lt;h2&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Microsoft Learn — &lt;em&gt;sys.dm_exec_requests (Transact-SQL)&lt;/em&gt;: the &lt;code&gt;blocking_session_id&lt;/code&gt; column and the blocked-requests example.&lt;/li&gt;
&lt;li&gt;Microsoft Learn — &lt;em&gt;sys.dm_os_wait_stats (Transact-SQL)&lt;/em&gt;: &quot;You can use this aggregated view to diagnose performance issues with SQL Server&quot;; lock waits indicate contention, page IO latch waits indicate IO response.&lt;/li&gt;
&lt;li&gt;Microsoft Learn — &lt;em&gt;sys.dm_db_missing_index_details&lt;/em&gt; and &lt;em&gt;Tune nonclustered indexes with missing index suggestions&lt;/em&gt;: column roles, equality-before-inequality key order, and &quot;suggestions aren&#39;t prescriptions&quot;.&lt;/li&gt;
&lt;li&gt;All timings, read counts and plan operators in this post were measured on the demo instance described in Step 0 while writing it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Get the code&lt;/h2&gt;
&lt;p&gt;The complete setup, diagnosis and fix scripts for this post live in the companion repository, in &lt;a href=&quot;https://github.com/sajjad037/visual-studio-learn-code/tree/main/topic-12-sql-server-slow&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;topic-12-sql-server-slow folder&lt;/a&gt;, so you can rebuild the slow scene and run every check yourself.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Disclosure: Visual Studio Learn is reader-supported. Some links may be affiliate links, which means the channel may earn a small commission at no extra cost to you. Only tools actually used in the tutorials are ever linked, and affiliate relationships never change a verdict.&lt;/em&gt;&lt;/p&gt;
&lt;div style=&quot;text-align:center;border:1px solid #e3e6ea;border-radius:8px;padding:18px 14px;margin:28px 0;background:#fbfcfd;&quot;&gt;&lt;h3 style=&quot;margin:0 0 6px;&quot;&gt;Enjoying these tutorials?&lt;/h3&gt;&lt;p style=&quot;margin:0 0 6px;&quot;&gt;Get new Visual Studio Learn posts by email — free, no spam:&lt;/p&gt;&lt;style&gt; .followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview { display: flex !important; flex-direction: column !important; justify-content: center !important; margin-top: 30px !important; padding: clamp(17px, 5%, 40px) clamp(17px, 7%, 50px) !important; max-width: none !important; border-radius: 6px !important; box-shadow: 0 5px 25px rgba(34, 60, 47, 0.25) !important; } .followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview, .followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview *{ box-sizing: border-box !important; } .followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-heading { width: 100% !important; } .followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-heading h5{ margin-top: 0 !important; margin-bottom: 0 !important; } .followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field { margin-top: 20px !important; width: 100% !important; } .followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input { width: 100% !important; height: 40px !important; border-radius: 6px !important; border: 2px solid #e9e8e8 !important; background-color: #fff; outline: none !important; } .followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input { color: #000000 !important; font-family: &quot;Montserrat&quot; !important; font-size: 14px; font-weight: 400; line-height: 20px; text-align: center; } .followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input::placeholder { color: #000000 !important; opacity: 1 !important; } .followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input:-ms-input-placeholder { color: #000000 !important; } .followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input::-ms-input-placeholder { color: #000000 !important; } .followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button { margin-top: 10px !important; width: 100% !important; } .followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button button { width: 100% !important; height: 40px !important; border: 0 !important; border-radius: 6px !important; line-height: 0px !important; } .followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button button:hover { cursor: pointer !important; } .followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .powered-by-line { color: #231f20 !important; font-family: &quot;Montserrat&quot; !important; font-size: 13px !important; font-weight: 400 !important; line-height: 25px !important; text-align: center !important; text-decoration: none !important; display: flex !important; width: 100% !important; justify-content: center !important; align-items: center !important; margin-top: 10px !important; } .followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .powered-by-line img { margin-left: 10px !important; height: 1.13em !important; max-height: 1.13em !important; } &lt;/style&gt;&lt;div class=&quot;followit--follow-form-container&quot; data-v-f93f6730=&quot;&quot; attr-a attr-b attr-c attr-d attr-e attr-f&gt;&lt;form data-v-f93f6730=&quot;&quot; action=&quot;https://api.follow.it/subscription-form/SmRpWjEzV1ovU0FOK2R2Y0oxajFBUmtIdW44K1ZiUWZRUm1FZVdpVlRKUHFLSFBKWjE4M1hTSk5yRU5oNmg0RU5zelhuTllNTGU1UmwzRkN0NzZWbnd2YkV5VWFwYnptWFdtekI3eXdwMmhtamtUNit3dlNSblVmRjJxd1JGbjZ8Q05yQWR2eitvYW5HRmNMMzJkbStpM09rNmxXZ0ZjWWhyOERya2VvVXRzZz0=/8&quot; method=&quot;post&quot;&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;form-preview&quot; style=&quot;background-color: rgb(255, 255, 255); border-style: solid; border-width: 1px; border-color: rgb(204, 204, 204); position: relative;&quot;&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;preview-heading&quot;&gt;&lt;h5 data-v-f93f6730=&quot;&quot; style=&quot;font-family: Arial; font-weight: bold; color: rgb(0, 0, 0); font-size: 16px; text-align: center; text-transform: none !important;&quot;&gt;Get new posts by email:&lt;/h5&gt;&lt;/div&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;preview-input-field&quot;&gt;&lt;input data-v-f93f6730=&quot;&quot; type=&quot;email&quot; name=&quot;email&quot; required=&quot;&quot; placeholder=&quot;Enter your email&quot; spellcheck=&quot;false&quot; style=&quot;font-family: Arial; font-weight: normal; color: rgb(0, 0, 0); font-size: 14px; text-align: center; background-color: rgb(255, 255, 255); text-transform: none !important;&quot; /&gt;&lt;/div&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;preview-submit-button&quot;&gt;&lt;button data-v-f93f6730=&quot;&quot; type=&quot;submit&quot; style=&quot;font-family: Arial; font-weight: bold; color: rgb(255, 255, 255); font-size: 16px; text-align: center; background-color: rgb(0, 0, 0); text-transform: none !important;&quot;&gt;Subscribe&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/form&gt;&lt;a href=&quot;https://follow.it&quot; class=&quot;powered-by-line&quot;&gt;Powered by &lt;img src=&quot;https://follow.it/images/colored-logo.svg&quot; alt=&quot;follow.it&quot; height=&quot;17px&quot;/&gt;&lt;/a&gt;&lt;/div&gt;&lt;p style=&quot;margin:12px 0 0;font-size:14px;&quot;&gt;Follow along: &lt;a href=&quot;https://www.youtube.com/@VisualStudioLearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/youtube/FF0000&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;YouTube&quot; /&gt; YouTube&lt;/a&gt; · &lt;a href=&quot;https://www.facebook.com/VisualStudio.Learn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/facebook/1877F2&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;Facebook&quot; /&gt; Facebook&lt;/a&gt; · &lt;a href=&quot;https://www.instagram.com/visualstudiolearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/instagram/E4405F&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;Instagram&quot; /&gt; Instagram&lt;/a&gt; · &lt;a href=&quot;https://www.tiktok.com/@visualstudiolearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/tiktok/000000&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;TikTok&quot; /&gt; TikTok&lt;/a&gt; · &lt;a href=&quot;https://www.visualstudiolearn.com/feeds/posts/default?alt=rss&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/rss/FFA500&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;RSS&quot; /&gt; RSS&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://www.visualstudiolearn.com/feeds/628846896233274021/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.visualstudiolearn.com/2026/11/why-is-my-sql-server-slow-4-step.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/628846896233274021'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/628846896233274021'/><link rel='alternate' type='text/html' href='http://www.visualstudiolearn.com/2026/11/why-is-my-sql-server-slow-4-step.html' title='Why Is My SQL Server Slow? A 4-Step Diagnosis with Blocking, Wait Stats, Execution Plans and Missing Indexes (2026)'/><author><name>The Loonie Lesson</name><uri>http://www.blogger.com/profile/02243798795439866634</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRY4V9cFLhqMxHWBOWCGBiGUqjan14Vda_dy5rz-C41kApceM-u6jAxS2_6AIRZtGkQeyiQolq1gCeSBhpo573lYq37MLmMGgSKDQLasowic65VoitivYCzqrmUDUqxJuqCTyzah8JuALYtW0Iz7rXIvIcj-ovhVpEuEhQJ6yG1FUQ-A/s1600/loonie-lesson-logo-profile.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://img.youtube.com/vi/jSroBM7mijs/default.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8861905861585113921.post-3265395906264283738</id><published>2026-11-11T21:03:11.000-05:00</published><updated>2026-11-22T02:50:57.605-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="AI Agents"/><category scheme="http://www.blogger.com/atom/ns#" term="Gemini"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><category scheme="http://www.blogger.com/atom/ns#" term="Tutorial"/><title type='text'>Build Your Own Coding Agent in Python (It Fixes Real Code — Behind an Approval Gate)</title><content type='html'>&lt;p&gt;Build your own coding agent — in one Python file, with three tools and one rule that makes it safe to use: &lt;strong&gt;it cannot change a single line of code until a human approves the change&lt;/strong&gt;. In this tutorial we build the agent on the Gemini API&#39;s function-calling loop, point it at a real project with a real failing test, and watch it find the bug, propose the fix, stop at the approval gate, and — only after a yes — apply it and prove the suite green. Then it prints its own bill. Every step has its own real screenshot, taken from the run you are reading about.&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;&lt;strong&gt;Versions used in this tutorial&lt;/strong&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Python &lt;strong&gt;3.12&lt;/strong&gt;&lt;/li&gt;&lt;li&gt;&lt;strong&gt;google-genai 3.0.0&lt;/strong&gt; (the Gemini SDK, pinned) and &lt;strong&gt;pytest 9.2.0&lt;/strong&gt; (pinned)&lt;/li&gt;&lt;li&gt;Model: &lt;strong&gt;gemini-3.8-flash&lt;/strong&gt; — rate card verified on Google&#39;s official pricing page on 2026-11-07: $0.75 per 1M input tokens, $3.75 per 1M output tokens (Standard tier, in effect through Dec 31, 2026); a free tier exists&lt;/li&gt;&lt;li&gt;Companion code: &lt;a href=&quot;https://github.com/sajjad037/visual-studio-learn-code/tree/main/topic-09-coding-agent&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;topic-09-coding-agent&lt;/a&gt; in the Visual Studio Learn code repo&lt;/li&gt;&lt;/ul&gt;&lt;/blockquote&gt;

&lt;h2&gt;Step 0 — Setup from zero&lt;/h2&gt;

&lt;p&gt;Nothing here assumes an existing project. You need Python 3.12, an isolated environment, the two pinned packages, and a free API key.&lt;/p&gt;

&lt;ol&gt;&lt;li&gt;Create a &lt;strong&gt;venv&lt;/strong&gt; — a virtual environment, an isolated folder holding this project&#39;s packages so they never collide with the rest of your machine — and activate it:&lt;pre&gt;&lt;code&gt;python3 -m venv .venv
source .venv/bin/activate   # on Windows: .venv\Scripts\activate&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;Install the pinned packages (the pins matter: this tutorial&#39;s code was verified against exactly these versions):&lt;pre&gt;&lt;code&gt;pip install google-genai==3.0.0 pytest==9.2.0&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;&lt;li&gt;In &lt;a href=&quot;https://aistudio.google.com&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Google AI Studio&lt;/a&gt;, create a free API key, and put it in an &lt;strong&gt;environment variable&lt;/strong&gt; — a setting your terminal session carries — named &lt;code&gt;GEMINI_API_KEY&lt;/code&gt;:&lt;pre&gt;&lt;code&gt;export GEMINI_API_KEY=your-key-here&lt;/code&gt;&lt;/pre&gt;The key goes in the environment, &lt;strong&gt;never in the code&lt;/strong&gt;. Code gets committed, shared and screenshotted; keys must not travel with it.&lt;/li&gt;&lt;/ol&gt;

&lt;p&gt;One warning about free keys, from Google&#39;s own pricing page: on the &lt;strong&gt;free tier, content you send may be used to improve Google&#39;s products&lt;/strong&gt;. Learn on demo code, like this tutorial does — not on anything private.&lt;/p&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjBXCKbUqfOOcE_i5Qnz9S6OY4YE9wHv6f6t2RHjYkSx_ZlrSXGMWpoptO0K9y9wd9H9X6yMWC9Xssz_-c7I78ZXA_6jw8Db-xMtvGoDbJ58noUBm38Xya1z_-4RlttD8sGS1442duJ3xiQUPRzZpkcHOfnBCAnauDyCTIduNF_bIh0ACcU5gcjpZ4y8N0/s1600/s0-setup.png&quot; alt=&quot;Terminal showing Python 3.12, the active venv, and the pinned google-genai and pytest installs completing&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Step 0: Python 3.12, the venv active, and the two pinned installs in place. The key is set in the environment — its value never appears on screen or in code.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;p style=&quot;text-align:center;&quot;&gt;&lt;iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/duquW7F_Ibc&quot; title=&quot;YouTube video player&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen&gt;&lt;/iframe&gt;&lt;/p&gt;


&lt;h2&gt;Step 1 — The patient: the buggy cart and its pytest suite&lt;/h2&gt;

&lt;p&gt;The demo project (the &lt;code&gt;demo/&lt;/code&gt; folder in the companion code) is a tiny shopping cart. One bug lives in it on purpose, in &lt;code&gt;Cart.add()&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def add(self, name, unit_price_cents, quantity=1):
    # BUG (planted): this replaces the quantity instead of adding to it.
    self._items[name] = [unit_price_cents, quantity]&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Add the same item twice and the quantity is &lt;em&gt;replaced&lt;/em&gt;, not accumulated. Around the cart sits a suite of four tests. Run it before the agent ever sees the project:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cd demo
python -m pytest -q
# 1 failed, 3 passed&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The failing test — &lt;code&gt;test_add_same_item_twice_accumulates&lt;/code&gt; — adds two notebooks, then three more, and expects five; the cart says three. The other three tests — a new item, a mixed-cart total, a removal — all pass, so the bug is narrow: one method, one wrong idea about what &quot;add&quot; means. &lt;strong&gt;1 failed, 3 passed&lt;/strong&gt; is the contract for everything below: the agent&#39;s job is to turn that line into &lt;strong&gt;4 passed&lt;/strong&gt; without touching a test.&lt;/p&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyo0LflXeY12fWqhGayJqcwVsiyrRqUFdV6Zm1DCJO_kFFsHDKjUTh3-9G0I9bkGLyYzrHpNWL1WAfrTZ_xA7E3J3D7dhyphenhyphenicL0CCXs1GvpLPnI9LKJCk7ro8hWRAuwEjZ7J7CHgQ_qYhFwLJO2TbE4Ic8EkWxLcV_xVhyphenhyphenhcDnJGwFXrUqxgfqwhCOyL-k/s1600/s1-prerun.png&quot; alt=&quot;Terminal running pytest on the untouched demo project: 1 failed, 3 passed, with the quantity test named as the failure&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Step 1, before: the untouched suite — exactly 1 failed, 3 passed. This is the result the agent has to change.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhbG5yJbn3MjtglcYRe9VX_GyIQB1wkfzOpgJM3US7xWn-271vqcW3GOANud4EfqMZnNJ5XApJPaIispjE4cuqEmudBPQVoC9El6o53GKA_ZzBJJUrLHvJwgiOGwc8Vb4PWJ2KKX6AlJRUSU19FalS7vPQ3UDKnPwvmOwG934bBcs-7yPLvEDWP-e8G5-w/s1600/s2-patient.png&quot; alt=&quot;Editor showing cart.py with the planted add() bug and the failing assertion in test_cart.py&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;The patient: &lt;code&gt;cart.py&lt;/code&gt;&#39;s &lt;code&gt;add()&lt;/code&gt; replaces the quantity; the test beside it expects accumulation — 2 + 3 = 5.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;h2&gt;Step 2 — The agent skeleton: client, model, task&lt;/h2&gt;

&lt;p&gt;The whole agent is one file, &lt;code&gt;agent.py&lt;/code&gt;. Its head is the entire design in miniature:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;import os
import subprocess
import sys
from pathlib import Path

from google import genai
from google.genai import types&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Every import the file needs sits at the top — &lt;code&gt;os&lt;/code&gt; for the key check, &lt;code&gt;subprocess&lt;/code&gt; and &lt;code&gt;sys&lt;/code&gt; for the test tool, &lt;code&gt;Path&lt;/code&gt; for the confinement check, and the SDK itself. Then the constants:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;PROJECT_DIR = Path(__file__).parent / &quot;demo&quot;
MODEL = &quot;gemini-3.8-flash&quot;
MAX_STEPS = 12

TASK = (
    &quot;Run the test suite in this project. If a test fails, read the test and &quot;
    &quot;the code it tests, find the bug, and fix the source file so the whole &quot;
    &quot;suite passes. Do not edit the tests. When every test passes, stop and &quot;
    &quot;summarise what the bug was and what you changed.&quot;
)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Three ideas to notice. The &lt;strong&gt;model&lt;/strong&gt; is &lt;code&gt;gemini-3.8-flash&lt;/code&gt;, the current Flash model — fast, inexpensive, and it supports &lt;strong&gt;function calling&lt;/strong&gt;: the mechanism this whole tutorial is built on. The model never runs code itself; it asks our program to run named functions — &lt;em&gt;tools&lt;/em&gt; — and reads their results. The &lt;strong&gt;task&lt;/strong&gt; is plain English with two hard edges: fix the source, &lt;em&gt;never the tests&lt;/em&gt;. And &lt;code&gt;MAX_STEPS&lt;/code&gt; is a hard cap of twelve model turns, so a confused model costs twelve turns — never an infinite loop.&lt;/p&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjojFHWlHrrUz4HCYw8FVWv88p_yuxJrnH6p_4xfccNuoRF9pt1LD5DozCsq0fvHSFS26Ar8UQUulVCfKCO_pLrDSdO-sPRMqR_irG49mOBxhqqGZ1wbjjxDrnaP4B-1emtad_zunOqaYkACBVTM3D4kA7skMdSfcA6cHgV9II6uq0hXZWQXWATlHQBT3w/s1600/s3-skeleton.png&quot; alt=&quot;Editor showing the head of agent.py: the client, the gemini-3.8-flash model constant, the step cap, and the plain-English task&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Step 2: the skeleton — client, model, step cap, and the task written as instructions a careful junior would understand.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;h2&gt;Step 3 — Tool 1: &lt;code&gt;read_file&lt;/code&gt;, confined&lt;/h2&gt;

&lt;p&gt;An agent is only its tools, and ours has exactly three. The first reads a file and hands its text to the model:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def confine(path_text):
    &quot;&quot;&quot;Resolve a model-supplied path and refuse anything outside the project.&quot;&quot;&quot;
    path = (PROJECT_DIR / path_text).resolve()
    if not path.is_relative_to(PROJECT_DIR.resolve()):
        return None
    return path

def tool_read_file(path):
    target = confine(path)
    if target is None or not target.is_file():
        return {&quot;error&quot;: f&quot;cannot read {path}&quot;}
    return {&quot;content&quot;: target.read_text()[:8000]}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The guard is the lesson: every path the model supplies is resolved and checked, and anything landing outside the demo project folder is refused. The model can &lt;em&gt;ask&lt;/em&gt; for anything; the code decides what it may touch. (We tested the escape: asking for a file two folders up — outside the project — comes back as an error, not a file.)&lt;/p&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjq4AC_XE_enChgW0OLJOjRYeliKlCzuAKU_jcxikxi9EXQ1r3U37Z-tQT2GW31c4-KjlNuncE7SKvmdRJQb_g7wy5WNDp44EyIaK5t8aXaPrj0_g_uts9zZTqlQ1nfIm5E-mv2MjgGBrxunO8S4Yl599rK8WgXJ2IeFzVTTpDZ3sY53gN_LbsqHyD9E94/s1600/s4-tool1.png&quot; alt=&quot;Editor showing the confine() path check and the read_file tool in agent.py&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Step 3: &lt;code&gt;confine()&lt;/code&gt; — every model-supplied path is resolved and refused if it escapes the project folder.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;h2&gt;Step 4 — Tool 2: &lt;code&gt;run_tests&lt;/code&gt;&lt;/h2&gt;

&lt;pre&gt;&lt;code&gt;def tool_run_tests():
    proc = subprocess.run(
        [sys.executable, &quot;-m&quot;, &quot;pytest&quot;, &quot;-q&quot;],
        cwd=PROJECT_DIR, capture_output=True, text=True, timeout=120,
    )
    return {&quot;exit_code&quot;: proc.returncode, &quot;output&quot;: (proc.stdout + proc.stderr)[-3000:]}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The second tool runs the suite with a fixed command — the model cannot alter it — inside the project folder, and returns the tail of the output, where pytest puts its summary. Tools 1 and 2 are read-only: watching and reading. Safe. The third tool is neither.&lt;/p&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgZfGCAZGg1EFZSYbWOsa_zr2tclsgHdEKYVlLUb79BiXkOq7NEezEPZwp5fFjejyP3Wyzu-_bU6ohZsdExzH6PhRRUYaMELzCyKgY1sUQk657Fnn8kBqs9wxR07TC1_EUF9q0X7oS3sUKazz9g9FGJZzH3aNuaTxOIbjDYioMQPiHAe-btwnPFU0iqLW0/s1600/s5-tool2.png&quot; alt=&quot;Editor showing the run_tests tool: a fixed pytest subprocess call returning the exit code and the output tail&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Step 4: &lt;code&gt;run_tests&lt;/code&gt; — a fixed command the model can invoke but never modify.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;h2&gt;Step 5 — Tool 3: &lt;code&gt;write_file&lt;/code&gt; and the approval gate&lt;/h2&gt;

&lt;p&gt;This is the dangerous tool, so it is built differently. &lt;strong&gt;The model never writes. It proposes.&lt;/strong&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def tool_write_file(path, content):
    &quot;&quot;&quot;THE GATE: the model only ever PROPOSES. A human decides.&quot;&quot;&quot;
    target = confine(path)
    if target is None:
        return {&quot;error&quot;: f&quot;refused: {path} is outside the project folder&quot;}
    print(f&quot;\n--- The agent wants to write {path} ---&quot;)
    print(content)
    answer = input(&quot;Apply this change? [y/N] &quot;).strip().lower()
    if answer != &quot;y&quot;:
        print(&quot;--- Denied by the human. The file was NOT changed. ---&quot;)
        return {&quot;result&quot;: &quot;DENIED by the human approver. Do not retry this write; &quot;
                          &quot;explain in your summary what you wanted to change.&quot;}
    target.write_text(content)
    print(&quot;--- Approved and written. ---&quot;)
    return {&quot;result&quot;: f&quot;{path} written&quot;}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The full proposed content is printed in the terminal, and the program stops at a yes/no prompt. Answer anything but &lt;code&gt;y&lt;/code&gt; and the file stays byte-for-byte untouched — and the model is told, in plain words, that a human refused and it must not retry. Only a yes writes. Every other tool in this agent can run all day; this one needs you.&lt;/p&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhgLkOg8hNTHqaE82Y1UzD2Z1smsw4p_6htoovo3a1NCOfR9F5KajGc6FMpeLy96nro1YcnNS_7mIN7QBEkf1C8JSY8DSMpWxtdf-_WPzab_PFGHtyudL5R08orf5DHni7dZookJA6PdiPPt7_b56krxVbsa63flE-eMYsnKYlv85eJVwpA3u-eb4bBSno/s1600/s6-tool3-gate-code.png&quot; alt=&quot;Editor showing the write_file tool: the proposal printout and the human yes-no approval prompt&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Step 5, in code: the model proposes; the &lt;code&gt;input()&lt;/code&gt; line is the gate — nothing writes before it returns &quot;y&quot;.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCl39VVZSP5nt9X4j1dDvBo-jdh4egEfTIfr7fPhe6hkWTe7q8UWq40hYsb6ptYI3o7wWqiWGheSxU9dR7-HUk8uLM-_rvIJkhgrkGLkWrc_0ZHc-H2s1_MO3AERuXExxu-ojF_Z6KoOG-kTM04_EGB6wbrZ4tfmQC7_o4IByEWwk-tN9xNX2qLVFsBQ4/s1600/s7-gate-live.png&quot; alt=&quot;Terminal at the real approval-gate moment of the run: the proposed file printed, the Apply this change prompt, and the approval being given&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Step 5, live: the gate fires during the real run. The proposed file is on screen; the file on disk has not changed yet.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;h2&gt;Step 6 — The loop: manual function calling with the Gemini API&lt;/h2&gt;

&lt;p&gt;Around the tools sits the loop. Each tool is declared to the model as a &lt;code&gt;FunctionDeclaration&lt;/code&gt; — a name, a plain-English description, and a schema of its arguments — and that declaration is all the model ever sees of the code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;DECLARATIONS = [
    types.FunctionDeclaration(
        name=&quot;read_file&quot;, description=&quot;Read a file inside the project folder.&quot;,
        parameters=types.Schema(type=&quot;OBJECT&quot;, properties={
            &quot;path&quot;: types.Schema(type=&quot;STRING&quot;, description=&quot;File name, e.g. cart.py&quot;)},
            required=[&quot;path&quot;])),
    # … run_tests and write_file are declared the same way …
]&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;One small table ties the names to the functions — the loop looks each call up by name:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;TOOLS = {&quot;read_file&quot;: tool_read_file, &quot;run_tests&quot;: tool_run_tests,
         &quot;write_file&quot;: tool_write_file}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And here is the loop itself — the whole of &lt;code&gt;main()&lt;/code&gt;, condensed only in its token-counting lines. Everything a working agent needs is on this screen:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def main():
    if not os.environ.get(&quot;GEMINI_API_KEY&quot;):
        sys.exit(&quot;Set GEMINI_API_KEY first (free key from Google AI Studio).&quot;)
    client = genai.Client()
    config = types.GenerateContentConfig(
        tools=[types.Tool(function_declarations=DECLARATIONS)])
    contents = [types.Content(role=&quot;user&quot;, parts=[types.Part(text=TASK)])]

    for step in range(1, MAX_STEPS + 1):
        response = client.models.generate_content(
            model=MODEL, contents=contents, config=config)
        # … count response.usage_metadata tokens here (Step 8) …
        model_content = response.candidates[0].content
        contents.append(model_content)

        calls = [p.function_call for p in model_content.parts if p.function_call]
        if not calls:  # the model answered in words — the task is done
            print(&quot;\nAGENT:&quot;, response.text)
            break
        replies = []
        for call in calls:
            print(f&quot;[step {step}] tool: {call.name} {dict(call.args or {})}&quot;)
            result = TOOLS[call.name](**dict(call.args or {}))
            replies.append(types.Part.from_function_response(
                name=call.name, response=result))
        contents.append(types.Content(role=&quot;user&quot;, parts=replies))
    else:
        print(f&quot;\nStopped: step cap ({MAX_STEPS}) reached.&quot;)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Read it as a conversation with a bouncer. The task and the declarations go in; the model&#39;s answer comes back as either words (job done — break) or function calls. Each call is dispatched through the &lt;code&gt;TOOLS&lt;/code&gt; dictionary to the real Python function — &lt;code&gt;write_file&lt;/code&gt; included, gate and all — and every result goes back to the model in a &lt;code&gt;Content&lt;/code&gt; with role &lt;code&gt;&quot;user&quot;&lt;/code&gt; — the manual-loop pattern this API accepts. (Worth knowing: some write-ups send function results with a &lt;code&gt;&quot;tool&quot;&lt;/code&gt; role; on the live run for this post that came straight back as a 400 error, and &lt;code&gt;&quot;user&quot;&lt;/code&gt; is what worked. The code above is the code that ran.) The &lt;code&gt;for…else&lt;/code&gt; is the step cap doing its job: exhaust twelve steps without a words-only answer and the loop stops itself. This manual pattern is deliberate: automatic function calling hides exactly the moving parts a builder needs to see — and the SDK&#39;s own documentation is changing how automatic calling is invoked in the next major version, while the manual loop this tutorial teaches stays as documented.&lt;/p&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_z0REtvjk8dOUduL085ql_09o62oH6xNN4DZf7sGFvW0Zfah_EPE-834Ht5SjnXM6LqifhyffZwwBDCGWMp4pQBoC9Nol2kZLmoHpl6PFojKUboohW8UgSvp8InpOTKAirbv9Ce0auvH7eK0hO6EJbBMVFTR5vUOU27wSQVaLsAQlHcIsk1why_R_I9w/s1600/s8-loop.png&quot; alt=&quot;Editor showing the main() loop in agent.py: the generate_content call, the function-call dispatch, and the step cap&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Step 6: the loop — send, dispatch, return the result, repeat — capped at twelve steps.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;h2&gt;Step 7 — The run&lt;/h2&gt;

&lt;p&gt;Now the real run, on the machine this post was written on, unedited. &lt;/p&gt;

&lt;p&gt;The agent&#39;s first move is &lt;code&gt;run_tests&lt;/code&gt; — it sees the failure itself: &lt;strong&gt;1 failed, 3 passed&lt;/strong&gt;. Then it reads both files: the failing test, then &lt;code&gt;cart.py&lt;/code&gt;. It names the bug in one sentence: &lt;code&gt;add()&lt;/code&gt; replaces the quantity instead of accumulating. Then the proposal arrives — and the run stops cold at the gate. The proposed file is on screen; nothing on disk has changed. The proposed file changes only the &lt;code&gt;add&lt;/code&gt; method. Approve — the write lands — and the agent runs the suite again on its own: &lt;strong&gt;4 passed&lt;/strong&gt;. Found, diagnosed, proposed, approved, fixed, proven.&lt;/p&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmKCZ83m6NK8q5ei9aBe862nGBUtmhZS6LtYOnnaFU5Ao3rxp4Gn_It9lS6VdJZRiKNJOdZ3fjKUJ-OPsR51E3ZEQUDDcOOeKVYHhUyf-EtcAlg5rjVTgARwdT9zy4LdD5wIYvzgbF6wW2uMzYDbZrWw9m0Nqwi7jO_5kUZ-NUq9fDIyGCqW_pjXoKigw/s1600/s9-run-part1.png&quot; alt=&quot;Terminal transcript of the live run: the agent calling run_tests, reading the test and cart.py, and naming the bug&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Step 7: the run&#39;s first half — the agent finds the failure, reads both files, and diagnoses the bug itself.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg3DNLmd3EYZUrGI1IHLVqFOxbf51IMJzjuix9vApBOuX8X4q7oT7Bb-MHx87UZZ19l_Rh180Dz7u7JJBxjw9JXU8dx72Fo-WY-aVGHSHTSeoeKEs3v_VqEWRF6XNE-getGtyyX2BOtk3BaxEzlz0fqlTo3ugLTxbxYfXm83nV289wMdzrpd0_oYzUHPio/s1600/s10-payoff.png&quot; alt=&quot;Terminal transcript of the live run after approval: the suite rerunning to 4 passed and the agent&#39;s summary&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Step 7, payoff: after the approval, the agent re-runs the suite on its own — 4 passed — and summarises the fix.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;h2&gt;Step 8 — The bill&lt;/h2&gt;

&lt;p&gt;An agent you cannot price is an agent you cannot plan around, so the agent counts its own tokens: every model turn reports its usage, and the program adds them up and prices them at the rate card. This run used &lt;strong&gt;7,017 tokens in&lt;/strong&gt; and &lt;strong&gt;1,202 tokens out&lt;/strong&gt; — output counted including thinking tokens, because that is how the official rate card bills them — for a total of about &lt;strong&gt;$0.0098&lt;/strong&gt; at $0.75 / $3.75 per million. The lesson is not that it is cheap; it is that the cost is &lt;em&gt;knowable&lt;/em&gt;, per run, from numbers the run itself prints.&lt;/p&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjGAhrsrQnngkUGFVxUS1xHlV3L5YnlDTWQ-N9WsnQLNW1B04mfrPssMqdbyWc9cEblEtSl0gc0NRGBOvy-f-aZAOlHgT0F2XWbs5CVpCX7if4d4K-T494YOOXvlfVgtcG-tMbEAmKrlGy0cRlToGoYOP6e3rICbtTQvLJvtnrIItBY_nM49lddRSoPW_4/s1600/s11-bill.png&quot; alt=&quot;Terminal showing the run&#39;s closing line: the token counts and the computed cost of the fix&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Step 8: the run prints its own bill — real token counts from the run&#39;s usage metadata, priced at the verified rate card.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;h2&gt;Step 9 — Guardrails: code, not prompts&lt;/h2&gt;

&lt;p&gt;Name the guardrails, because they are the difference between a tool and a liability:&lt;/p&gt;

&lt;ol&gt;&lt;li&gt;&lt;strong&gt;The approval gate&lt;/strong&gt; — no write is ever ungated; a refusal is final and the model is told not to retry.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Confinement&lt;/strong&gt; — every tool is locked to one project folder, enforced by the resolved-path check in code, not requested in a prompt.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;The step cap&lt;/strong&gt; — twelve turns, then it stops, whatever it believes.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;What the agent does not have&lt;/strong&gt; — no shell tool, so it cannot run arbitrary commands; no network tool, so it cannot send your code anywhere.&lt;/li&gt;&lt;/ol&gt;

&lt;p&gt;These are the same patterns the serious tools use: Claude Code confines its writes and asks before dangerous actions, and approval-routing designs send a risky action to a human before it is allowed to execute. Prompts are suggestions. Code is enforcement. Build the enforcement.&lt;/p&gt;

&lt;h2&gt;Step 10 — The honest verdict&lt;/h2&gt;

&lt;p&gt;What it nailed: the diagnosis was exact, the patch minimal, and it stopped when the suite went green instead of gold-plating. Where it wobbled: it rewrote the whole file to change three lines in one method — whole-file writes are simple, but blunt — and the file it saved still carries its header comment claiming a bug lives here on purpose, which stopped being true the moment the fix landed. That is what the gate is for: a human reading the proposal catches the stale comment; an auto-approver ships it. And the limits, plainly: this is a teaching agent for small, well-tested code. A full tool brings a terminal, a whole-codebase index, and years of hardening this one file does not have. Build one anyway — not to replace those tools, but because the day you have written the loop, the gate, and the confinement yourself, you understand what every agent product is actually doing under the hood. That understanding is the real deliverable.&lt;/p&gt;

&lt;h2&gt;Errors you may hit&lt;/h2&gt;

&lt;ol&gt;&lt;li&gt;&lt;strong&gt;Bad or expired key&lt;/strong&gt; — the program stops before anything runs (&lt;code&gt;Set GEMINI_API_KEY first&lt;/code&gt;). Set the variable in the same terminal you run the agent from.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;The model repeats a tool call&lt;/strong&gt; — the step cap ends the loop at twelve. Read the transcript; the fix is usually a clearer task sentence.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;A patch breaks a different test&lt;/strong&gt; — the suite goes red again and the loop continues, and the gate gives you a second decision. Never approve a fix you have not read.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;&lt;code&gt;pytest&lt;/code&gt; not found&lt;/strong&gt; — your venv is not active. Activate it, and run the agent from the project folder.&lt;/li&gt;&lt;/ol&gt;

&lt;h2&gt;Go further: the agent files its own report (GitHub MCP)&lt;/h2&gt;

&lt;p&gt;The companion video&#39;s Short takes one more step: after the fix, the agent opens &lt;a href=&quot;https://github.com/sajjad037/visual-studio-learn-code/issues/16&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;issue #16&lt;/a&gt; in this blog&#39;s code repo, summarising the bug and the fix — through GitHub&#39;s official MCP server, and it asks at the gate before posting. The server is &lt;strong&gt;remote&lt;/strong&gt;, hosted by GitHub at &lt;code&gt;https://api.githubcopilot.com/mcp/&lt;/code&gt;; for an issues-only connection you can scope it to &lt;code&gt;https://api.githubcopilot.com/mcp/x/issues&lt;/code&gt;. Authenticate with a &lt;strong&gt;fine-grained personal access token&lt;/strong&gt; — a PAT: an API password whose permissions you can limit — &lt;strong&gt;scoped to the one repository, with Issues set to Read and write&lt;/strong&gt;. Least privilege, exactly like the confinement in Step 3. The google-genai SDK can also consume an MCP session directly as a tool (its docs mark the support experimental); either way, the rule from Step 5 travels with it: an outward-facing write gets a human gate first.&lt;/p&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEioRpARfvpjOAvqyO048HOxJRjmsuy463Jy_Lp-_2uxayCzeTpVoa5YdPAqPG8r3uAilEv2NEjToYFK8O7zqXCx6_A4IgKvt-U5yIHLr-5Is8TFkiXtKau05Lfc4ysjgdQYAQZDp8IdSWZhU3wTNfVsUipp6LDbEX_cMCPShG8SPs6XQYuiR47Y5uOD6C0/s1600/s12-mcp-issue.png&quot; alt=&quot;Browser showing the GitHub issue the agent opened in the visual-studio-learn-code repo, summarising the bug and the fix&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;The artifact: the issue the agent filed in the repo after the fix — proposed, approved at the gate, then posted.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;h2&gt;Sources&lt;/h2&gt;

&lt;ul&gt;&lt;li&gt;Google, &lt;a href=&quot;https://ai.google.dev/gemini-api/docs/pricing&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Gemini Developer API pricing&lt;/a&gt; — the gemini-3.8-flash rate card and the free-tier data-use note (accessed 2026-11-07).&lt;/li&gt;&lt;li&gt;Google, &lt;a href=&quot;https://googleapis.github.io/python-genai/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Google Gen AI SDK documentation&lt;/a&gt; — the manual function-calling pattern, FunctionDeclaration, and function-response roles (accessed 2026-11-07).&lt;/li&gt;&lt;li&gt;GitHub, &lt;a href=&quot;https://github.com/github/github-mcp-server&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;github/github-mcp-server&lt;/a&gt; — the remote MCP endpoint, toolsets, and token guidance (accessed 2026-11-07).&lt;/li&gt;&lt;li&gt;Google AI Studio — &lt;a href=&quot;https://aistudio.google.com&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;aistudio.google.com&lt;/a&gt; (free API keys).&lt;/li&gt;&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Companion code:&lt;/strong&gt; the complete agent, the demo project, and the tests are free in the repo folder linked at the top of this post. The companion video walks the same build on the real screens.&lt;/p&gt;

&lt;div style=&quot;text-align:center;border:1px solid #e3e6ea;border-radius:8px;padding:18px 14px;margin:28px 0;background:#fbfcfd;&quot;&gt;&lt;h3 style=&quot;margin:0 0 6px;&quot;&gt;Enjoying these tutorials?&lt;/h3&gt;&lt;p style=&quot;margin:0 0 6px;&quot;&gt;Get new Visual Studio Learn posts by email — free, no spam:&lt;/p&gt;&lt;style&gt;
    .followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview {
  display: flex !important;
  flex-direction: column !important;
  justify-content: center !important;
  margin-top: 30px !important;
  padding: clamp(17px, 5%, 40px) clamp(17px, 7%, 50px) !important;
  max-width: none !important;
  border-radius: 6px !important;
  box-shadow: 0 5px 25px rgba(34, 60, 47, 0.25) !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview,
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview *{
  box-sizing: border-box !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-heading {
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-heading h5{
  margin-top: 0 !important;
  margin-bottom: 0 !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field {
  margin-top: 20px !important;
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input {
  width: 100% !important;
  height: 40px !important;
  border-radius: 6px !important;
  border: 2px solid #e9e8e8 !important;
  background-color: #fff;
  outline: none !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input {
  color: #000000 !important;
  font-family: &quot;Montserrat&quot; !important;
  font-size: 14px;
  font-weight: 400;
  line-height: 20px;
  text-align: center;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input::placeholder {
  color: #000000 !important;
  opacity: 1 !important;
}

.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input:-ms-input-placeholder {
  color: #000000 !important;
}

.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input::-ms-input-placeholder {
  color: #000000 !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button {
  margin-top: 10px !important;
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button button {
  width: 100% !important;
  height: 40px !important;
  border: 0 !important;
  border-radius: 6px !important;
  line-height: 0px !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button button:hover {
  cursor: pointer !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .powered-by-line {
  color: #231f20 !important;
  font-family: &quot;Montserrat&quot; !important;
  font-size: 13px !important;
  font-weight: 400 !important;
  line-height: 25px !important;
  text-align: center !important;
  text-decoration: none !important;
  display: flex !important;
  width: 100% !important;
  justify-content: center !important;
  align-items: center !important;
  margin-top: 10px !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .powered-by-line img {
  margin-left: 10px !important;
  height: 1.13em !important;
  max-height: 1.13em !important;
}

  &lt;/style&gt;
  &lt;div class=&quot;followit--follow-form-container&quot; attr-a attr-b attr-c attr-d attr-e attr-f&gt;
    &lt;form data-v-f93f6730=&quot;&quot; action=&quot;https://api.follow.it/subscription-form/SmRpWjEzV1ovU0FOK2R2Y0oxajFBUmtIdW44K1ZiUWZRUm1FZVdpVlRKUHFLSFBKWjE4M1hTSk5yRU5oNmg0RU5zelhuTllNTGU1UmwzRkN0NzZWbnd2YkV5VWFwYnptWFdtekI3eXdwMmhtamtUNit3dlNSblVmRjJxd1JGbjZ8Q05yQWR2eitvYW5HRmNMMzJkbStpM09rNmxXZ0ZjWWhyOERya2VvVXRzZz0=/8&quot; method=&quot;post&quot;&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;form-preview&quot; style=&quot;background-color: rgb(255, 255, 255); border-style: solid; border-width: 1px; border-color: rgb(204, 204, 204); position: relative;&quot;&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;preview-heading&quot;&gt;&lt;h5 data-v-f93f6730=&quot;&quot; style=&quot;font-family: Arial; font-weight: bold; color: rgb(0, 0, 0); font-size: 16px; text-align: center; text-transform: none !important;&quot;&gt;Get new posts by email:&lt;/h5&gt;&lt;/div&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;preview-input-field&quot;&gt;&lt;input data-v-f93f6730=&quot;&quot; type=&quot;email&quot; name=&quot;email&quot; required=&quot;&quot; placeholder=&quot;Enter your email&quot; spellcheck=&quot;false&quot; style=&quot;font-family: Arial; font-weight: normal; color: rgb(0, 0, 0); font-size: 14px; text-align: center; background-color: rgb(255, 255, 255); text-transform: none !important;&quot; /&gt;&lt;/div&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;preview-submit-button&quot;&gt;&lt;button data-v-f93f6730=&quot;&quot; type=&quot;submit&quot; style=&quot;font-family: Arial; font-weight: bold; color: rgb(255, 255, 255); font-size: 16px; text-align: center; background-color: rgb(0, 0, 0); text-transform: none !important;&quot;&gt;Subscribe&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/form&gt;&lt;a href=&quot;https://follow.it&quot; class=&quot;powered-by-line&quot;&gt;Powered by &lt;img src=&quot;https://follow.it/images/colored-logo.svg&quot; alt=&quot;follow.it&quot; height=&quot;17px&quot;/&gt;&lt;/a&gt;
  &lt;/div&gt;&lt;p style=&quot;margin:12px 0 0;font-size:14px;&quot;&gt;Follow along: &lt;a href=&quot;https://www.youtube.com/@VisualStudioLearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/youtube/FF0000&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;YouTube&quot; /&gt; YouTube&lt;/a&gt; · &lt;a href=&quot;https://www.facebook.com/VisualStudio.Learn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/facebook/1877F2&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;Facebook&quot; /&gt; Facebook&lt;/a&gt; · &lt;a href=&quot;https://www.instagram.com/visualstudiolearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/instagram/E4405F&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;Instagram&quot; /&gt; Instagram&lt;/a&gt; · &lt;a href=&quot;https://www.tiktok.com/@visualstudiolearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/tiktok/000000&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;TikTok&quot; /&gt; TikTok&lt;/a&gt; · &lt;a href=&quot;https://www.visualstudiolearn.com/feeds/posts/default?alt=rss&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/rss/FFA500&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;RSS&quot; /&gt; RSS&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;

</content><link rel='replies' type='application/atom+xml' href='http://www.visualstudiolearn.com/feeds/3265395906264283738/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.visualstudiolearn.com/2026/11/build-your-own-coding-agent-in-python.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/3265395906264283738'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/3265395906264283738'/><link rel='alternate' type='text/html' href='http://www.visualstudiolearn.com/2026/11/build-your-own-coding-agent-in-python.html' title='Build Your Own Coding Agent in Python (It Fixes Real Code — Behind an Approval Gate)'/><author><name>The Loonie Lesson</name><uri>http://www.blogger.com/profile/02243798795439866634</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRY4V9cFLhqMxHWBOWCGBiGUqjan14Vda_dy5rz-C41kApceM-u6jAxS2_6AIRZtGkQeyiQolq1gCeSBhpo573lYq37MLmMGgSKDQLasowic65VoitivYCzqrmUDUqxJuqCTyzah8JuALYtW0Iz7rXIvIcj-ovhVpEuEhQJ6yG1FUQ-A/s1600/loonie-lesson-logo-profile.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjBXCKbUqfOOcE_i5Qnz9S6OY4YE9wHv6f6t2RHjYkSx_ZlrSXGMWpoptO0K9y9wd9H9X6yMWC9Xssz_-c7I78ZXA_6jw8Db-xMtvGoDbJ58noUBm38Xya1z_-4RlttD8sGS1442duJ3xiQUPRzZpkcHOfnBCAnauDyCTIduNF_bIh0ACcU5gcjpZ4y8N0/s72-c/s0-setup.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8861905861585113921.post-8171362774294879324</id><published>2026-11-11T07:15:49.000-05:00</published><updated>2026-11-22T02:50:58.905-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="DevOps"/><category scheme="http://www.blogger.com/atom/ns#" term="FastAPI"/><category scheme="http://www.blogger.com/atom/ns#" term="Grafana"/><category scheme="http://www.blogger.com/atom/ns#" term="Monitoring"/><category scheme="http://www.blogger.com/atom/ns#" term="Prometheus"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><title type='text'>Monitor a FastAPI App with Prometheus + Grafana (Metrics, Dashboards, Alerts)</title><content type='html'>&lt;p&gt;Your FastAPI app is slow, and you cannot see it. There is no graph of your traffic, no counter of your errors, and nothing that tells you &lt;em&gt;which&lt;/em&gt; endpoint is the slow one. The first symptom is a user complaint — and by then you are debugging blind.&lt;/p&gt;

&lt;p&gt;In this tutorial you will fix that, end to end, on your own machine: instrument a FastAPI app so it publishes its own metrics, collect them with Prometheus, and build a Grafana dashboard that catches a slow endpoint live — then set an alert that fires when it happens again. Everything is local and free: no accounts, no API keys, no cloud.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What we are using&lt;/strong&gt; (pinned, so your install matches this post exactly): prometheus-fastapi-instrumentator 8.1.0 · Prometheus 3.16.0 · Grafana OSS 13.3.0 · FastAPI 0.162.4 · uvicorn 0.54.0 · Python 3.12. The complete companion code — the app, the Prometheus config, the load script, the exported dashboard JSON and the alert rule — is in the &lt;a href=&quot;https://github.com/sajjad037/visual-studio-learn-code&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;companion repository&lt;/a&gt;, folder &lt;code&gt;topic-10-prometheus-grafana/&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img alt=&quot;Grafana RED dashboard after the FastAPI load burst, p95 settling&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjzItyMhIH1oPTj57zbkCJM1tHDiUZSsiZKq6N6VLe6uxItIxDmcZWM0n0Bq_QMtJ-0_wRYTWsM9i_HllA8jMWP9dGTYGJwlKmERqHBd5mHTTDSJVsotQqiwnQBQCrzl7bxIjZE-3cTomB-SU6jzCRsnHN49JpB1mcRPEsJBU10B6MpxcU_959-mG0U09s/s1600/grafana-dashboard-after-fastapi-load-burst.png&quot; /&gt;&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;iframe allowfullscreen=&quot;&quot; class=&quot;BLOG_video_class&quot; height=&quot;266&quot; src=&quot;https://www.youtube.com/embed/7M9h7TpwSo8&quot; width=&quot;320&quot; youtube-src-id=&quot;7M9h7TpwSo8&quot;&gt;&lt;/iframe&gt;&lt;/div&gt;&lt;br /&gt;

&lt;h2&gt;Step 1 — Install the stack&lt;/h2&gt;

&lt;p&gt;Create a virtual environment and install the app stack. The instrumentator is the only monitoring-specific package the app needs:&lt;/p&gt;

&lt;pre&gt;$ python -m venv venv &amp;amp;&amp;amp; source venv/bin/activate
$ pip install fastapi uvicorn prometheus-fastapi-instrumentator&lt;/pre&gt;

&lt;p&gt;Pin what you installed in a &lt;code&gt;requirements.txt&lt;/code&gt; (&lt;code&gt;fastapi==0.162.4&lt;/code&gt;, &lt;code&gt;uvicorn==0.54.0&lt;/code&gt;, &lt;code&gt;prometheus-fastapi-instrumentator==8.1.0&lt;/code&gt;, and &lt;code&gt;httpx==0.28.1&lt;/code&gt;, which the load script in Step 9 uses). Prometheus and Grafana themselves are single binaries you download from their official sites and run locally — no Docker required, which is how this post was produced.&lt;/p&gt;

&lt;h2&gt;Step 2 — The demo app: one endpoint per signal&lt;/h2&gt;

&lt;p&gt;The demo app is deliberately small, with one endpoint per thing a dashboard should show you:&lt;/p&gt;

&lt;pre&gt;import asyncio

from fastapi import FastAPI
from fastapi.responses import JSONResponse

app = FastAPI(title=&quot;Demo Shop API&quot;)

@app.get(&quot;/&quot;)
async def home():  # instant JSON — the healthy traffic
    return {&quot;status&quot;: &quot;ok&quot;, &quot;service&quot;: &quot;demo-shop&quot;}

@app.get(&quot;/slow&quot;)
async def slow():  # sleeps 0.8s, then answers
    await asyncio.sleep(0.8)
    return {&quot;status&quot;: &quot;ok&quot;, &quot;detail&quot;: &quot;that took a while&quot;}

@app.get(&quot;/boom&quot;)
async def boom():  # HTTP 500, every time
    return JSONResponse(status_code=500, content={&quot;detail&quot;: &quot;something broke&quot;})&lt;/pre&gt;

&lt;p&gt;That file is complete and runnable as written — it is the companion repo&#39;s &lt;code&gt;app.py&lt;/code&gt; minus the two monitoring lines, which are Step 3.&lt;/p&gt;

&lt;p&gt;Run it with &lt;code&gt;uvicorn app:app --port 8000&lt;/code&gt; and curl all three: &lt;code&gt;/&lt;/code&gt; answers instantly, &lt;code&gt;/slow&lt;/code&gt; takes 0.80s on the timer, and &lt;code&gt;/boom&lt;/code&gt; returns a clean HTTP 500. Nothing is monitored yet — that is the point. We are about to watch all three behaviours appear on a dashboard.&lt;/p&gt;

&lt;h2&gt;Step 3 — Instrument the app (two calls)&lt;/h2&gt;

&lt;p&gt;This is the whole integration. Add the import and one line after you create the app:&lt;/p&gt;

&lt;pre&gt;from prometheus_fastapi_instrumentator import Instrumentator

Instrumentator().instrument(app).expose(app)&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;instrument(app)&lt;/code&gt; wraps your app so every request is measured; &lt;code&gt;expose(app)&lt;/code&gt; serves those measurements at a new endpoint, &lt;code&gt;/metrics&lt;/code&gt;. Restart uvicorn, generate a few requests, and open &lt;code&gt;http://localhost:8000/metrics&lt;/code&gt; in your browser.&lt;/p&gt;

&lt;h2&gt;Step 4 — Read what /metrics gives you for free&lt;/h2&gt;

&lt;p&gt;&lt;img alt=&quot;The /metrics page exposed by prometheus-fastapi-instrumentator&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgB7_M48XugTsm1-SHRusmLwywo-QoLftIw-8TLwOvyZDhyphenhyphen4k1dit0isvh3bUeKqUJDUlOBLv5pWqf9TUCYyR_kE6Aa2BGE_Eq8WzJ3uF4hEz8CDmt-Rk9rI9qur-wA_3PgzVsLK-2hPb0EvVE7Q6DpE-EepoVxVdp_T9uZo4XZPHdkRRAjAO5iy0f_zcc/s1600/fastapi-metrics-endpoint.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Without writing a single metric yourself, the instrumentator publishes five defaults. The three that matter most:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;http_requests_total&lt;/code&gt; — a counter of every request, labelled by &lt;code&gt;handler&lt;/code&gt; (your endpoint), &lt;code&gt;status&lt;/code&gt; and &lt;code&gt;method&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;http_request_duration_seconds&lt;/code&gt; — a histogram of how long requests take, in buckets. This is where response-time percentiles come from.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;http_request_size_bytes&lt;/code&gt; and &lt;code&gt;http_response_size_bytes&lt;/code&gt; — request and response sizes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;One gotcha to learn now, because it bites everyone:&lt;/strong&gt; the status label is &lt;em&gt;grouped&lt;/em&gt;. After hitting &lt;code&gt;/boom&lt;/code&gt;, the counter reads &lt;code&gt;http_requests_total{handler=&quot;/boom&quot;, status=&quot;5xx&quot;}&lt;/code&gt; — there is no &lt;code&gt;status=&quot;500&quot;&lt;/code&gt; series. Remember that when we write queries in Step 6, and again in the Traps section at the end.&lt;/p&gt;

&lt;p&gt;&lt;img alt=&quot;http_request_duration_seconds histogram buckets in the /metrics output&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhLj89yBpiB1GHpicNp_KKMGetF4S62gcObhlGGIVr00fmX2g11JfcgB0cEs8CbqV_AHzhj0_i2f60H22Fe0QFjbT8UQUuKC71HebSydg6IhWm_ohoI-b6DL77JUiZcazJRr53kUw9u7LZikwXEwodAEr4wHNLmOYsU3aouR2IaeF5r7Nj10QLee21o7b4/s1600/prometheus-duration-histogram-buckets.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The duration histogram&#39;s default buckets are &lt;code&gt;le = 0.1, 0.5, 1.0, +Inf&lt;/code&gt; seconds. A percentile computed from a histogram is an &lt;em&gt;interpolated estimate&lt;/em&gt; inside a bucket, not an exact measurement — keep that in mind when we read &quot;0.975 seconds&quot; in Step 9: it is the honest estimate the buckets support, and widening the buckets is how you trade precision for cost.&lt;/p&gt;

&lt;h2&gt;Step 5 — Point Prometheus at the app&lt;/h2&gt;

&lt;p&gt;Prometheus &lt;em&gt;pulls&lt;/em&gt; metrics: every few seconds it fetches &lt;code&gt;/metrics&lt;/code&gt; and stores the numbers as time series. Create &lt;code&gt;prometheus.yml&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;global:
  scrape_interval: 5s   # a demo choice — the default is 1m

scrape_configs:
  - job_name: fastapi
    static_configs:
      - targets: [&quot;localhost:8000&quot;]&lt;/pre&gt;

&lt;p&gt;The 5-second scrape interval is a deliberate demo choice so the dashboard moves while you watch; in production, 15–60 seconds is normal. Start Prometheus with this config (&lt;code&gt;./prometheus --config.file=prometheus.yml&lt;/code&gt; from the folder you extracted it to), then open &lt;code&gt;http://localhost:9090/targets&lt;/code&gt;: the &lt;code&gt;fastapi&lt;/code&gt; target must read &lt;strong&gt;UP&lt;/strong&gt;. If it reads DOWN, stop and fix that first — every later step depends on it (see Traps).&lt;/p&gt;

&lt;p&gt;&lt;img alt=&quot;Prometheus Targets page showing the fastapi job UP&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjGVb82lLuMxs0zcChcjua0B7BTFgPO2oAhJKeYLTx9or2SMOtliCPlCck4UmOXs_P9ia4X2-Ca4P787-jdFdyNutvss7govYqca-VP14EWwIDXq_zZIEVKv78-M3wx_zF1KH_FB7C-3u-CxJx5QkADyuUZLthJg7J9DdeucNfKiymr3WG_DQfPHGu34O8/s1600/prometheus-targets-up.png&quot; /&gt;&lt;/p&gt;

&lt;h2&gt;Step 6 — Ask three questions with PromQL&lt;/h2&gt;

&lt;p&gt;Monitoring dashboards are built from three questions — the &lt;strong&gt;RED method&lt;/strong&gt;: &lt;strong&gt;Rate&lt;/strong&gt; (how much traffic am I getting?), &lt;strong&gt;Errors&lt;/strong&gt; (how much of it is failing?), &lt;strong&gt;Duration&lt;/strong&gt; (how slow is it?). Each one is a single Prometheus query. Try them in Prometheus&#39;s own graph tab first (&lt;code&gt;http://localhost:9090/graph&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rate&lt;/strong&gt; — requests per second, per endpoint. &lt;code&gt;rate(...)&lt;/code&gt; turns the ever-growing counter into a per-second rate over a window — here, the last 5 minutes:&lt;/p&gt;

&lt;pre&gt;sum by (handler) (rate(http_requests_total{handler!=&quot;/metrics&quot;, handler!=&quot;none&quot;}[5m]))&lt;/pre&gt;

&lt;p&gt;&lt;img alt=&quot;PromQL rate query graph in Prometheus, requests per second by handler&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhmOqOUNYpWVjgVHJYAmTo72Ktb54Tsb_DXHTszGX_ODuqxoIfr_yRfFqZtjm2XhjJfzW8GnlNNGy76htmXcm1DXmEsyXBAMPuzC8A90fQ0ih8BSseoTnXNQREP5qbaE_F49sYw82MfQJyl8BC4cmcaHK9xH0C8w6nl1FF6MFNS2PmyL99-T-fbE9S8Ohg/s1600/promql-rate-query-graph.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Errors&lt;/strong&gt; — the share of responses that are 5xx. Note the grouped label from Step 4: the numerator selects &lt;code&gt;status=&quot;5xx&quot;&lt;/code&gt;, because &lt;code&gt;status=&quot;500&quot;&lt;/code&gt; does not exist:&lt;/p&gt;

&lt;pre&gt;sum(rate(http_requests_total{status=&quot;5xx&quot;}[5m]))
/
sum(rate(http_requests_total{handler!=&quot;/metrics&quot;}[5m]))&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Duration&lt;/strong&gt; — the 95th percentile response time, per endpoint. Read it as: &quot;95% of requests were faster than this.&quot; &lt;code&gt;histogram_quantile&lt;/code&gt; does the bucket interpolation for you:&lt;/p&gt;

&lt;pre&gt;histogram_quantile(0.95,
  sum by (handler, le) (rate(http_request_duration_seconds_bucket{handler!=&quot;/metrics&quot;, handler!=&quot;none&quot;}[5m])))&lt;/pre&gt;

&lt;p&gt;&lt;img alt=&quot;PromQL histogram_quantile p95 graph in Prometheus&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiW6b8U4P3yR2fz9KEzc_iz2GCJtBXJKc7SsUV6xYvu2Lj-1yFJNenx9voiny67Lf4OY49s0Pq79RBUCJm5tfxBlcTi0J82ONL1X31XDLzAqn8pH1NBMVZ5UMPvxnbkBGy-4JxKp-f48o5H93aCvPCuMXbpL2yoGeteImc76S8Vzwzjj2nC_Yg0BAd9_ps/s1600/promql-p95-histogram-quantile.png&quot; /&gt;&lt;/p&gt;

&lt;h2&gt;Step 7 — Connect Grafana to Prometheus&lt;/h2&gt;

&lt;p&gt;Start Grafana (run the &lt;code&gt;grafana server&lt;/code&gt; binary from the folder you extracted it to) and sign in at &lt;code&gt;http://localhost:3000&lt;/code&gt; (the first-run login is &lt;code&gt;admin&lt;/code&gt; / &lt;code&gt;admin&lt;/code&gt;). Go to &lt;strong&gt;Connections → Data sources → Add data source → Prometheus&lt;/strong&gt;, and set the URL to &lt;code&gt;http://localhost:9090&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;img alt=&quot;Grafana add data source form for Prometheus&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpTyoumc9_P1njuJpjmiX30-4O0jFDPWyuGajXfyhRHKj9s_0Ky-pW1Mnp1T0bczRypEXifadoacQgsRyqWn2BNtgwErwn_fhSDF1H-rVYw6ivj_jm_IzqMAXEGoGri1RW7SeAZD2KL3n04kS5pGl9L9GK9GJN6xuOYAYoNGeizoP_UI23xbA9I36jQno/s1600/grafana-prometheus-data-source-form.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Click &lt;strong&gt;Save &amp;amp; test&lt;/strong&gt;. You want the green confirmation that Grafana successfully queried the Prometheus API — that message is the whole step done:&lt;/p&gt;

&lt;p&gt;&lt;img alt=&quot;Grafana data source saved and successfully tested&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgitlY6GsuBpuxrPX6V2TynybT6LCZ8_ib0NhfO-DL-FozsKqrk2A8mbuWW6MfFlfOoVxntdNc0NWhyASUtrPodjYFwIDXC0l_BUfcXihKT5nHJGGkF24LOHlNKC8v0Ar2qRjOTc2rF3F96rYuQR3XpBZhISpGXj4m8u7sBW5aYCkt_qbjyxyKopldEr8s/s1600/grafana-data-source-saved-test.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Running Grafana in Docker instead?&lt;/em&gt; Then &lt;code&gt;localhost&lt;/code&gt; is the container, not your machine — use &lt;code&gt;host.docker.internal&lt;/code&gt; in the URL. This is the single most common reason a first data source fails its test (see Traps).&lt;/p&gt;

&lt;h2&gt;Step 8 — Build the RED dashboard&lt;/h2&gt;

&lt;p&gt;Create a dashboard with three panels — one per question from Step 6, using the same three queries:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Rate — requests/sec by handler&lt;/strong&gt; (the rate query)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Errors — share of 5xx responses&lt;/strong&gt; (the errors query; set the unit to percent 0–1)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Duration — p95 and p50 by handler&lt;/strong&gt; (the duration query, plus the same query at 0.50)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img alt=&quot;Grafana panel editor with the PromQL rate query&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgB7GIivpn2l7aIP8AArpmAKCgvkePkz-aIwgRTcgKWhmrntEjY4xYm6e5tJQKOwjdBZk90BL0iJ3A5j5SHOO0WHboI0l9eq_7yFF-R_3Z4h0WQYSpqXa50JhHStW1gA_RGQ-kv4fa8pDDcg4xro-FvDXczYOBJv0ZA69QYWnnUEHCzuGQ2MSzr3QyhJE8/s1600/grafana-panel-editor-promql.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Set the dashboard to refresh every 5 seconds and show the last 15 minutes. Send some gentle traffic to the app and you should see the baseline: about a request a second, a small 5xx share from &lt;code&gt;/boom&lt;/code&gt;, and the Duration panel already telling a story — &lt;code&gt;/slow&lt;/code&gt; sits up near a second while everything else hugs the floor.&lt;/p&gt;

&lt;p&gt;&lt;img alt=&quot;Grafana RED dashboard for FastAPI: Rate, Errors and Duration panels&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-5zFv9Y8D9voVoZAkSL4Fuw6oa8ApzTG8kqlt_Q1lEs1QBPBsCuMc1bX_vdlKYO6Gn4S-YyPr24eaRx3k-lu1sjDL7NZ9M7L3Xz7v_GC_LOUQoxVamKJCO7TQtlG6y0G6D2tL3dRk4-F_5-1nOOUp3A6MnsE4DYPQPIRx6dD-YC5U03G7Sqy3Kwa7CNk/s1600/grafana-fastapi-red-dashboard.png&quot; /&gt;&lt;/p&gt;

&lt;h2&gt;Step 9 — The burst: watch it catch a slow endpoint live&lt;/h2&gt;

&lt;p&gt;Now attack your own app. The companion repository&#39;s &lt;code&gt;load_burst.py&lt;/code&gt; runs a four-phase load timeline — baseline, an all-endpoints burst, a slow-heavy phase, then an error-heavy phase. The full run made &lt;strong&gt;2,285 requests&lt;/strong&gt;: 1,767 to &lt;code&gt;/&lt;/code&gt;, 344 to &lt;code&gt;/slow&lt;/code&gt;, and 174 to &lt;code&gt;/boom&lt;/code&gt; (every one an HTTP 500).&lt;/p&gt;

&lt;p&gt;&lt;img alt=&quot;Grafana dashboard during the load burst, p95 rising&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjibZBIl3oyWgVDXx-AscsXc5UN_qagxAXAuwSSAF8KmdykXhjVF5q4eio3R98Gc6YuPXhLeI1j4b9IRyYvkC4vLaxmvtSNYzz74r_zHe8WRLFZlab4Iiz36U_2bt0-5HLjzIUVKsXorTL2vooPZXLO-EHX51b9OxrBqiJMwTHGBpVHkcaPkYgMaSJwis4/s1600/grafana-dashboard-during-load-burst.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;What the dashboard showed, in order:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Rate climbed first&lt;/strong&gt; — peaking at roughly 19 requests per second measured on a 2-minute window. (Your dashboard&#39;s 5-minute window will show a lower peak, around 6 req/s. Both are correct — a rate is meaningless without its window, which is why this post states the window every time.)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Errors followed&lt;/strong&gt; — at the peak of the error-heavy phase, &lt;strong&gt;over 11% of responses were failing&lt;/strong&gt; (11.4% measured).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Duration is the tell.&lt;/strong&gt; &lt;code&gt;/slow&lt;/code&gt;&#39;s p95 read &lt;strong&gt;0.975 seconds&lt;/strong&gt; — and it had read that since the very first scrape, long before the burst, because the endpoint sleeps 0.8 seconds and the histogram interpolates inside its 0.5–1.0 bucket. The dashboard did not just catch the burst; it had already fingered the slow endpoint at baseline. (The overall p95 across all endpoints peaked at about &lt;strong&gt;0.90 seconds&lt;/strong&gt; during the burst — remember that number when we set the alert threshold in Step 10.)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img alt=&quot;Grafana Errors panel at the burst peak, 5xx share above 11 percent&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9U4NwhzlDbj6auklbVRw82C8k-mg5Wi4Adj66tJF5a3kRotucFgAan_lxFDngDls6TLM8EdJYIHdvvH93-YBoSDK8NpOc2ttlyG27mza8cZ3VnWZs_4yXE5MS_DP9IBUUMA3Po3yeZkt14UeWan6ICGZk_Jf8skSXS85FzdJtMfAVdWbLj4ge-gSWq58/s1600/grafana-5xx-error-share-peak.png&quot; /&gt;&lt;/p&gt;

&lt;h2&gt;Step 10 — One alert: know when it happens again&lt;/h2&gt;

&lt;p&gt;A dashboard you must watch is a chore; an alert watches for you. In Grafana: &lt;strong&gt;Alerting → Alert rules → New alert rule&lt;/strong&gt;. Query the &lt;em&gt;overall&lt;/em&gt; p95 (the duration query without the per-handler split), and set the condition: when the last value of that query &lt;strong&gt;is above 0.5&lt;/strong&gt;, sustained for &lt;strong&gt;1 minute&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;img alt=&quot;Grafana alert rule: p95 above 0.5 seconds for one minute&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEifY4SE2mM6xTwODtS2QraWFRaBlCNh6htFa0YXwNzCLFTVlwieRqB29UxV1wSyV3_tqX64ytxvW37OoPii9TjCyYCgcGJlfTk7EyJZEi2JNi6EyLP5gAjTIqjG_DDqdp2TDDyU41z8TEyrVBpKoFxIL3HybOEsPopXo8sDFYrexbD3ZxYFF00QvNetN6g/s1600/grafana-alert-rule-p95.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Run the burst again and wait. Once the p95 stays above half a second for a full minute, the rule flips to &lt;strong&gt;Firing&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;img alt=&quot;Grafana alert rule in Firing state during the burst&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmQSoVlYMw6f2e1Kl4lRgkUSZcZG7Ij29AA_JZL111BnOTQfBlc-6FN7AOJokJjYXiKv9tgkSWojkyaXFiRoaXuc7gNalS3afN3VsDx0squrCgfQ5-R0W8oFazNSMHHyUJ4PLI4nhwyL_WOvCqlVw0Jr6OjAXa1ZBPNqgH88ntgewMwMfK-zNl_A5Hcag/s1600/grafana-alert-firing.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Two honest notes. First, this demo wires &lt;strong&gt;no notification channel&lt;/strong&gt; — the rule fires in Grafana&#39;s UI, and connecting email, Slack or a webhook (a &quot;contact point&quot;) is your next step; this post does not claim a message was delivered. Second, the same rule exists as a Prometheus alerting rule in YAML in the companion repository, for setups that alert from Prometheus instead of Grafana.&lt;/p&gt;

&lt;h2&gt;Traps (each one cost somebody an hour)&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Target DOWN.&lt;/strong&gt; If Prometheus shows your target as DOWN, nothing else can work. Usually the app is not running, or the port in &lt;code&gt;prometheus.yml&lt;/code&gt; is wrong. Check Targets &lt;em&gt;before&lt;/em&gt; debugging queries.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Docker networking.&lt;/strong&gt; If Grafana runs in Docker and Prometheus (or the app) runs on your host, &lt;code&gt;localhost&lt;/code&gt; inside the data-source URL points at the container. Use &lt;code&gt;host.docker.internal&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Querying &lt;code&gt;status=&quot;500&quot;&lt;/code&gt;.&lt;/strong&gt; It returns an empty result — not an error, just nothing — because the instrumentator groups statuses (&lt;code&gt;5xx&lt;/code&gt;). An empty graph here almost always means a label value that does not exist, not &quot;no errors&quot;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img alt=&quot;Prometheus query for status 500 returns an empty result because statuses are grouped as 5xx&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEifMvZz-MUw2DcaJ5yfZNgGXz7_DfE_Yt1hmsXPVMySa5VjaqZOoAdGhsL9q2mVtcdzw1Yo1jcTrNo1r69KMuDEa2F3SfrKSo1_k7ynftScSqPEagfvfUxv8pbHdiML7CU2ggt82Gs4e4I9fZCGOrzIQjA7pFzcqPpLf6gj6cJecODuYwK5wUogc1hOJkU/s1600/prometheus-status-500-empty-result.png&quot; /&gt;&lt;/p&gt;

&lt;h2&gt;Recap&lt;/h2&gt;

&lt;p&gt;You now have the full loop: the app publishes its own numbers at &lt;code&gt;/metrics&lt;/code&gt; (two calls to add), Prometheus scrapes them every few seconds, three PromQL queries answer Rate, Errors and Duration, a Grafana dashboard shows them live, and one alert fires when the p95 stays too high. The pattern transfers unchanged to a real application: keep the three questions, watch the labels (grouped statuses!), and always quote a rate with its window.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Practice task:&lt;/strong&gt; add the instrumentator to an app of your own, find your slowest endpoint&#39;s p95 at baseline, and write the number down. If it surprises you, the dashboard has already paid for itself.&lt;/p&gt;

&lt;h2&gt;FAQ&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Do I need Docker for Prometheus and Grafana?&lt;/strong&gt;&lt;br /&gt;No. Both are single binaries that run locally, which is how this tutorial was produced. Docker works too — just mind the &lt;code&gt;host.docker.internal&lt;/code&gt; note in Step 7.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why is my p95 an estimate?&lt;/strong&gt;&lt;br /&gt;Because it comes from a histogram: Prometheus knows how many requests fell into each duration bucket, and &lt;code&gt;histogram_quantile&lt;/code&gt; interpolates inside the bucket. It is the right tool for spotting slow endpoints, not a stopwatch.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What does this cost to run in production?&lt;/strong&gt;&lt;br /&gt;Prometheus and Grafana OSS are free and self-hosted. The real cost is storage, which you control with the scrape interval and retention settings — start at 15–60 second scrapes, not the 5 seconds this demo used.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The full code — app, Prometheus config, load script, dashboard JSON and alert rules — is free in the companion repository (folder &lt;code&gt;topic-10-prometheus-grafana/&lt;/code&gt;), and the video version of this build is on the Visual Studio Learn YouTube channel. Monitoring pairs well with deployment: once your app is observable, the next step is shipping it — see &lt;a href=&quot;https://www.visualstudiolearn.com/2026/10/how-do-i-deploy-fastapi-app-to-azure.html&quot;&gt;how to deploy a FastAPI app to Azure App Service&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;Sources&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://pypi.org/project/prometheus-fastapi-instrumentator/&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;prometheus-fastapi-instrumentator — PyPI project page&lt;/a&gt; (the instrumentator&#39;s defaults and label behaviour)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://prometheus.io/docs/prometheus/latest/querying/basics/&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;Prometheus — Querying basics&lt;/a&gt; (rate, histogram_quantile, label matchers)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://grafana.com/docs/grafana/latest/alerting/&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;Grafana — Alerting documentation&lt;/a&gt; (alert rules, conditions, pending/firing states)&lt;/li&gt;
&lt;li&gt;All measurements in this post (request counts, rates, percentiles, the firing alert) are from the author&#39;s own run of the companion code, November 2026.&lt;/li&gt;
&lt;/ul&gt;

&lt;div style=&quot;background: rgb(251, 252, 253); border-radius: 8px; border: 1px solid rgb(227, 230, 234); margin: 28px 0px; padding: 18px 14px; text-align: center;&quot;&gt;&lt;h3 style=&quot;margin: 0px 0px 6px;&quot;&gt;Enjoying these tutorials?&lt;/h3&gt;&lt;p style=&quot;margin: 0px 0px 6px;&quot;&gt;Get new Visual Studio Learn posts by email — free, no spam:&lt;/p&gt;&lt;style&gt;
    .followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview {
  display: flex !important;
  flex-direction: column !important;
  justify-content: center !important;
  margin-top: 30px !important;
  padding: clamp(17px, 5%, 40px) clamp(17px, 7%, 50px) !important;
  max-width: none !important;
  border-radius: 6px !important;
  box-shadow: 0 5px 25px rgba(34, 60, 47, 0.25) !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview,
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview *{
  box-sizing: border-box !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-heading {
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-heading h5{
  margin-top: 0 !important;
  margin-bottom: 0 !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field {
  margin-top: 20px !important;
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input {
  width: 100% !important;
  height: 40px !important;
  border-radius: 6px !important;
  border: 2px solid #e9e8e8 !important;
  background-color: #fff;
  outline: none !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input {
  color: #000000 !important;
  font-family: &quot;Montserrat&quot; !important;
  font-size: 14px;
  font-weight: 400;
  line-height: 20px;
  text-align: center;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input::placeholder {
  color: #000000 !important;
  opacity: 1 !important;
}

.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input:-ms-input-placeholder {
  color: #000000 !important;
}

.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input::-ms-input-placeholder {
  color: #000000 !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button {
  margin-top: 10px !important;
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button button {
  width: 100% !important;
  height: 40px !important;
  border: 0 !important;
  border-radius: 6px !important;
  line-height: 0px !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button button:hover {
  cursor: pointer !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .powered-by-line {
  color: #231f20 !important;
  font-family: &quot;Montserrat&quot; !important;
  font-size: 13px !important;
  font-weight: 400 !important;
  line-height: 25px !important;
  text-align: center !important;
  text-decoration: none !important;
  display: flex !important;
  width: 100% !important;
  justify-content: center !important;
  align-items: center !important;
  margin-top: 10px !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .powered-by-line img {
  margin-left: 10px !important;
  height: 1.13em !important;
  max-height: 1.13em !important;
}

  &lt;/style&gt;
  &lt;div attr-a=&quot;&quot; attr-b=&quot;&quot; attr-c=&quot;&quot; attr-d=&quot;&quot; attr-e=&quot;&quot; attr-f=&quot;&quot; class=&quot;followit--follow-form-container&quot;&gt;
    &lt;form action=&quot;https://api.follow.it/subscription-form/SmRpWjEzV1ovU0FOK2R2Y0oxajFBUmtIdW44K1ZiUWZRUm1FZVdpVlRKUHFLSFBKWjE4M1hTSk5yRU5oNmg0RU5zelhuTllNTGU1UmwzRkN0NzZWbnd2YkV5VWFwYnptWFdtekI3eXdwMmhtamtUNit3dlNSblVmRjJxd1JGbjZ8Q05yQWR2eitvYW5HRmNMMzJkbStpM09rNmxXZ0ZjWWhyOERya2VvVXRzZz0=/8&quot; data-v-f93f6730=&quot;&quot; method=&quot;post&quot;&gt;&lt;div class=&quot;form-preview&quot; data-v-f93f6730=&quot;&quot; style=&quot;background-color: white; border-color: rgb(204, 204, 204); border-style: solid; border-width: 1px; position: relative;&quot;&gt;&lt;div class=&quot;preview-heading&quot; data-v-f93f6730=&quot;&quot;&gt;&lt;h5 data-v-f93f6730=&quot;&quot; style=&quot;color: black; font-family: Arial; font-size: 16px; font-weight: bold; text-align: center; text-transform: none;&quot;&gt;Get new posts by email:&lt;/h5&gt;&lt;/div&gt;&lt;div class=&quot;preview-input-field&quot; data-v-f93f6730=&quot;&quot;&gt;&lt;input data-v-f93f6730=&quot;&quot; name=&quot;email&quot; required=&quot;&quot; spellcheck=&quot;false&quot; style=&quot;background-color: white; color: black; font-family: Arial; font-size: 14px; font-weight: normal; text-align: center; text-transform: none;&quot; type=&quot;email&quot; /&gt;&lt;/div&gt;&lt;div class=&quot;preview-submit-button&quot; data-v-f93f6730=&quot;&quot;&gt;&lt;button data-v-f93f6730=&quot;&quot; style=&quot;background-color: black; color: white; font-family: Arial; font-size: 16px; font-weight: bold; text-align: center; text-transform: none;&quot; type=&quot;submit&quot;&gt;Subscribe&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/form&gt;&lt;a class=&quot;powered-by-line&quot; href=&quot;https://follow.it&quot;&gt;Powered by &lt;img alt=&quot;follow.it&quot; height=&quot;17px&quot; src=&quot;https://follow.it/images/colored-logo.svg&quot; /&gt;&lt;/a&gt;
  &lt;/div&gt;&lt;p style=&quot;font-size: 14px; margin: 12px 0px 0px;&quot;&gt;Follow along: &lt;a href=&quot;https://www.youtube.com/@VisualStudioLearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/youtube/FF0000&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;YouTube&quot; /&gt; YouTube&lt;/a&gt; · &lt;a href=&quot;https://www.facebook.com/VisualStudio.Learn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/facebook/1877F2&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;Facebook&quot; /&gt; Facebook&lt;/a&gt; · &lt;a href=&quot;https://www.instagram.com/visualstudiolearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/instagram/E4405F&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;Instagram&quot; /&gt; Instagram&lt;/a&gt; · &lt;a href=&quot;https://www.tiktok.com/@visualstudiolearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/tiktok/000000&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;TikTok&quot; /&gt; TikTok&lt;/a&gt; · &lt;a href=&quot;https://www.visualstudiolearn.com/feeds/posts/default?alt=rss&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/rss/FFA500&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;RSS&quot; /&gt; RSS&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://www.visualstudiolearn.com/feeds/8171362774294879324/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.visualstudiolearn.com/2026/11/monitor-fastapi-app-with-prometheus.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/8171362774294879324'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/8171362774294879324'/><link rel='alternate' type='text/html' href='http://www.visualstudiolearn.com/2026/11/monitor-fastapi-app-with-prometheus.html' title='Monitor a FastAPI App with Prometheus + Grafana (Metrics, Dashboards, Alerts)'/><author><name>The Loonie Lesson</name><uri>http://www.blogger.com/profile/02243798795439866634</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRY4V9cFLhqMxHWBOWCGBiGUqjan14Vda_dy5rz-C41kApceM-u6jAxS2_6AIRZtGkQeyiQolq1gCeSBhpo573lYq37MLmMGgSKDQLasowic65VoitivYCzqrmUDUqxJuqCTyzah8JuALYtW0Iz7rXIvIcj-ovhVpEuEhQJ6yG1FUQ-A/s1600/loonie-lesson-logo-profile.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjzItyMhIH1oPTj57zbkCJM1tHDiUZSsiZKq6N6VLe6uxItIxDmcZWM0n0Bq_QMtJ-0_wRYTWsM9i_HllA8jMWP9dGTYGJwlKmERqHBd5mHTTDSJVsotQqiwnQBQCrzl7bxIjZE-3cTomB-SU6jzCRsnHN49JpB1mcRPEsJBU10B6MpxcU_959-mG0U09s/s72-c/grafana-dashboard-after-fastapi-load-burst.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8861905861585113921.post-5467713991224651713</id><published>2026-11-05T16:18:38.000-05:00</published><updated>2026-11-22T02:50:56.205-05:00</updated><title type='text'>How Do I Backup and Restore a SQL Server Database in SSMS? (Step by Step, 2026)</title><content type='html'>&lt;p&gt;Back up a SQL Server database in SSMS to a &lt;code&gt;.bak&lt;/code&gt; file, break it, then restore it back — that is this whole post, in that order. We take a &lt;strong&gt;full backup&lt;/strong&gt; of the DemoDb database from the &lt;a href=&quot;https://www.visualstudiolearn.com/2026/11/how-do-i-connect-sql-server-to-visual.html&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;previous tutorial&lt;/a&gt;, delete a row and corrupt another one on purpose, then restore the backup &lt;strong&gt;over the damage&lt;/strong&gt; and prove the data is back. After that we restore the same file a second time &lt;strong&gt;as a copy&lt;/strong&gt;, side by side with the original. Every step has its own real screenshot, and the two restore failures we hit on the way — the ones you will hit too — are shown exactly as they appeared, with their fixes.&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;&lt;strong&gt;Versions used in this tutorial&lt;/strong&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;SQL Server &lt;strong&gt;2025 (17.x)&lt;/strong&gt; — Enterprise Developer edition (free for development and testing), build 17.0.1000.7&lt;/li&gt;&lt;li&gt;SQL Server Management Studio &lt;strong&gt;22.11.3&lt;/strong&gt;&lt;/li&gt;&lt;li&gt;Demo database: &lt;strong&gt;DemoDb&lt;/strong&gt; with the table &lt;strong&gt;dbo.DemoCustomers&lt;/strong&gt; — the same database the previous post connected to Visual Studio&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;The screenshots were captured on a Windows Server 2022 machine; the SSMS clicks are identical on Windows 10 and 11 desktops.&lt;/p&gt;&lt;/blockquote&gt;

&lt;h2&gt;Before you start: set up your environment from zero&lt;/h2&gt;

&lt;p&gt;Nothing here assumes a running server. If you worked through the previous post, you already have all of this — skip to the verify step.&lt;/p&gt;

&lt;h3&gt;Step A — Install SQL Server 2025 (Enterprise Developer, free)&lt;/h3&gt;

&lt;ol&gt;&lt;li&gt;On Microsoft&amp;#x27;s SQL Server downloads page, choose &lt;strong&gt;Developer → Enterprise Developer&lt;/strong&gt;. New in SQL Server 2025, there are two free Developer editions: &lt;strong&gt;Enterprise Developer&lt;/strong&gt; (every Enterprise feature) and &lt;strong&gt;Standard Developer&lt;/strong&gt; (every Standard feature). Both are free — no key, no subscription — licensed for development and testing, never production.&lt;/li&gt;&lt;li&gt;Run setup → &lt;strong&gt;Installation → New SQL Server standalone installation&lt;/strong&gt;.&lt;/li&gt;&lt;li&gt;At &lt;strong&gt;Instance Configuration&lt;/strong&gt;, keep the &lt;strong&gt;default instance&lt;/strong&gt; (this tutorial uses a default instance; a named instance works the same, you just type its name when connecting).&lt;/li&gt;&lt;li&gt;At &lt;strong&gt;Database Engine Configuration → Authentication Mode&lt;/strong&gt;, keep &lt;strong&gt;Windows authentication mode&lt;/strong&gt; — it is all this tutorial needs.&lt;/li&gt;&lt;li&gt;Click &lt;strong&gt;Add Current User&lt;/strong&gt; so your Windows account is a SQL Server administrator, then &lt;strong&gt;Install&lt;/strong&gt;.&lt;/li&gt;&lt;/ol&gt;

&lt;h3&gt;Step B — Install SSMS 22&lt;/h3&gt;

&lt;p&gt;Download &lt;strong&gt;SQL Server Management Studio 22&lt;/strong&gt; (this tutorial uses 22.11.3) from Microsoft — a separate free tool — and install it. Backup and restore live in SSMS, not in Visual Studio; administering the server is SSMS&amp;#x27;s job.&lt;/p&gt;

&lt;h3&gt;Step C — Create the backup folder and the demo database&lt;/h3&gt;

&lt;ol&gt;&lt;li&gt;Create a folder for backups: &lt;code&gt;C:\VSL-Backups&lt;/code&gt;. One honest warning that saves confusion later: when a backup runs, it is the &lt;strong&gt;SQL Server service account&lt;/strong&gt; — not your Windows login — that reads and writes this folder. On a default local install that just works; if a backup ever fails with an access-denied error, it is the service account&amp;#x27;s permission that is missing (see Errors #5).&lt;/li&gt;&lt;li&gt;Open SSMS, connect to your instance, open a &lt;strong&gt;New Query&lt;/strong&gt; window and run &lt;code&gt;setup.sql&lt;/code&gt; from the companion code folder (linked at the end):&lt;/li&gt;&lt;/ol&gt;

&lt;pre&gt;&lt;code&gt;CREATE DATABASE DemoDb;
GO
USE DemoDb;
GO
CREATE TABLE dbo.DemoCustomers (
    CustomerId INT IDENTITY(1,1) PRIMARY KEY,
    FullName NVARCHAR(100) NOT NULL,
    City NVARCHAR(100) NOT NULL
);
INSERT INTO dbo.DemoCustomers (FullName, City) VALUES
(N&amp;#x27;Amara&amp;#x27;, N&amp;#x27;Toronto&amp;#x27;),
(N&amp;#x27;Jonas&amp;#x27;, N&amp;#x27;Berlin&amp;#x27;),
(N&amp;#x27;Priya&amp;#x27;, N&amp;#x27;Lagos&amp;#x27;);
SELECT CustomerId, FullName, City FROM dbo.DemoCustomers;&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Step D — Verify everything&lt;/h3&gt;

&lt;p&gt;Two checks. First, the setup script&amp;#x27;s SELECT returns three rows — Amara in Toronto, Jonas in Berlin, Priya in Lagos. That grid is the &lt;em&gt;before&lt;/em&gt; picture; the whole tutorial is about getting back to exactly it.&lt;/p&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiamLjkzwhYoRWc79KGJdNL2HG-wBY23Tchq5PAnSmrob1D9x-nMAz7x6OUEcXCnmEAiRgJA1Lv4GSDOq3XXxLJIpDRmeJyL7eb3X7MLWJvUfI1iuUoWEfCEU1zzr35h3qbxn9bFES_ZusF6eCeujwzGAFsF5lPQ5IGtU--v07DmiEiLNx2QUbFey3Rvag/s1600/s3.jpg&quot; alt=&quot;SSMS query window with the setup script and the results grid showing the three demo rows&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Setup verified: the script that built DemoDb, and the baseline SELECT — 1 Amara Toronto, 2 Jonas Berlin, 3 Priya Lagos. Memorise this grid; it is the restore-proof later.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;p&gt;Second, check the database&amp;#x27;s &lt;strong&gt;recovery model&lt;/strong&gt; — one setting that decides whether a checkbox in Step 13 even exists:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;SELECT name, recovery_model_desc FROM sys.databases WHERE name = &amp;#x27;DemoDb&amp;#x27;;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;On a default install this returns &lt;strong&gt;FULL&lt;/strong&gt;, because new databases inherit their recovery model from the &lt;strong&gt;model&lt;/strong&gt; system database, and model ships as FULL. DemoDb here is FULL. If yours says SIMPLE, everything in this post still works except the tail-log part of Step 13 — under SIMPLE that section is not offered at all, and there is nothing to uncheck. Now the backup.&lt;/p&gt;

&lt;h2&gt;Part A — Take the full backup (Steps 1–7)&lt;/h2&gt;

&lt;h3&gt;Step 1 — Connect, and find DemoDb in the tree&lt;/h3&gt;

&lt;p&gt;Open SSMS and connect to your instance. On this machine the server name is &lt;code&gt;localhost&lt;/code&gt; (a default instance on the same PC), with &lt;strong&gt;Windows Authentication&lt;/strong&gt;. SSMS 22&amp;#x27;s Connect dialog sets &lt;strong&gt;Encrypt&lt;/strong&gt; to Mandatory — and on a local instance you will typically tick &lt;strong&gt;Trust Server Certificate&lt;/strong&gt;, the same self-signed-certificate story as the previous post&amp;#x27;s Visual Studio error.&lt;/p&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgpNRi_th6PSQlf9iNcSYJAo5Dt3XzCd4ZqJLCFzwI2ZgCOCsmyVSlI6kLEAd-cVdBmE1781dV2RCiIop2ieNAlvnhbW5Bvs5MlsLQOSJHqPd_hpV1Z5gmSfAwyCudGe5xOuJX5DAwmBPNzs_48RMi_l4vBavTfgyhH3HmIj2GqR0YZ93dlKPGVIBzxQy0/s1600/s1.jpg&quot; alt=&quot;SSMS Connect dialog with server name localhost, Windows Authentication, Encrypt Mandatory and Trust Server Certificate ticked&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Step 1: SSMS&#39;s Connect dialog — server localhost, Windows Authentication, Encrypt Mandatory, Trust Server Certificate ticked for the local instance.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;p&gt;Expand &lt;strong&gt;Databases&lt;/strong&gt; and find &lt;strong&gt;DemoDb → Tables → dbo.DemoCustomers&lt;/strong&gt;. If the table is there, your setup is real and Part A can start.&lt;/p&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgIZmXYaTtX3e1_n_V11Hv9CM_K-fgYg3BU6KwENydXKDtnmG-nZiTaTPqKiQi_MHlMnkAwPPLwj6YkK1juum99FxQUZFssQZ4fvssRuDrTuvoCHws7aQUPQAK1_ezEImTuRKLFqtco8jz9tM-naVbFK1S2YrPLzA7sZSbuqjl9te4F9v_qb1CpScXIptk/s1600/s2.jpg&quot; alt=&quot;SSMS Object Explorer showing the local instance with DemoDb expanded to Tables and dbo.DemoCustomers&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Step 1, second half: Object Explorer — localhost → Databases → DemoDb → Tables → dbo.DemoCustomers.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;h3&gt;Step 2 — Right-click DemoDb → Tasks → Back Up…&lt;/h3&gt;

&lt;p&gt;Backups start from the database itself. Right-click &lt;strong&gt;DemoDb&lt;/strong&gt;, hover &lt;strong&gt;Tasks&lt;/strong&gt;, and click &lt;strong&gt;Back Up…&lt;/strong&gt; — the first item in the Tasks submenu.&lt;/p&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyjGK_RA1u9zQ0DBU_wv3EnCEIHpj5UojjhTxsujQZH2VVVZGZNuUXDhZm4NUCvPWckFfd9yRwyppGQWKx6c83a2uvg4jjk9CBnmpvxfnume375Lkt5Ef4tHNgJrAciZ5NzBD8Gn6eisxPauGh2BXU5WyUMBEGrmFRcyijvAWOFUJU-mA80hzw-wVo4Xg/s1600/s4.jpg&quot; alt=&quot;Right-click menu on DemoDb in Object Explorer with the Tasks submenu open showing Back Up&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Step 2: right-click DemoDb → Tasks → Back Up… — every SSMS backup starts here.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;h3&gt;Step 3 — Read the Back Up Database dialog once, properly&lt;/h3&gt;

&lt;p&gt;The dialog opens on its &lt;strong&gt;General&lt;/strong&gt; page, and four fields decide everything:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Database:&lt;/strong&gt; DemoDb (the database you right-clicked).&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Recovery model:&lt;/strong&gt; FULL — read-only here; it is the setting you checked in Step D.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Backup type:&lt;/strong&gt; &lt;strong&gt;Full&lt;/strong&gt;. A full backup copies the entire database. The other types — differential and transaction log — only make sense standing on a full backup, which is why we take this one first.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Backup component:&lt;/strong&gt; &lt;strong&gt;Database&lt;/strong&gt; (whole database, not selected files).&lt;/li&gt;&lt;/ul&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgq2VVAXi3K4ESreV3fCidUrY8nnXFUGu3qbnswP4mRXoUnwu9bjXGBiAfsMk7teo0mgT28MBIKjQxbO65ueiPmDf07JlZ0YlhxHnRGJLfI4RRBh3Gv1kV4NoQzTXk4wf5Zttl6prRnRrZSXiApAygLoz1OQZPUvL6yxlixnublRqcgbp8sUKEdNXe51kU/s1600/s5.jpg&quot; alt=&quot;Back Up Database dialog General page showing DemoDb, recovery model FULL, backup type Full and the destination file&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Step 3: the Back Up Database dialog — DemoDb, FULL recovery model, backup type Full, component Database.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;h3&gt;Step 4 — Set the destination: your own folder, your own file name&lt;/h3&gt;

&lt;p&gt;Under &lt;strong&gt;Destination&lt;/strong&gt;, SSMS pre-fills a backup file inside the instance&amp;#x27;s own Backup folder. Remove it. Click &lt;strong&gt;Add…&lt;/strong&gt;, and in &lt;strong&gt;Select Backup Destination&lt;/strong&gt; type the full path of &lt;em&gt;your&lt;/em&gt; file:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;C:\VSL-Backups\DemoDb-full-2026-11-05.bak&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;.bak&lt;/code&gt; extension is a convention, not a rule the engine enforces — but every tool and every colleague expects it, and the dialog itself tells you to use it. A dated file name pays off the first time a folder holds more than one backup.&lt;/p&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjzS7tQrtOognoQ1ME59N8C_jJzxMzUArRHrLMDD5gvx8aO7rI1loTOJDwVUX7OMnrSgkc7UQrjpEShSwLEQZqKBSuQU4ttjaOPtmKYPq31jTcpojQEwUM5bxeQOWq5oQZdSoisrBdTqNSw7EUWN_ezn-XEogMa-YgtPMpo2m6bnyrUvxyUNFPbO-Mj3Ag/s1600/s6.jpg&quot; alt=&quot;Select Backup Destination dialog with the file name C:\VSL-Backups\DemoDb-full-2026-11-05.bak typed in&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Step 4: Select Backup Destination — our file, in our folder, with .bak at the end.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;h3&gt;Step 5 — Optional but worth it once: press Script and read what the dialog really does&lt;/h3&gt;

&lt;p&gt;Before clicking OK, try the &lt;strong&gt;Script&lt;/strong&gt; button at the top of the dialog (Script → Script Action to New Query Window). SSMS writes the exact T-SQL your clicks amount to, into a query window:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;BACKUP DATABASE [DemoDb] TO DISK = N&amp;#x27;C:\VSL-Backups\DemoDb-full-2026-11-05.bak&amp;#x27;
WITH NOFORMAT, NOINIT, NAME = N&amp;#x27;DemoDb-Full Database Backup&amp;#x27;, SKIP, NOREWIND, NOUNLOAD, STATS = 10&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Two options in that statement matter for later. &lt;strong&gt;NOINIT&lt;/strong&gt; means &lt;em&gt;append&lt;/em&gt;: if the file already exists, the new backup is added to it as another backup &lt;em&gt;set&lt;/em&gt; rather than replacing it. The opposite choice — starting a fresh media set — is the &lt;strong&gt;Overwrite all existing backup sets&lt;/strong&gt; option on the dialog&amp;#x27;s Media Options page (FORMAT in T-SQL). Appending is the default and it surprises people at restore time (Errors #8). The companion folder&amp;#x27;s &lt;code&gt;01-backup-full.sql&lt;/code&gt; is this statement, plus an optional &lt;code&gt;RESTORE VERIFYONLY&lt;/code&gt; line that checks the finished backup set is complete and readable.&lt;/p&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjX2umz1-VQU6Ih0nZK7OnHtuLByNTBYYALVZazcHifaiQlE9WejZMYJMpJrayXY7yy9VgVB2s7fbXfQ-Axds5esXV6tq-_JnZUBCCJOYxW7K7nJuKP9pJG1d-xzKB6zVWNl4FuM563PzxviCjl2ZxN0TG9189kvhFxNjvd3c2eMZRz2Hptf34zzbCXD_w/s1600/s7.jpg&quot; alt=&quot;Query window holding the BACKUP DATABASE statement generated by the Script button&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Step 5: the statement the Script button generated — the dialog, translated into T-SQL. NOINIT (append) is the default hiding in plain sight.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;h3&gt;Step 6 — Click OK and read the confirmation&lt;/h3&gt;

&lt;p&gt;Close the extra query window if you opened one, click &lt;strong&gt;OK&lt;/strong&gt; on the Back Up Database dialog, and wait for the confirmation:&lt;/p&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUMYHiSgQ8kkL-sOa_-Q1azIi4XZudoQqGheAfCpw9ekJcRId3CleqgSkmxtRvgAfjuhTXT89WEXY3HnGoHSH233w75ci0sWwqZ7pTmmiTGjiHAOTb7l43H46KfzbsdfoQSjSoofvhF8QX8eA4r83NpXBdLikwbN19EWR2X01VeGtkBC98DIzulY8hq3E/s1600/s8.jpg&quot; alt=&quot;The backup of database DemoDb completed successfully confirmation dialog over the Back Up Database dialog&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Step 6: “The backup of database &#39;DemoDb&#39; completed successfully.” — the backup exists.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;h3&gt;Step 7 — Prove the file exists, on disk, with a size&lt;/h3&gt;

&lt;p&gt;Open &lt;code&gt;C:\VSL-Backups&lt;/code&gt; in File Explorer. The &lt;code&gt;.bak&lt;/code&gt; file is there — on this machine &lt;strong&gt;3,636 KB&lt;/strong&gt;, written at the minute the dialog confirmed. A backup you have not seen on disk is a rumour; this one is a file.&lt;/p&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEidp8Fj2J3D0I1jPWQrw9viSdJPE70JI0XrSJM0nJwY-fo8QjdjLGvS0rFMt39iV71coC4OQZvSDUcPr_0ABbTROgLbbMgjNJaz6j1Qv_r74wTcughBEucF0dk3R-b8UNycrMBnT3fjlQyp7D3eq9nJwM0_rSx-3CMSKA7quqwxe3iE9vb9mGgEatJ3RX0/s1600/s9.jpg&quot; alt=&quot;File Explorer showing DemoDb-full-2026-11-05.bak in C:\VSL-Backups with its size of 3,636 KB&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Step 7: C:\VSL-Backups on disk — DemoDb-full-2026-11-05.bak, BAK File, 3,636 KB.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;h2&gt;Part B — Break the database, on purpose (Step 8)&lt;/h2&gt;

&lt;h3&gt;Step 8 — One wrong UPDATE, one DELETE&lt;/h3&gt;

&lt;p&gt;A restore you have never watched fix real damage is a restore you merely believe in. So we do the damage deliberately — the two accidents that happen in production every week: a value overwritten with a wrong one, and a row deleted outright. In a new query window on DemoDb, run &lt;code&gt;02-break-it.sql&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;USE DemoDb;
UPDATE dbo.DemoCustomers SET City = N&amp;#x27;Munich&amp;#x27; WHERE FullName = N&amp;#x27;Jonas&amp;#x27;;
DELETE FROM dbo.DemoCustomers WHERE FullName = N&amp;#x27;Priya&amp;#x27;;
SELECT CustomerId, FullName, City FROM dbo.DemoCustomers;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The SELECT now returns exactly &lt;strong&gt;2 rows&lt;/strong&gt;: Amara untouched, and Jonas — in &lt;strong&gt;Munich&lt;/strong&gt;, a city he was never in. Priya&amp;#x27;s row is gone. Keep this query window open; in about two steps it becomes the villain of the piece.&lt;/p&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjXXkMZrpORLMC--kUUgqoaVgo_kCXbWkt_rBwLtArDtTrtRXhjYeJqTxouIe3wsTg4nS_wcEP7Zi7mRGG54tIBfDX0MrmFw5L5tBoMblL2h01X7Ykaru2gGdQGeB6QupgLqfkl2eeiaiCVtfgnKWIJTZkg11CG-3kBJxa3J67zP-LNT5rQssdy-rzGxOE/s1600/s10.jpg&quot; alt=&quot;The damage script and its results grid showing two rows with Jonas in Munich&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Step 8: the damage, done and proven — 2 rows, Jonas in Munich, Priya gone.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;h2&gt;Part C — Restore over the damage (Steps 9–15)&lt;/h2&gt;

&lt;h3&gt;Step 9 — Start from the Databases node, not from the database&lt;/h3&gt;

&lt;p&gt;Here is the classic wrong turn, made by almost everyone the first time: restore does &lt;strong&gt;not&lt;/strong&gt; start by right-clicking the database. Right-click the &lt;strong&gt;Databases&lt;/strong&gt; node — the folder above all your databases — and choose &lt;strong&gt;Restore Database…&lt;/strong&gt;. Starting at the node is what allows restoring over an existing database &lt;em&gt;and&lt;/em&gt; restoring under a brand-new name (Part D) from the same dialog.&lt;/p&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiaRqUYyyX8BtJNj7lHERWGAt2vShSGJ8QI5LnLXnFKhhvoBLVO8s_UobxKv6DsynQmI33PhLPSAwaypA4pe5jonyyvSUayyHvIZ_fiNPW9HNIaf-346OFy9qIJMYbpyQGjt4bn2aC-rmfahFmZdHRll6CpOaU0g9AHnJnMFW1KT-YpvikPXO1HzD1p4bI/s1600/s11.jpg&quot; alt=&quot;Right-click menu on the Databases node in Object Explorer showing Restore Database&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Step 9: right-click the Databases node → Restore Database… — not the database itself.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;h3&gt;Step 10 — Source: Device → add your .bak file&lt;/h3&gt;

&lt;p&gt;On the &lt;strong&gt;General&lt;/strong&gt; page, under &lt;strong&gt;Source&lt;/strong&gt;, select &lt;strong&gt;Device&lt;/strong&gt; (not Database), click the &lt;strong&gt;…&lt;/strong&gt; button, and in &lt;strong&gt;Select backup devices&lt;/strong&gt; click &lt;strong&gt;Add&lt;/strong&gt;. Browse to &lt;code&gt;C:\VSL-Backups\DemoDb-full-2026-11-05.bak&lt;/code&gt; and OK your way back. The devices dialog now lists your file as the backup media — the restore will read its sets from here.&lt;/p&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2zEFmdJwHC7gTYoOp8mKGm6BFv0uFpJnRF14vMNecdaL68Jqp_GJFBsMMm023HmbAn7-0MqOrq8sv_wU6xsIlGgF-_DHXad1w4nTO8RsPV2PTC-k1r5NcHdDa9H1Z9qvis3gXRlmDTzkfkNxwGJaMGAZ4qIfkorjIJ9XMBiNtGLxv58dHxOXiHwRLjFo/s1600/s12.jpg&quot; alt=&quot;Select backup devices dialog with C:\VSL-Backups\DemoDb-full-2026-11-05.bak in the backup media list&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Step 10: Select backup devices — the media list holds exactly our .bak file.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;h3&gt;Step 11 — Check the destination and the backup set&lt;/h3&gt;

&lt;p&gt;Back on the General page, SSMS has read the file and filled in the plan:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Source Database:&lt;/strong&gt; DemoDb (read from the backup itself).&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Destination Database:&lt;/strong&gt; &lt;strong&gt;DemoDb&lt;/strong&gt; — the existing, damaged database. This one field is the whole difference between restoring &lt;em&gt;over&lt;/em&gt; a database and restoring &lt;em&gt;as a copy&lt;/em&gt;: type a different name here and Part D happens instead.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Backup sets to restore:&lt;/strong&gt; one row — &lt;em&gt;DemoDb-Full Database Backup&lt;/em&gt;, type Full — ticked. If this grid ever shows more rows than you expect, stop and read Errors #8 before clicking anything.&lt;/li&gt;&lt;/ul&gt;

&lt;p&gt;Note the yellow banner at the top of the dialog: &lt;em&gt;“A tail-log backup of the source database will be taken. View this setting on the Options page.”&lt;/em&gt; SSMS is warning you about Step 13 already. Good — we will deal with it there, deliberately.&lt;/p&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxaIlybqW9smwbo11XTqWu9ewSbDlzNwZkHG-GN7GcbIrfuPpkczEIgWoHpD_7B201U-3hkvuOOk1YPM26Er32W9bD15ChST8tWHcM0rt4tfoI57mS3A_VSqUTy8hEs4lVFX9sJ-IFbvz8T0Dqi8Mu677IZ2bQkHpsbi0ztJ0kVczayTIOmG63TI281nw/s1600/s13.jpg&quot; alt=&quot;Restore Database dialog General page with the device file, destination DemoDb and the full backup set ticked&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Step 11: Restore Database, General — source file, destination DemoDb, the one full backup set ticked, and the tail-log banner up top.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;h3&gt;Step 12 — Files page: where the restored files will live&lt;/h3&gt;

&lt;p&gt;Open the &lt;strong&gt;Files&lt;/strong&gt; page and read the grid, even if you change nothing. Each row is one file inside the backup: its &lt;strong&gt;logical name&lt;/strong&gt; (&lt;code&gt;DemoDb&lt;/code&gt; for the data file, &lt;code&gt;DemoDb_log&lt;/code&gt; for the log), where it originally lived, and &lt;strong&gt;Restore As&lt;/strong&gt; — where it will be written. For a restore over itself, Restore As points at the database&amp;#x27;s current files, which is exactly right. This grid is also the T-SQL &lt;code&gt;WITH MOVE&lt;/code&gt; clause wearing a dialog&amp;#x27;s clothes — Part D edits it, and Errors #4 is what happens when the paths point somewhere that does not exist.&lt;/p&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgE4q_dAf6ZfH64Ns_gaNsz2J-N6p99_7ycRKzzJGWtjdKdWDe-A4ZInX7muZtSOKs0xFI6JPVqOp2w8NRX5s564dWwFrK3OFVtrS5aZRBkzrWIOSDyOukq8E6AwvjXmqr42YDlz5MnUx7vX-HjREC8oOOSahdylwCVG0FZI0RrZpOyGaZ0uH3blfesv8w/s1600/s14.jpg&quot; alt=&quot;Restore Database Files page showing the logical file names and the Restore As paths&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Step 12: the Files page — logical names DemoDb and DemoDb_log, and the Restore As paths the restore will write to.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;h3&gt;Step 13 — Options page: the three switches this whole tutorial turns on&lt;/h3&gt;

&lt;p&gt;Open the &lt;strong&gt;Options&lt;/strong&gt; page. As the dialog first opens it — and this is verified on the screenshots from this tutorial&amp;#x27;s own session — the page arrives like this: &lt;strong&gt;Overwrite&lt;/strong&gt; unticked, &lt;strong&gt;Close existing connections&lt;/strong&gt; unticked, and &lt;strong&gt;Take tail-log backup before restore&lt;/strong&gt; &lt;em&gt;ticked&lt;/em&gt;. All three defaults are wrong for what we are doing. Set them deliberately:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Overwrite the existing database (WITH REPLACE): TICK it.&lt;/strong&gt; You are knowingly replacing DemoDb&amp;#x27;s current contents with the backup&amp;#x27;s. The T-SQL name tells the truth: REPLACE bypasses SQL Server&amp;#x27;s built-in safety checks, so it should only ever be ticked when overwriting is the actual intention — here, it is.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Close existing connections to destination database: TICK it.&lt;/strong&gt; A restore needs the database to itself. Any open connection — an application, another SSMS window, &lt;em&gt;the Step 8 window still sitting on DemoDb&lt;/em&gt; — blocks it. Ticking this makes SSMS flip the database to single-user, restore, and flip it back to multi-user for you.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Take tail-log backup before restore: UNTICK it.&lt;/strong&gt; A &lt;em&gt;tail-log backup&lt;/em&gt; captures the log records written since the last backup — the tool for rolling a database &lt;em&gt;forward&lt;/em&gt; to the latest possible moment before a restore. We are doing the opposite: travelling &lt;em&gt;back&lt;/em&gt; to the backup, throwing the later changes (our damage) away. A tail-log backup here is not just pointless — on a restore under a new name it can flip the &lt;em&gt;original&lt;/em&gt; database into the Restoring state (Errors #3). Leave &lt;strong&gt;Recovery state&lt;/strong&gt; at its default, &lt;strong&gt;RESTORE WITH RECOVERY&lt;/strong&gt; — it brings the database back online, ready to use, when the restore finishes.&lt;/li&gt;&lt;/ul&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg4CDDbdOOXMLuZ_qTYjWxQN6lBGA-HYiwftGhoCvREImTCpIOd2wHQVU25OFZeMLVMY8URHQhXl9MVS4dUbtl7lc_OlnOf_bXX6BHQWx0WC8iHkdTzkB9J3nAS8UwSIKD-Kp-blZ2TWYxzzTCG3gUDkGAv4L1pYVupHh5GIAhrA9jX6foFn1zWacfR9SU/s1600/s15b.jpg&quot; alt=&quot;Restore Database Options page as it first opens: Overwrite unticked, Close existing connections unticked, Take tail-log backup ticked, recovery state RESTORE WITH RECOVERY&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Step 13, as the Options page first opens — Overwrite ☐, Close existing connections ☐, Take tail-log backup ☑. All three defaults are wrong for this restore; the next screenshot shows them set.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjee8WGjBV8CdwSCehjha293_gpRJFI0FFW8jS_v0tmhA2dgCtt5gKUsBk0ZvGMliUdhNBMIJ5KSYh7PPEhc8EJFg2-lJmg_2WVKkujyFRAHSZ54d93NJvS8cU4g5iOdNE5ISB-BBGUnR6vpqRrVMrx10U_wGogL2tNCYHhYX1buKtbvhn6Pf39m-Rv3WQ/s1600/s15.jpg&quot; alt=&quot;Restore Database Options page with Overwrite ticked, Close existing connections ticked, tail-log backup unticked and recovery state RESTORE WITH RECOVERY&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Step 13: the Options page as submitted — Overwrite ✔, Close existing connections ✔, tail-log backup ✘, RESTORE WITH RECOVERY. The restore is already running at 100% in this frame.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;h3&gt;What actually went wrong twice, before this restore worked&lt;/h3&gt;

&lt;p&gt;This tutorial&amp;#x27;s own session hit the restore failure twice before the clean run above — same error both times, two different causes. Both dialogs are real, captured live, and they are the two failures you are most likely to meet:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Attempt 1 — Overwrite unticked.&lt;/strong&gt; We expected the textbook safety error here (error 3154 — see Errors #1). We never reached it. The restore failed earlier, with &lt;strong&gt;“Exclusive access could not be obtained because the database is in use”&lt;/strong&gt; — because the Step 8 query window was still connected to DemoDb. Lesson: the in-use check fires &lt;em&gt;before&lt;/em&gt; the overwrite safety check. An open window beats a checkbox.&lt;/p&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghGKFU4lKSqFjFCrVCwJ_b9DUfaI_oZAtyQ_yi5lbRjyorNKMOFdvP5nnUg6cJ7Kyzf1JCa5zXEVdgyZFebTzEVaNTJmohnX_Jw6VPCz2dj7MtWpVaUgEwl7y0k_tMkWrhqR4edFjPu5W0dWmyjUmffun0g-DGhPG_XZ2SiyvoI3sJ160vXPCnnTWNDJ0/s1600/x1.jpg&quot; alt=&quot;Restore failure dialog reading Exclusive access could not be obtained because the database is in use, with Overwrite unticked behind it&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Attempt 1 (real): Overwrite unticked — and the error is still the in-use one, because the damage window was connected. “Exclusive access could not be obtained because the database is in use.”&lt;/figcaption&gt;&lt;/figure&gt;

&lt;p&gt;&lt;strong&gt;Attempt 2 — Overwrite ticked, Close existing connections unticked.&lt;/strong&gt; Same error, verbatim. With Overwrite now on, the only remaining blocker was the connection itself. (Notice, in the full session, that the tail-log backup was also still ticked at this point — three wrong switches, peeled off one at a time.) The fix for both attempts is the Step 13 setting above: &lt;strong&gt;Close existing connections&lt;/strong&gt;. Switching your own query window&amp;#x27;s context to &lt;code&gt;master&lt;/code&gt; first — &lt;code&gt;USE master;&lt;/code&gt; — helps too; the session did both before the clean run.&lt;/p&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiRjcTEy2H10HXLQIT_TvwKe8jpfZkDTScgBo6DFqzX2EoOdqgIRNH_JSL2GLBUxQGuDVVnt3jwnUfKRbjg-sOUEIONYHrf2fx1VdtP-pQrAe6fvck8Nk2Vb8sAkszJycFKsId_R9X2s84lbjbxRilsHQw3JgmEfdXP1X9qIGM9vCWkm8Bgt7xB6uGP-NM/s1600/x2.jpg&quot; alt=&quot;Restore failure dialog reading Exclusive access could not be obtained because the database is in use, with Overwrite ticked behind it&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Attempt 2 (real): Overwrite ticked, Close existing connections still unticked — same error. The checkbox, not the error text, is the difference.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;h3&gt;Step 14 — Click OK: restored successfully&lt;/h3&gt;

&lt;p&gt;With the three switches set, click &lt;strong&gt;OK&lt;/strong&gt;. The restore runs (a small database is seconds) and SSMS confirms:&lt;/p&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjt9jezhzX3jG8R73teQMunCkgr6SPLQ5xvZ8HHgTq9vttts2hBKGPWh4AHpD787pC6FrEvnamiyYNEEoVhgeMo58dimi2i4Qgoy8HkRjBuo-gD8eX0ERlJgkhh0nb3JdY6MXSFBouR-ZRylpG96WFBqh_4ilaNGc3XCUjdPz6m4jyMcxRxqmkvkzfP5ZA/s1600/s16.jpg&quot; alt=&quot;Database DemoDb restored successfully dialog over the Restore Database Options page&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Step 14: “Database &#39;DemoDb&#39; restored successfully.”&lt;/figcaption&gt;&lt;/figure&gt;

&lt;h3&gt;Step 15 — The restore-proof: three rows, and Jonas back in Berlin&lt;/h3&gt;

&lt;p&gt;Never trust the success dialog alone — verify the data. Run &lt;code&gt;05-verify.sql&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;USE DemoDb;
SELECT CustomerId, FullName, City FROM dbo.DemoCustomers;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Three rows.&lt;/strong&gt; And check the &lt;em&gt;value&lt;/em&gt;, not just the count: &lt;strong&gt;Jonas is back in Berlin&lt;/strong&gt;, and Priya&amp;#x27;s row exists again. The restore did not just bring back a row count — it rolled the database back to the exact moment the backup was taken, undoing an UPDATE and a DELETE in one move. If your grid ever shows three rows but Jonas still in Munich, you restored the wrong backup set — Errors #8.&lt;/p&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZGVD3XTZHQfuoTqCe_7UYMxkYNLTUYAeeHDTh37oB_LOjgCHMi2_HZEPK3XoVohIiire-tt5c0nzLlfcnQqMvRoGJajwtBRg77MNBGOk_KHNmhMtwmQYZZQeZUPJ153i_7TMPGWzoW0-zbQRmGpkSSJZrUMuDprAHd00385Wtgefs-ZqLsCNGExFZJJs/s1600/s17.jpg&quot; alt=&quot;Verify query results showing three rows with Jonas back in Berlin and Priya restored&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Step 15: the restore-proof — 3 rows, Jonas in Berlin, Priya back. The exact grid from Step D.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;h2&gt;Part D — Restore the same backup as a copy (Steps 16–18)&lt;/h2&gt;

&lt;p&gt;Same file, different destination: a second, independent database next to the original. This is how you inspect a backup without touching production data, hand a developer a realistic copy, or park a before-snapshot next to a risky change.&lt;/p&gt;

&lt;h3&gt;Step 16 — Repeat Steps 9–11, but type a new name: DemoDbCopy&lt;/h3&gt;

&lt;p&gt;Right-click &lt;strong&gt;Databases → Restore Database…&lt;/strong&gt;, Source &lt;strong&gt;Device&lt;/strong&gt;, add the same &lt;code&gt;.bak&lt;/code&gt; — then on the General page, change &lt;strong&gt;Destination Database&lt;/strong&gt; from DemoDb to &lt;strong&gt;DemoDbCopy&lt;/strong&gt;. Typing a name that does not exist yet is the entire trick; watch the dialog&amp;#x27;s title change to match.&lt;/p&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJEp8wqLhgRNl42f8p5zxpBPnvLvuYjqmJPb-aodUEtVtdMWlX-Uy5FKhYAVRkQTEloNvZkRxo92MzrB9qsHehyNYzCFTQvtwZpqd1bTMRtk3_n9n1Ph_mRHnsQ9bEBmZpNLaIEnvrPNA1T426-5aDU9UbirD9WBE-iZXFKeFUShrulnZi1BuzH1ZaCGQ/s1600/s18.jpg&quot; alt=&quot;Restore Database dialog with DemoDbCopy typed as the destination database&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Step 16: same backup, new destination — DemoDbCopy. The dialog title follows the name.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;h3&gt;Step 17 — Files page: rename the Restore As paths&lt;/h3&gt;

&lt;p&gt;Open the &lt;strong&gt;Files&lt;/strong&gt; page before clicking anything else. The backup&amp;#x27;s files want to land on the original database&amp;#x27;s file names — &lt;code&gt;DemoDb.mdf&lt;/code&gt; and &lt;code&gt;DemoDb_log.ldf&lt;/code&gt;, which are in use. Rename &lt;strong&gt;Restore As&lt;/strong&gt; for both rows, keeping the same folder:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;...\MSSQL\DATA\DemoDbCopy.mdf
...\MSSQL\DATA\DemoDbCopy_log.ldf&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then open the &lt;strong&gt;Options&lt;/strong&gt; page and &lt;strong&gt;untick Take tail-log backup&lt;/strong&gt; here as well — for a restore under a new name this is the dangerous one (Errors #3). Overwrite stays unticked: DemoDbCopy does not exist, so there is nothing to overwrite, and SSMS does not ask for it.&lt;/p&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHGpRLMQiofqI9rkvWV4i2mtZjrdu-Zwgh7ve_QuVCgggdd83zq4-z2c-HuY-YPdWnP9NPBHLbFbUas2QV0zIsMZ34ARVQ5jEzKzGkn5YzFGUDQcNT9nE8nVDtgQGlIMylVqbfxcr7dggktG-VVZDxr3ipdbugPAAoesVmgn135zv-acfhG6Y5jeigu84/s1600/s19.jpg&quot; alt=&quot;Restore Database Files page for DemoDbCopy with the Restore As paths renamed to DemoDbCopy.mdf and DemoDbCopy_log.ldf&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Step 17: the Files page for the copy — Restore As renamed to DemoDbCopy.mdf and DemoDbCopy_log.ldf in the same DATA folder.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;h3&gt;Step 18 — Click OK, then look at the tree&lt;/h3&gt;

&lt;p&gt;Click &lt;strong&gt;OK&lt;/strong&gt;. When it completes, refresh &lt;strong&gt;Databases&lt;/strong&gt; in Object Explorer: &lt;strong&gt;DemoDb and DemoDbCopy&lt;/strong&gt;, side by side. Open a query window on &lt;strong&gt;DemoDbCopy&lt;/strong&gt; and run the same SELECT: the same three rows, in the copy — while the original was never touched by this second restore.&lt;/p&gt;

&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg74DWybsfNEIZ9J_uvX_fE7VLzxj9H2bUp2zst1ydb5Z1Svh9vTm_3Zp3kQoYpvWAx_l-VfxrViErvqoHm0v2GXTCFUljf9zOQeAcLU6ciQeGSavdOZDEh9vAQy2gC7I85NCbN6gh4ltFR47uvwydm6kvYcDQhNOeM6ONxpJquFso7Y6QWebrggZQsU5Q/s1600/s20.jpg&quot; alt=&quot;Object Explorer showing DemoDb and DemoDbCopy side by side, with the copy&#39;s three rows in the results grid&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Step 18: both databases in the tree, and the copy&#39;s SELECT returns the same three rows.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;p&gt;The T-SQL form of Part D (in &lt;code&gt;04-restore-as-copy.sql&lt;/code&gt;) shows what the Files page edits became — note the logical names, straight from Step 12&amp;#x27;s grid:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;RESTORE DATABASE [DemoDbCopy]
FROM DISK = N&amp;#x27;C:\VSL-Backups\DemoDb-full-2026-11-05.bak&amp;#x27;
WITH
    MOVE &amp;#x27;DemoDb&amp;#x27;     TO &amp;#x27;C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\DATA\DemoDbCopy.mdf&amp;#x27;,
    MOVE &amp;#x27;DemoDb_log&amp;#x27; TO &amp;#x27;C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\DATA\DemoDbCopy_log.ldf&amp;#x27;,
    RECOVERY;&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Backup types and recovery models, in one minute&lt;/h2&gt;

&lt;p&gt;Everything above was one &lt;strong&gt;full backup&lt;/strong&gt; — the whole database in one file, the foundation every other backup type stands on. The other two, now that a full backup exists:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Differential backup&lt;/strong&gt; — only the changes since the last full backup. Restoring means the latest full &lt;em&gt;plus&lt;/em&gt; the single latest differential — never a chain of them.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Transaction log backup&lt;/strong&gt; — the log records since the last log backup; a chain of them is what makes point-in-time restore possible. Available only under the &lt;strong&gt;FULL&lt;/strong&gt; or &lt;strong&gt;BULK_LOGGED&lt;/strong&gt; recovery models — under &lt;strong&gt;SIMPLE&lt;/strong&gt;, the log is truncated automatically and log backups do not exist, which is also why the tail-log section vanishes from the Options page.&lt;/li&gt;&lt;/ul&gt;

&lt;p&gt;Choosing a recovery model is choosing how much work you can afford to lose: SIMPLE trades recoverability for zero log management; FULL keeps every log record so you can restore to a point in time, at the price of taking log backups so the log does not grow forever. And the old DBA saying that outranks all of this — &lt;em&gt;you don&amp;#x27;t have a backup until you&amp;#x27;ve restored it&lt;/em&gt; — which, if you followed along, you have now done twice.&lt;/p&gt;

&lt;h2&gt;Errors you may hit&lt;/h2&gt;

&lt;h3&gt;1. Error 3154 — “The backup set holds a backup of a database other than the existing &amp;#x27;DemoDb&amp;#x27; database”&lt;/h3&gt;

&lt;p&gt;The safety check doing its job: you are restoring over an existing database &lt;em&gt;without&lt;/em&gt; Overwrite (WITH REPLACE), and SQL Server refuses to silently replace one database with another&amp;#x27;s backup. The full dialog form, from Microsoft&amp;#x27;s own SSMS documentation example, reads: &lt;em&gt;“System.Data.SqlClient.SqlError: The backup set holds a backup of a database other than the existing &amp;#x27;Sales&amp;#x27; database. (Microsoft.SqlServer.SmoExtended)”&lt;/em&gt; — with your database&amp;#x27;s name in place of Sales. &lt;strong&gt;Fix:&lt;/strong&gt; if overwriting is genuinely the intention, tick &lt;strong&gt;Overwrite the existing database (WITH REPLACE)&lt;/strong&gt; on the Options page. If it is not the intention, stop — this error just saved you. (One sequencing truth from this tutorial&amp;#x27;s session: if anything is still &lt;em&gt;connected&lt;/em&gt; to the database, you will meet Error #2 first — the in-use check fires earlier.)&lt;/p&gt;

&lt;h3&gt;2. “Exclusive access could not be obtained because the database is in use”&lt;/h3&gt;

&lt;p&gt;The error this tutorial actually hit — twice, in the Part C callout above. A restore needs exclusive access; &lt;em&gt;something&lt;/em&gt; is connected: an application, a service, or your own SSMS query window still pointed at the database (guilty). &lt;strong&gt;Fix:&lt;/strong&gt; tick &lt;strong&gt;Close existing connections to destination database&lt;/strong&gt; on the Options page — SSMS flips the database to single-user, restores, and returns it to multi-user — and switch your own query windows to &lt;code&gt;master&lt;/code&gt; (&lt;code&gt;USE master;&lt;/code&gt;) before restoring. The T-SQL equivalent of the checkbox is in &lt;code&gt;03-restore-overwrite.sql&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;3. The tail-log trap — two faces&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Face one:&lt;/strong&gt; you are restoring to go &lt;em&gt;back&lt;/em&gt; to a backup (our Part C), and &lt;strong&gt;Take tail-log backup before restore&lt;/strong&gt; is ticked — as it is by default under the FULL recovery model. The tail-log backup preserves the very changes you are trying to undo, and if its file cannot be written, the restore fails before it starts. Untick it when travelling back. &lt;strong&gt;Face two (the dangerous one):&lt;/strong&gt; restoring under a &lt;em&gt;new name&lt;/em&gt; with the tail-log option left on can leave the &lt;em&gt;original&lt;/em&gt; database stuck in the &lt;strong&gt;Restoring&lt;/strong&gt; state — Microsoft&amp;#x27;s restore documentation walks through exactly this scenario, including the “the tail of the log for the database has not been backed up” error that accompanies it. Untick it in Part D as well. The rule that covers both: a tail-log backup belongs to rolling &lt;em&gt;forward&lt;/em&gt; before a restore, never to going back or sideways.&lt;/p&gt;

&lt;h3&gt;4. Restore As paths point at a folder that does not exist&lt;/h3&gt;

&lt;p&gt;The Files page&amp;#x27;s &lt;strong&gt;Restore As&lt;/strong&gt; values come from where the files lived on the machine that &lt;em&gt;made&lt;/em&gt; the backup. Restore a backup from another server — or rename carelessly in Part D — and the paths can point at folders that are not on this machine, and the restore fails on file creation. &lt;strong&gt;Fix:&lt;/strong&gt; correct the paths on the Files page (or use &lt;code&gt;WITH MOVE&lt;/code&gt; in T-SQL, exactly as &lt;code&gt;04-restore-as-copy.sql&lt;/code&gt; does). Also make sure the new file names do not collide with an existing database&amp;#x27;s files.&lt;/p&gt;

&lt;h3&gt;5. Access denied on the backup folder&lt;/h3&gt;

&lt;p&gt;Backup and restore run as the &lt;strong&gt;SQL Server service account&lt;/strong&gt;, not as you. A folder your login can open can still be closed to the service — network shares are the classic case — and the failure only appears when the operation actually runs. &lt;strong&gt;Fix:&lt;/strong&gt; grant the service account read/write on the backup folder, or put the file somewhere it already has access (the instance&amp;#x27;s own Backup folder always qualifies).&lt;/p&gt;

&lt;h3&gt;6. “The backup was taken on a newer version” — restoring down-level fails&lt;/h3&gt;

&lt;p&gt;Backups only travel one way: a backup taken on &lt;strong&gt;SQL Server 2025&lt;/strong&gt; cannot be restored onto &lt;strong&gt;SQL Server 2022&lt;/strong&gt; or older — the restore fails on the version mismatch. Older-to-newer works (restoring a 2022 backup onto 2025 succeeds and upgrades the database&amp;#x27;s internal format as part of the restore). Plan migrations accordingly: you can move up with backup/restore, never down.&lt;/p&gt;

&lt;h3&gt;7. Differential or Log backup type is unavailable&lt;/h3&gt;

&lt;p&gt;Two ordinary causes. &lt;strong&gt;No full backup exists yet&lt;/strong&gt; — differentials and log backups stand on a full backup; take one first (Part A). Or the database is under the &lt;strong&gt;SIMPLE&lt;/strong&gt; recovery model, where log backups do not exist at all. Check the model with the Step D query; if the work needs log backups, the database needs FULL (and a log-backup routine to go with it).&lt;/p&gt;

&lt;h3&gt;8. The restore grid shows the wrong backup set&lt;/h3&gt;

&lt;p&gt;Remember Step 5&amp;#x27;s &lt;strong&gt;NOINIT&lt;/strong&gt;: backing up to the same &lt;code&gt;.bak&lt;/code&gt; file again &lt;em&gt;appends&lt;/em&gt; a second backup set instead of replacing the file. Restore from that file later and the &lt;strong&gt;Backup sets to restore&lt;/strong&gt; grid lists &lt;em&gt;every&lt;/em&gt; set in the file — pick by the backup date and position columns, and make sure the tick is on the set you mean. Restoring last month&amp;#x27;s set over today&amp;#x27;s database is how a routine restore becomes an incident. (The devices dialog&amp;#x27;s &lt;strong&gt;Contents&lt;/strong&gt; button lists a file&amp;#x27;s sets without starting a restore.)&lt;/p&gt;

&lt;h2&gt;Practice exercise&lt;/h2&gt;

&lt;blockquote&gt;&lt;p&gt;&lt;strong&gt;Try it yourself (10 minutes, code in the companion folder):&lt;/strong&gt;&lt;/p&gt;&lt;ol&gt;&lt;li&gt;Run &lt;code&gt;setup.sql&lt;/code&gt;, take the full backup with &lt;code&gt;01-backup-full.sql&lt;/code&gt;, then break the data with &lt;code&gt;02-break-it.sql&lt;/code&gt;.&lt;/li&gt;&lt;li&gt;Restore over the damage using the SSMS dialogs (Steps 9–15) — deliberately leaving &lt;strong&gt;Close existing connections&lt;/strong&gt; unticked the first time, with your damage window still open. Read the error. Then set the three Step 13 switches properly and restore clean.&lt;/li&gt;&lt;li&gt;Run &lt;code&gt;05-verify.sql&lt;/code&gt; and confirm the proof properly: three rows, &lt;em&gt;and&lt;/em&gt; Jonas in Berlin.&lt;/li&gt;&lt;li&gt;Bonus: restore the same file as &lt;code&gt;DemoDbPractice&lt;/code&gt; with &lt;code&gt;04-restore-as-copy.sql&lt;/code&gt; — remember to rename the Restore As paths (or the WITH MOVE targets) first.&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;Reply with your backup file&amp;#x27;s size in KB — I read every one.&lt;/p&gt;&lt;/blockquote&gt;

&lt;h2&gt;FAQ&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Do I need a full backup before a differential or log backup?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Yes. A differential captures changes &lt;em&gt;since the last full backup&lt;/em&gt;, and a log chain starts from one too. No full backup, nothing for the others to stand on — which is why Part A comes first.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Can I restore a SQL Server 2025 backup onto SQL Server 2022?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;No — restores only move to the same or a newer version (Errors #6). Going down a version means scripting the schema and moving the data, not backup/restore.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Does restoring a database overwrite it without asking?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;No. Without &lt;strong&gt;Overwrite (WITH REPLACE)&lt;/strong&gt;, SQL Server&amp;#x27;s safety checks stop a restore that would replace an existing database (Errors #1). REPLACE is a deliberate act — the dialog makes you tick it, and T-SQL makes you type it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Is a verified backup (RESTORE VERIFYONLY) a proven backup?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It proves the backup set is complete and readable — worth doing, and &lt;code&gt;01-backup-full.sql&lt;/code&gt; includes it. It does not prove the restore itself will succeed on your target, with your paths and your permissions. The only full proof is a test restore — Part D is exactly that, pointed at a copy.&lt;/p&gt;

&lt;h2&gt;Get the code&lt;/h2&gt;

&lt;p&gt;The companion folder has every script from this post, in run order: &lt;code&gt;setup.sql&lt;/code&gt;, the backup statement SSMS generated (&lt;code&gt;01-backup-full.sql&lt;/code&gt;), the deliberate damage (&lt;code&gt;02-break-it.sql&lt;/code&gt;), both restores as T-SQL (&lt;code&gt;03-restore-overwrite.sql&lt;/code&gt;, &lt;code&gt;04-restore-as-copy.sql&lt;/code&gt;), and the restore-proof SELECTs (&lt;code&gt;05-verify.sql&lt;/code&gt;):&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/sajjad037/visual-studio-learn-code/tree/main/topic-08-backup-restore-ssms&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;strong&gt;github.com/sajjad037/visual-studio-learn-code — topic-08-backup-restore-ssms&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Sources — the official Microsoft Learn pages behind this post&lt;/h2&gt;

&lt;p&gt;Every procedure and error above traces to Microsoft Learn (accessed November 4–5, 2026):&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;Back up and restore of SQL Server databases: &lt;a href=&quot;https://learn.microsoft.com/en-us/sql/relational-databases/backup-restore/back-up-and-restore-of-sql-server-databases&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;https://learn.microsoft.com/en-us/sql/relational-databases/backup-restore/back-up-and-restore-of-sql-server-databases&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Create a full database backup: &lt;a href=&quot;https://learn.microsoft.com/en-us/sql/relational-databases/backup-restore/create-a-full-database-backup-sql-server&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;https://learn.microsoft.com/en-us/sql/relational-databases/backup-restore/create-a-full-database-backup-sql-server&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Restore a database backup using SSMS: &lt;a href=&quot;https://learn.microsoft.com/en-us/sql/relational-databases/backup-restore/restore-a-database-backup-using-ssms&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;https://learn.microsoft.com/en-us/sql/relational-databases/backup-restore/restore-a-database-backup-using-ssms&lt;/a&gt;&lt;/li&gt;&lt;li&gt;SSMS 22 release notes (22.11.3): &lt;a href=&quot;https://learn.microsoft.com/en-us/ssms/release-notes-22&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;https://learn.microsoft.com/en-us/ssms/release-notes-22&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Editions and supported features of SQL Server 2025: &lt;a href=&quot;https://learn.microsoft.com/en-us/sql/sql-server/editions-and-components-of-sql-server-2025?view=sql-server-ver17&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;https://learn.microsoft.com/en-us/sql/sql-server/editions-and-components-of-sql-server-2025?view=sql-server-ver17&lt;/a&gt;&lt;/li&gt;&lt;li&gt;What&amp;#x27;s new in SQL Server 2025: &lt;a href=&quot;https://learn.microsoft.com/en-us/sql/sql-server/what-s-new-in-sql-server-2025?view=sql-server-ver17&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;https://learn.microsoft.com/en-us/sql/sql-server/what-s-new-in-sql-server-2025?view=sql-server-ver17&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Disclosure: Visual Studio Learn is reader-supported. Some links may be affiliate links, which means the channel may earn a small commission at no extra cost to you. Only tools actually used in the tutorials are ever linked, and affiliate relationships never change a verdict.&lt;/em&gt;&lt;/p&gt;

&lt;div style=&quot;text-align:center;border:1px solid #e3e6ea;border-radius:8px;padding:18px 14px;margin:28px 0;background:#fbfcfd;&quot;&gt;&lt;h3 style=&quot;margin:0 0 6px;&quot;&gt;Enjoying these tutorials?&lt;/h3&gt;&lt;p style=&quot;margin:0 0 6px;&quot;&gt;Get new Visual Studio Learn posts by email — free, no spam:&lt;/p&gt;&lt;style&gt;
    .followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview {
  display: flex !important;
  flex-direction: column !important;
  justify-content: center !important;
  margin-top: 30px !important;
  padding: clamp(17px, 5%, 40px) clamp(17px, 7%, 50px) !important;
  max-width: none !important;
  border-radius: 6px !important;
  box-shadow: 0 5px 25px rgba(34, 60, 47, 0.25) !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview,
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview *{
  box-sizing: border-box !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-heading {
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-heading h5{
  margin-top: 0 !important;
  margin-bottom: 0 !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field {
  margin-top: 20px !important;
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input {
  width: 100% !important;
  height: 40px !important;
  border-radius: 6px !important;
  border: 2px solid #e9e8e8 !important;
  background-color: #fff;
  outline: none !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input {
  color: #000000 !important;
  font-family: &quot;Montserrat&quot; !important;
  font-size: 14px;
  font-weight: 400;
  line-height: 20px;
  text-align: center;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input::placeholder {
  color: #000000 !important;
  opacity: 1 !important;
}

.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input:-ms-input-placeholder {
  color: #000000 !important;
}

.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input::-ms-input-placeholder {
  color: #000000 !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button {
  margin-top: 10px !important;
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button button {
  width: 100% !important;
  height: 40px !important;
  border: 0 !important;
  border-radius: 6px !important;
  line-height: 0px !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button button:hover {
  cursor: pointer !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .powered-by-line {
  color: #231f20 !important;
  font-family: &quot;Montserrat&quot; !important;
  font-size: 13px !important;
  font-weight: 400 !important;
  line-height: 25px !important;
  text-align: center !important;
  text-decoration: none !important;
  display: flex !important;
  width: 100% !important;
  justify-content: center !important;
  align-items: center !important;
  margin-top: 10px !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .powered-by-line img {
  margin-left: 10px !important;
  height: 1.13em !important;
  max-height: 1.13em !important;
}

  &lt;/style&gt;
  &lt;div class=&quot;followit--follow-form-container&quot; attr-a attr-b attr-c attr-d attr-e attr-f&gt;
    &lt;form data-v-f93f6730=&quot;&quot; action=&quot;https://api.follow.it/subscription-form/SmRpWjEzV1ovU0FOK2R2Y0oxajFBUmtIdW44K1ZiUWZRUm1FZVdpVlRKUHFLSFBKWjE4M1hTSk5yRU5oNmg0RU5zelhuTllNTGU1UmwzRkN0NzZWbnd2YkV5VWFwYnptWFdtekI3eXdwMmhtamtUNit3dlNSblVmRjJxd1JGbjZ8Q05yQWR2eitvYW5HRmNMMzJkbStpM09rNmxXZ0ZjWWhyOERya2VvVXRzZz0=/8&quot; method=&quot;post&quot;&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;form-preview&quot; style=&quot;background-color: rgb(255, 255, 255); border-style: solid; border-width: 1px; border-color: rgb(204, 204, 204); position: relative;&quot;&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;preview-heading&quot;&gt;&lt;h5 data-v-f93f6730=&quot;&quot; style=&quot;font-family: Arial; font-weight: bold; color: rgb(0, 0, 0); font-size: 16px; text-align: center; text-transform: none !important;&quot;&gt;Get new posts by email:&lt;/h5&gt;&lt;/div&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;preview-input-field&quot;&gt;&lt;input data-v-f93f6730=&quot;&quot; type=&quot;email&quot; name=&quot;email&quot; required=&quot;&quot; placeholder=&quot;Enter your email&quot; spellcheck=&quot;false&quot; style=&quot;font-family: Arial; font-weight: normal; color: rgb(0, 0, 0); font-size: 14px; text-align: center; background-color: rgb(255, 255, 255); text-transform: none !important;&quot; /&gt;&lt;/div&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;preview-submit-button&quot;&gt;&lt;button data-v-f93f6730=&quot;&quot; type=&quot;submit&quot; style=&quot;font-family: Arial; font-weight: bold; color: rgb(255, 255, 255); font-size: 16px; text-align: center; background-color: rgb(0, 0, 0); text-transform: none !important;&quot;&gt;Subscribe&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/form&gt;&lt;a href=&quot;https://follow.it&quot; class=&quot;powered-by-line&quot;&gt;Powered by &lt;img src=&quot;https://follow.it/images/colored-logo.svg&quot; alt=&quot;follow.it&quot; height=&quot;17px&quot;/&gt;&lt;/a&gt;
  &lt;/div&gt;&lt;p style=&quot;margin:12px 0 0;font-size:14px;&quot;&gt;Follow along: &lt;a href=&quot;https://www.youtube.com/@VisualStudioLearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/youtube/FF0000&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;YouTube&quot; /&gt; YouTube&lt;/a&gt; · &lt;a href=&quot;https://www.facebook.com/VisualStudio.Learn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/facebook/1877F2&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;Facebook&quot; /&gt; Facebook&lt;/a&gt; · &lt;a href=&quot;https://www.instagram.com/visualstudiolearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/instagram/E4405F&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;Instagram&quot; /&gt; Instagram&lt;/a&gt; · &lt;a href=&quot;https://www.tiktok.com/@visualstudiolearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/tiktok/000000&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;TikTok&quot; /&gt; TikTok&lt;/a&gt; · &lt;a href=&quot;https://www.visualstudiolearn.com/feeds/posts/default?alt=rss&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/rss/FFA500&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;RSS&quot; /&gt; RSS&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.visualstudiolearn.com/feeds/5467713991224651713/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.visualstudiolearn.com/2026/11/how-do-i-backup-and-restore-sql-server.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/5467713991224651713'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/5467713991224651713'/><link rel='alternate' type='text/html' href='http://www.visualstudiolearn.com/2026/11/how-do-i-backup-and-restore-sql-server.html' title='How Do I Backup and Restore a SQL Server Database in SSMS? (Step by Step, 2026)'/><author><name>The Loonie Lesson</name><uri>http://www.blogger.com/profile/02243798795439866634</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRY4V9cFLhqMxHWBOWCGBiGUqjan14Vda_dy5rz-C41kApceM-u6jAxS2_6AIRZtGkQeyiQolq1gCeSBhpo573lYq37MLmMGgSKDQLasowic65VoitivYCzqrmUDUqxJuqCTyzah8JuALYtW0Iz7rXIvIcj-ovhVpEuEhQJ6yG1FUQ-A/s1600/loonie-lesson-logo-profile.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiamLjkzwhYoRWc79KGJdNL2HG-wBY23Tchq5PAnSmrob1D9x-nMAz7x6OUEcXCnmEAiRgJA1Lv4GSDOq3XXxLJIpDRmeJyL7eb3X7MLWJvUfI1iuUoWEfCEU1zzr35h3qbxn9bFES_ZusF6eCeujwzGAFsF5lPQ5IGtU--v07DmiEiLNx2QUbFey3Rvag/s72-c/s3.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8861905861585113921.post-1588386196091207986</id><published>2026-11-04T02:22:50.000-05:00</published><updated>2026-11-22T02:50:54.833-05:00</updated><title type='text'>How Do I Connect SQL Server to Visual Studio? Three Ways — Server Explorer, Object Explorer &amp; Connected Services (2026)</title><content type='html'>&lt;p&gt;You connect Visual Studio to SQL Server three ways — &lt;strong&gt;Server Explorer&lt;/strong&gt; for a design-time connection, &lt;strong&gt;SQL Server Object Explorer&lt;/strong&gt; to browse and query the way you would in SSMS, and &lt;strong&gt;Connected Services&lt;/strong&gt; to wire the database into a project so your code can use it. All three are below, click by click, on Visual Studio 2022 17.14.44 with SQL Server 2025 — including the one encryption error that stops almost everyone the first time.&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;&lt;strong&gt;Versions used in this tutorial&lt;/strong&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Visual Studio 2022 — &lt;strong&gt;17.14.44&lt;/strong&gt; (released October 29, 2026)&lt;/li&gt;&lt;li&gt;SQL Server &lt;strong&gt;2025 (17.x)&lt;/strong&gt; — Enterprise Developer edition (free for development and testing)&lt;/li&gt;&lt;li&gt;SQL Server Management Studio &lt;strong&gt;22.11.3&lt;/strong&gt; (for the comparison section)&lt;/li&gt;&lt;li&gt;Microsoft.Data.SqlClient &lt;strong&gt;6.1.7&lt;/strong&gt; (the LTS line, supported until August 14, 2028)&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;On Visual Studio 2026? Same three tools, same clicks — this walkthrough is captured on 2022, which is still fully supported, and everything below works identically in the newer IDE.&lt;/p&gt;&lt;/blockquote&gt;

&lt;h2&gt;Before you start: set up your environment from zero&lt;/h2&gt;

&lt;p&gt;Nothing here assumes you already have anything installed. We install Visual Studio with the right workloads, a real SQL Server you control, and SSMS — then verify all of them before connecting anything.&lt;/p&gt;

&lt;h3&gt;Step 1 — Visual Studio 2022 with the data workloads&lt;/h3&gt;

&lt;ol&gt;&lt;li&gt;Download &lt;strong&gt;Visual Studio 2022 Community&lt;/strong&gt; (free) from Microsoft and run the Visual Studio Installer.&lt;/li&gt;&lt;li&gt;On the &lt;strong&gt;Workloads&lt;/strong&gt; tab, tick:&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Data storage and processing&lt;/strong&gt; — brings SQL Server Data Tools (SSDT) and SQL Server Express LocalDB. Without it, two of the three methods below do not exist in your IDE.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;.NET desktop development&lt;/strong&gt; — for the demo project in Method 3.&lt;/li&gt;&lt;li&gt;Optional: &lt;strong&gt;Azure development&lt;/strong&gt;, only for the Azure SQL path in Method 3.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;Click &lt;strong&gt;Install&lt;/strong&gt;, launch Visual Studio, and verify: &lt;strong&gt;View → SQL Server Object Explorer&lt;/strong&gt; exists. (Minimums: 64-bit Windows 11, 4 GB RAM — 16 GB recommended — and 20–50 GB free disk for a typical install.)&lt;/li&gt;&lt;/ol&gt;

&lt;h3&gt;Step 2 — SQL Server 2025 Developer edition&lt;/h3&gt;

&lt;p&gt;LocalDB (from Step 1) covers Methods 1 and 3, but Method 2 deserves a real server. New in SQL Server 2025, there are &lt;strong&gt;two&lt;/strong&gt; free Developer editions: &lt;strong&gt;Enterprise Developer&lt;/strong&gt; (every Enterprise feature) and &lt;strong&gt;Standard Developer&lt;/strong&gt; (every Standard feature). Pick Enterprise Developer for a general dev machine; pick Standard Developer when production runs Standard, so no Enterprise-only feature sneaks into your schema. Both are free — no key, no subscription — licensed for development and testing, not production.&lt;/p&gt;

&lt;ol&gt;&lt;li&gt;On Microsoft&amp;#x27;s SQL Server downloads page, choose &lt;strong&gt;Developer → Enterprise Developer&lt;/strong&gt;.&lt;/li&gt;&lt;li&gt;Run setup → &lt;strong&gt;Installation → New SQL Server standalone installation&lt;/strong&gt;.&lt;/li&gt;&lt;li&gt;At &lt;strong&gt;Instance Configuration&lt;/strong&gt;, keep the &lt;strong&gt;default instance&lt;/strong&gt; or name yours — write the name down; you will type it into connection dialogs exactly.&lt;/li&gt;&lt;li&gt;At &lt;strong&gt;Server Configuration&lt;/strong&gt;, set the &lt;strong&gt;SQL Server Database Engine&lt;/strong&gt; service to start &lt;strong&gt;automatically&lt;/strong&gt;.&lt;/li&gt;&lt;li&gt;At &lt;strong&gt;Database Engine Configuration → Authentication Mode&lt;/strong&gt;, choose deliberately: &lt;strong&gt;Windows authentication mode&lt;/strong&gt; is enough for this whole post and is the safer default; &lt;strong&gt;Mixed Mode&lt;/strong&gt; additionally enables SQL logins such as &lt;code&gt;sa&lt;/code&gt; — pick it only if you actually need them. You are choosing your server&amp;#x27;s security shape, not ticking a box.&lt;/li&gt;&lt;li&gt;Click &lt;strong&gt;Add Current User&lt;/strong&gt; so your Windows account is a SQL Server administrator, then &lt;strong&gt;Install&lt;/strong&gt;.&lt;/li&gt;&lt;li&gt;Verify in &lt;strong&gt;Services&lt;/strong&gt;: your instance is &lt;strong&gt;Running&lt;/strong&gt;. (Needs Windows 10 or later, 4 GB RAM recommended, 6 GB free disk.)&lt;/li&gt;&lt;/ol&gt;

&lt;h3&gt;Step 3 — SSMS 22 (for the comparison later)&lt;/h3&gt;

&lt;p&gt;Download &lt;strong&gt;SQL Server Management Studio 22&lt;/strong&gt; (latest: 22.11.3) from Microsoft — a separate free tool, installed through the Visual Studio Installer — and connect it to the same instance. We will put it side by side with Visual Studio near the end.&lt;/p&gt;&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi1AgFzFX-d8QEso15dhZrlVlSh823S1khxTKqcKCBlBOsvcIsGUE050853LHFNJaUw_w-t7pLxdjoqJzz9cdLlbnCp793zdWANhJTuNAbaCOIA93bae5UrLrLNkUZJyfHnRunVnRmsO8O303woI2eT-Gqh5T5TRBO72I-O9J-hTXv-UETa5bRnRXSACr0/s1919/s1.jpg&quot; alt=&quot;SSMS Connect dialog with Windows Authentication selected, Encrypt set to Mandatory and Trust Server Certificate unticked&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;SSMS&#39;s Connect dialog: Windows Authentication, Encrypt Mandatory, and Trust Server Certificate unticked — the settings this post connects with.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;h3&gt;Step 4 — Verify everything&lt;/h3&gt;

&lt;p&gt;In a terminal, run &lt;code&gt;sqllocaldb info&lt;/code&gt;. You should see &lt;strong&gt;MSSQLLocalDB&lt;/strong&gt; — the automatic LocalDB instance, owned by your Windows user, starting on demand. LocalDB is a lightweight developer edition of SQL Server Express: user mode, no service to manage, Windows-only. The Developer instance is verified in Services above. Two green checks — now the three methods.&lt;/p&gt;

&lt;h2&gt;Method 1 — Server Explorer: the design-time connection&lt;/h2&gt;

&lt;p&gt;The oldest way in, and still the fastest way to attach a database to a project for design-time work.&lt;/p&gt;

&lt;ol&gt;&lt;li&gt;Open &lt;strong&gt;View → Server Explorer&lt;/strong&gt;.&lt;/li&gt;&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUNGKJEdio6IV8OB_6lRUke_V58SnjybNf0WBImXvvoJ9Smy7DpsSeFEW6MdF9CBDoFhb673TdLxg6Bd9grpof2FQtul4VoPh1PQzc0pXbKP8yCJJWjccqP1G3JO8eIMNdE4DL4AylCQolz5DYWoQXPStjqBjA41epbGheRJFXLa8M3qnBWtAM9_yz-eo/s1919/s3.jpg&quot; alt=&quot;Server Explorer panel in Visual Studio showing the Data Connections node before any connection is added&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Server Explorer before anything is added: the Data Connections node is where every connection in this method will live.&lt;/figcaption&gt;&lt;/figure&gt;&lt;li&gt;Click &lt;strong&gt;Connect to Database&lt;/strong&gt; in the toolbar (or right-click &lt;strong&gt;Data Connections → Add Connection&lt;/strong&gt;).&lt;/li&gt;&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3yd2QFtncgIbNr5HcGYSttrvDQlPFdYfLydHWIsqTxyaTByTuwy-EZUg9oz5oe4_8fAjggrEt7j8-UjJFF3U5-aQDYY7v9xQTf0Ggrbo8Jxf5BJ0To5ytelw55QgZ2YKW85tqr5zxcxf9avTMttZNO5lgqvcClQyswLttq93o8ObuTGF_REhfHIDWkO8/s1919/s6.jpg&quot; alt=&quot;Right-click menu on Data Connections in Server Explorer showing Add Connection&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Right-click Data Connections → Add Connection… — the menu this method starts from.&lt;/figcaption&gt;&lt;/figure&gt;&lt;li&gt;In &lt;strong&gt;Choose Data Source&lt;/strong&gt;, select &lt;strong&gt;Microsoft SQL Server&lt;/strong&gt; → OK. (Different source preselected? Use &lt;strong&gt;Change&lt;/strong&gt;.)&lt;/li&gt;&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEibe04_NvyZzeK5618IFYCw5Nz8PPNj276LpK-Yl6VNUjMISUSRy-cG2DEXh4fPsg-7uUmAZspiR_WmgoJzJ9f5NENS_TuL362y3yINcZ2_uaV71GnHs328oqAhBb5jCr9f0l-Xcp9Pz8ejQ-Ak-n3T4VqCQjHrmkutAgq7KrvTGkRGJhO7RpXdjiRFQdM/s1919/c3.jpg&quot; alt=&quot;Visual Studio Add Connection dialog with Microsoft SQL Server selected as the data source&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;The real Add Connection dialog — this is where every Visual Studio database connection starts.&lt;/figcaption&gt;&lt;/figure&gt;&lt;li&gt;In &lt;strong&gt;Add Connection&lt;/strong&gt;: &lt;strong&gt;Server name&lt;/strong&gt; &lt;code&gt;(localdb)\MSSQLLocalDB&lt;/code&gt;; &lt;strong&gt;Authentication:&lt;/strong&gt; Windows Authentication; &lt;strong&gt;database:&lt;/strong&gt; pick one from the drop-down.&lt;/li&gt;&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhn4fZ_-IPCz-5yB0TVWwHYg3x4c17RY0VEk2rPJhSvTZoug1EsZbAZ9FhBTZX_GhCqRrcFtSWa25Z31ATWYRiRcnx6NlMq7TXnuSws9hzx8vqBRyT5ZiDkveSqtUKg6-RfKabE-7rk-zaGu2WwbC-pXwye5g2QWugZuh3qqf67664TEZlC33QNoltTfTA/s1919/s4.jpg&quot; alt=&quot;Add Connection dialog filled in with server name localhost and database DemoDb, Windows Authentication, Encrypt Mandatory (True) and Trust Server Certificate unticked&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;The dialog filled in: a server name, Windows Authentication, a database — and Encrypt already Mandatory (True), with Trust Server Certificate unticked.&lt;/figcaption&gt;&lt;/figure&gt;&lt;li&gt;Click &lt;strong&gt;Test Connection&lt;/strong&gt;. The success message is the fastest honest check that your server name and login are both right.&lt;/li&gt;&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh8g4HWC18NQVLdCbuD0bPTOfHUm9MfRA2YPF8go-eAgbenjswVt8WfBbFCDTAbRkGW85-Q4pR7FcSZzrIQOUQvC0o3UpqB_i2rqhBT4ef3KCoOJRESAuoTclvlM0Vteav6g8WO0YIKZK5zMWJEelWhHsmXDYOyIO6UOuWqQRgON57m9T-ETcnLQMdEf90/s1919/c4.jpg&quot; alt=&quot;The certificate chain error shown when connecting with Encrypt on and no trusted certificate&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;The error exactly as it appears on a current Visual Studio — Encrypt is on by default, and the local server&#39;s certificate is not trusted.&lt;/figcaption&gt;&lt;/figure&gt;&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-sWaEpMcVlvlK8Fo9ilwC2mMnXl0F3TfvxuT3ja3SsyMdkuxjRKcwrhfYAMGTiQ4PpPv_etGMYm0K1vU3hEa7Fm2K6JXptWYEfh_bKrcyRRhD90K95qbGrp4Q8kdnJA5lb8mz44ldlDsukUiVogwg0s2jJxnNjqGM7sRXg2NCyjgVNc-O6-msKWWJ4Gw/s1600/s5.jpg&quot; alt=&quot;Visual Studio Add Connection dialog for DemoDb on localhost showing the Test connection succeeded message&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;With the certificate fix in place, Test Connection answers &quot;Test connection succeeded.&quot; — this is the success Method 1 is checking for.&lt;/figcaption&gt;&lt;/figure&gt;&lt;li&gt;Click &lt;strong&gt;OK&lt;/strong&gt; and expand the connection under &lt;strong&gt;Data Connections&lt;/strong&gt; to browse tables.&lt;/li&gt;&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEioGVC0gDWhqkeYx83Rh3TtGpaIcXpymjWHWOOJ5R7c21wbdBhPg7jLoGL7J9AQPEKSGfF70P0WAP6eZnsj8k6JkOB5GPMeoE4oaK90mHyLSeOeH_XT-M7SWEpeXFuZLbkT9voJJhj4T3kjNhhOg1_rzJlUPf4tU_0cdfu1FPW_0ubsW4lmBR10w4KZGqk/s1919/c6.jpg&quot; alt=&quot;Server Explorer showing DemoDb connected and the DemoCustomers table data with three rows&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Connected — the DemoCustomers table opened from Server Explorer.&lt;/figcaption&gt;&lt;/figure&gt;&lt;/ol&gt;

&lt;p&gt;One thing old tutorials never showed: in designer flows, Visual Studio saves this connection&#39;s string into your project&#39;s configuration (the &lt;code&gt;connectionStrings&lt;/code&gt; section of App.config). That string — not the tree node — is what your application actually uses, which is why the decoding section below matters.&lt;/p&gt;

&lt;h2&gt;Method 2 — SQL Server Object Explorer: SSMS inside Visual Studio&lt;/h2&gt;

&lt;p&gt;If Server Explorer is a connection, Object Explorer is a workplace. It requires SSDT from Step 1 and deliberately mirrors the SSMS tree.&lt;/p&gt;

&lt;ol&gt;&lt;li&gt;Open &lt;strong&gt;View → SQL Server Object Explorer&lt;/strong&gt; (shortcut: &lt;strong&gt;Ctrl+\, Ctrl+S&lt;/strong&gt;).&lt;/li&gt;&lt;li&gt;Right-click the &lt;strong&gt;SQL Server&lt;/strong&gt; node → &lt;strong&gt;Add SQL Server&lt;/strong&gt;.&lt;/li&gt;&lt;li&gt;In the &lt;strong&gt;Connect&lt;/strong&gt; dialog, enter your Developer instance&#39;s server name (machine name for a default instance, &lt;code&gt;HOST\INSTANCE&lt;/code&gt; for a named one), keep &lt;strong&gt;Windows Authentication&lt;/strong&gt;, click &lt;strong&gt;Connect&lt;/strong&gt;.&lt;/li&gt;&lt;li&gt;Expand the instance → &lt;strong&gt;Databases&lt;/strong&gt; — tables, views and stored procedures in the same hierarchy SSMS shows.&lt;/li&gt;&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2yxOhH2PlbTuTtXjTECYLCwvWiAb9O7qGKKIpKiAEuwReRzaCZux_k2iIbIQrLm9e4uR0p76I82MOMWayBYATxYi0FqjfXMjJQd4Z9ObttlEkUEHBPrkN7dFdlODoWzuHgaPpEOiKbZGCHKTDrr7oTw5Xc3f59Y1OPbSCEqlpqkUg1xgk1_vu5FyfkEs/s1919/c7.jpg&quot; alt=&quot;SQL Server Object Explorer in Visual Studio showing DemoDb and its tables&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Method 2: the same server and database in SQL Server Object Explorer.&lt;/figcaption&gt;&lt;/figure&gt;&lt;li&gt;Right-click a database → &lt;strong&gt;New Query&lt;/strong&gt; and run real T-SQL:&lt;/li&gt;&lt;/ol&gt;&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdRLX2RaREfPrey8rtxWOy4pScv4WPfC2dBeadE7N6dIpYUSo2_aFkwZCn7XlLBjDcPWolzW3aSmBR4omV7k--Tm8VgVgLzdIhIn5enlsnK6uV17zlyiSelihGa7Aot2mIPdCT_B7sQqpuZDvDDmZD2X5VyOcfdZE10XjjQQknkU1CofNCuSd6FLNOTpk/s1919/s2.jpg&quot; alt=&quot;SSMS New Database dialog with DemoDb typed as the database name&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Where DemoDb itself came from: SSMS → right-click Databases → New Database, with the name typed in.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;pre&gt;&lt;code&gt;CREATE TABLE dbo.DemoCustomers (
CustomerId INT IDENTITY(1,1) PRIMARY KEY,
FullName NVARCHAR(100) NOT NULL,
City NVARCHAR(100) NOT NULL
);
INSERT INTO dbo.DemoCustomers (FullName, City) VALUES
(N&#39;Amara&#39;, N&#39;Toronto&#39;),
(N&#39;Jonas&#39;, N&#39;Berlin&#39;),
(N&#39;Priya&#39;, N&#39;Lagos&#39;);
SELECT CustomerId, FullName, City FROM dbo.DemoCustomers;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;(The companion code folder at the end contains this exact script as &lt;code&gt;setup.sql&lt;/code&gt;, so the post, the video and the code can never drift apart.)&lt;/p&gt;&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj2Z0F0R9Qu2VQWWN-fF80py9I_UhSo4tQ_QaesAe-2hu2twrAeK0gpJ7L3CEy6pCi-SAi9FvShs5doAjFvBiYJQCI0c5cHdsIzYdHKM3fOm0GsT9q8keqNKxmA3fNZBE277qwutBIyk_ara_GNb4YXhzguDQgSiz3UGJeYlZBIpzhGS7dSczzOhDlBODI/s1919/c2.jpg&quot; alt=&quot;SSMS query window showing the setup script and the results grid with three customer rows&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;The setup script run — DemoCustomers with its three rows.&lt;/figcaption&gt;&lt;/figure&gt;&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRmK169ZluQbW0IfMMTjKYAjx4moz2A9l2eTwEOAe_4L-hEHIGwEiIG2gjdsVlOwxiBd77k5iqMaMnd4eyUz0gCLGCz5VQ6D4Yf5axDGycMarkxTRD63NBMrEsGtr7XT6bldnpNuK1K5QqSjX4PKjOmlOzY2tJYUjcATDIoHisw77VDvOD6EOrCPaLqu8/s1919/c1.jpg&quot; alt=&quot;SSMS Object Explorer showing the local SQL Server instance with the DemoDb database expanded&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;SQL Server Management Studio, connected, with DemoDb created.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;h2&gt;Method 3 — Connected Services: wire the database into a project&lt;/h2&gt;

&lt;p&gt;Methods 1 and 2 connect &lt;em&gt;you&lt;/em&gt; to the database. Connected Services connects &lt;em&gt;your project&lt;/em&gt; — package references added, connection string stored properly, dependency registered.&lt;/p&gt;

&lt;ol&gt;&lt;li&gt;In &lt;strong&gt;Solution Explorer&lt;/strong&gt;, right-click the &lt;strong&gt;Connected Services&lt;/strong&gt; node → &lt;strong&gt;Add&lt;/strong&gt;.&lt;/li&gt;&lt;li&gt;Choose &lt;strong&gt;SQL Server Database&lt;/strong&gt;. The Connect to dependency page offers four targets: &lt;strong&gt;SQL Server Express LocalDB&lt;/strong&gt;, &lt;strong&gt;SQL Server Database in a local container&lt;/strong&gt;, &lt;strong&gt;SQL Server Database (on-premises, local network)&lt;/strong&gt;, and &lt;strong&gt;Azure SQL Database&lt;/strong&gt;.&lt;/li&gt;&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3PQzYfCC7WHQi9KNe8s7nTB6aeQ1ZqpufEWf8gv5nmRotonjGR-I4bMcosLjT8fl-J5tSnPhwUqj98XOac5giX1gaqQZ040yCsxmqgWcGGGANiqycRBwrFMwAchzVX_TgYs3DAbnjN8oxg_NfR9uuIW5o_HejjM-sNgvLLIEOSrU9GQsoXmOWUSHKB2E/s1919/c8-flow-list.jpg&quot; alt=&quot;Connected Services Add dependency list in Visual Studio with SQL Server Database at the top&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;Connected Services → Add dependency. SQL Server Database is the on-premises entry the wizard walks through.&lt;/figcaption&gt;&lt;/figure&gt;&lt;li&gt;Pick &lt;strong&gt;SQL Server Express LocalDB&lt;/strong&gt; — the &lt;strong&gt;MSSQLLocalDB&lt;/strong&gt; instance.&lt;/li&gt;&lt;li&gt;For connection-string storage, choose the &lt;strong&gt;local secrets file&lt;/strong&gt; — never a source file (the decoding section explains why).&lt;/li&gt;&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhPT5_GX-B5Cj-Z7Z0y75u1iEnXm9ck8VljbfNkungofUD3cxJ65TbjKCV-_LucHUrDYuk3D1yQaN9skCLr-Vdf-eoJou81ovxE8MVkeGYdXwANagHzLNTzGYdKcu6SPp66tRxjWTXjO7Yj_9rxjsksa4w54kKVEEdLhJVp-gscvxzKNdNTpAAdgBzwS04/s1919/c8-flow-connect.jpg&quot; alt=&quot;Connected Services page in Visual Studio with a SQL Server Database connection&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;The Connect page: the connection-string name and value Connected Services stores for the project — here in the local secrets file.&lt;/figcaption&gt;&lt;/figure&gt;&lt;li&gt;&lt;strong&gt;Next → Finish:&lt;/strong&gt; When the wizard completes, it stores the string in the local secrets file from Step 4 and the dependency appears under &lt;strong&gt;Service Dependencies&lt;/strong&gt;. One honest warning from this tutorial&#39;s own capture session: on our machine the wizard stopped at that last step — &lt;strong&gt;&amp;quot;Failed to configure Secrets store&amp;quot;&lt;/strong&gt; — twice, and no dependency appeared (see &lt;strong&gt;Errors you may hit #7&lt;/strong&gt;). The connection string itself was never the problem: the companion console app prints the server version and the demo rows with the same string, and Methods 1 and 2 connect with it too. If the wizard stops on your machine, store the string yourself (Error #7 shows how) and carry on.&lt;/li&gt;&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgbBe42YVngDk2FRBf3NCrNXxg7-sQijQXZOr5sMs5EfAm2XlcKJO4jOpMMurVfr37hO9CHBofIsTF46JWlb70iRwTq2vgngIMwITRKrdV-bYDCnEKe8gxlMeWJzK6t_MIY4WDhj50xsRs55gwY0QSl_YO1gObnRCqDJI2GOFc6-enLCCSCwWJt47MA0q0/s1919/s7.jpg&quot; alt=&quot;Visual Studio debug console showing the companion app connected to DemoDb and printing the three demo rows&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;The companion console app running against the database: connected, the three demo rows printed, exit code 0.&lt;/figcaption&gt;&lt;/figure&gt;&lt;/ol&gt;

&lt;p&gt;The same dialog reaches on-premises servers and Azure SQL Database (that path needs the Azure development workload and an Azure account, and since VS 2022 17.12 it uses Microsoft Entra authentication — Visual Studio does &lt;strong&gt;not&lt;/strong&gt; create the Entra admin user on the database for you, so if none exists, sign-in fails there, not in VS).&lt;/p&gt;

&lt;h2&gt;Visual Studio or SSMS — which do I open?&lt;/h2&gt;

&lt;p&gt;Not rivals: Microsoft ships both, on separate cadences, for different jobs.&lt;/p&gt;

&lt;table&gt;&lt;tr&gt;&lt;th&gt;The job&lt;/th&gt;&lt;th&gt;Open&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Building the app that uses the database&lt;/td&gt;&lt;td&gt;Visual Studio&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Browsing and querying while you code&lt;/td&gt;&lt;td&gt;SQL Server Object Explorer&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Wiring a database into a project&lt;/td&gt;&lt;td&gt;Connected Services&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Administering the server itself — backups, jobs, logins, configuration&lt;/td&gt;&lt;td&gt;SSMS&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;SSMS administers the &lt;em&gt;server&lt;/em&gt;, whether or not any application exists; Visual Studio&amp;#x27;s tools live next to your code and its connections become part of your project. You will end up using both — that is the intended workflow. (Thursday&amp;#x27;s post goes deeper on the SSMS side: backing up and restoring this same database.)&lt;/p&gt;&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVLEkECDVJ1g5QeaT8RB-JFopsyIhfVTErI3G-0Sktx99A97fhj0XDVKypGfpMtSQHiO68i0D-tbd-QaZtFMYgk0djEeVePQz-sbD7J3MlugHgPhgnCYRg15RFMW1NljdcjxcbcQB66adgWcYuSZjxXV3Z7QWIf1MlN1A0nFhGXYrZGr_FguIYvHk6ThY/s1919/s8.jpg&quot; alt=&quot;SSMS Object Explorer showing localhost, Databases, DemoDb and its Tables folder expanded to dbo.DemoCustomers&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;The SSMS side of the same database: DemoDb → Tables, with dbo.DemoCustomers.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;h2&gt;Your connection string, decoded&lt;/h2&gt;

&lt;p&gt;Everything above eventually produces one line of text. The LocalDB version, keyword by keyword:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Server=(localdb)\MSSQLLocalDB;Database=DemoDb;Integrated Security=true;Encrypt=true;TrustServerCertificate=true;&lt;/code&gt;&lt;/pre&gt;

&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Server&lt;/strong&gt; (Data Source) — &lt;code&gt;(localdb)\MSSQLLocalDB&lt;/code&gt; for LocalDB; &lt;code&gt;HOSTNAME&lt;/code&gt; for a default instance; &lt;code&gt;HOSTNAME\INSTANCE&lt;/code&gt; for a named one (the shape for a hosted or on-premises server too); &lt;code&gt;HOSTNAME,1433&lt;/code&gt; pins the TCP port.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Database&lt;/strong&gt; (Initial Catalog) — the database you land in.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Integrated Security=true&lt;/strong&gt; — Windows Authentication: no password in the string, and the recommended pattern for development. SQL Authentication uses &lt;strong&gt;User Id&lt;/strong&gt; + &lt;strong&gt;Password&lt;/strong&gt; instead — and then the string is &lt;em&gt;never&lt;/em&gt; a secret: user secrets or an environment variable, never source control.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Encrypt=true&lt;/strong&gt; — on by default in modern tooling (error #1 below). Non-negotiable on any network you do not own.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;TrustServerCertificate=true&lt;/strong&gt; — Encrypt&amp;#x27;s local-dev companion: accept the dev instance&amp;#x27;s self-signed certificate. Correct at your desk; in production the real fix is a properly trusted certificate, not this switch.&lt;/li&gt;&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Coming from Python/FastAPI?&lt;/strong&gt; Same anatomy, ODBC spelling — pyodbc with Microsoft ODBC Driver 18 for SQL Server:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;DRIVER={ODBC Driver 18 for SQL Server};SERVER=(localdb)\MSSQLLocalDB;DATABASE=DemoDb;Trusted_Connection=yes;Encrypt=yes;TrustServerCertificate=yes&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you deployed the FastAPI app from an earlier tutorial in this series to Azure App Service, this string is exactly what its configuration needs next.&lt;/p&gt;

&lt;h2&gt;Errors you may hit&lt;/h2&gt;

&lt;h3&gt;1. &amp;quot;The certificate chain was issued by an authority that is not trusted&amp;quot;&lt;/h3&gt;

&lt;p&gt;The error that defines connecting to SQL Server in 2026. Since Visual Studio 17.8 the data tools enable &lt;strong&gt;Encrypt by default&lt;/strong&gt; — modern Microsoft.Data.SqlClient does the same — so your connection demands a TLS certificate from a trusted authority. A local dev instance presents a self-signed one, and the connection dies. It is not your server name, not your password, not a broken install.&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;&lt;strong&gt;At your desk:&lt;/strong&gt; add &lt;code&gt;TrustServerCertificate=true&lt;/code&gt; (or tick the equivalent in the Connect dialog) and reconnect. Microsoft&amp;#x27;s own LocalDB examples use exactly this pair.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;In production:&lt;/strong&gt; install a certificate from a trusted authority. The trust-anything switch is a development tool, full stop.&lt;/li&gt;&lt;/ul&gt;&lt;figure style=&quot;margin:20px 0;text-align:center;&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg5swQwWO5YvxasXLNOxkgc966yWQSU5t5eOczjxRLJAjsq4Z_OPtad8jhZ4RbmeBTp5pldiG_bpj0eWsD_9HSEDElDrkc5HC2nuOmR-Vi75OCGlUJf82HF7Y4Ah_D9jR_c-tqeZL2wJtfJHunfSp8_cvhBGipQjtXxuAYsY_7jbcbgPQNLpJfshffDOms/s1919/c5.jpg&quot; alt=&quot;Add Connection Advanced properties with Trust Server Certificate set to True&quot; style=&quot;max-width:100%;height:auto;&quot; /&gt;&lt;figcaption style=&quot;font-size:14px;color:#555;margin-top:6px;&quot;&gt;The fix for a local development server: Trust Server Certificate set to True in Advanced properties.&lt;/figcaption&gt;&lt;/figure&gt;

&lt;h3&gt;2. &amp;quot;A network-related or instance-specific error occurred…&amp;quot; (error 40)&lt;/h3&gt;

&lt;p&gt;Server not found or unreachable. Causes, in order: the instance name is misspelled (the &lt;code&gt;\INSTANCE&lt;/code&gt; half especially), or the Database Engine service is stopped. Open &lt;strong&gt;Services&lt;/strong&gt;, confirm the instance is running, re-check the spelling against Step 2.&lt;/p&gt;

&lt;h3&gt;3. &amp;quot;Login failed for user&amp;quot; (error 18456)&lt;/h3&gt;

&lt;p&gt;Authentication reached the server and was refused — classically, a SQL login (&lt;code&gt;sa&lt;/code&gt;, or one you created) against a server installed in &lt;strong&gt;Windows authentication mode only&lt;/strong&gt;: Mixed Mode was never enabled at setup, or the login does not exist. Fix the Step 2 choice, or create the login under SSMS → Security.&lt;/p&gt;

&lt;h3&gt;4. &amp;quot;(localdb) … server was not found&amp;quot;&lt;/h3&gt;

&lt;p&gt;LocalDB itself is missing. The &lt;strong&gt;Data storage and processing&lt;/strong&gt; workload (or the SQL Server Express LocalDB individual component) installs it — a NuGet package does not. Visual Studio Installer → Modify → add it, then confirm with &lt;code&gt;sqllocaldb info&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;5. A named instance is unreachable from another machine&lt;/h3&gt;

&lt;p&gt;Three suspects: &lt;strong&gt;TCP/IP disabled&lt;/strong&gt; in SQL Server Configuration Manager, the &lt;strong&gt;SQL Browser&lt;/strong&gt; service stopped, or the &lt;strong&gt;firewall&lt;/strong&gt; closed on port 1433. Enable, start, open, restart the instance, retest.&lt;/p&gt;

&lt;h3&gt;6. Python: &amp;quot;Data source name not found and no default driver specified&amp;quot;&lt;/h3&gt;

&lt;p&gt;pyodbc cannot find the driver your string names. Install &lt;strong&gt;Microsoft ODBC Driver 18 for SQL Server&lt;/strong&gt;, then copy its name into &lt;code&gt;DRIVER={...}&lt;/code&gt; character for character — the braces and exact spacing are part of the name.&lt;/p&gt;

&lt;h3&gt;7. Connected Services: &amp;quot;Failed to configure Secrets store&amp;quot;&lt;/h3&gt;

&lt;p&gt;Method 3&#39;s wizard can fail at its last step: the progress dialog ends with &lt;strong&gt;&amp;quot;Failed to configure Secrets store&amp;quot;&lt;/strong&gt; and &amp;quot;Failed to configure dependency SQL Server Database&amp;quot;, and no dependency appears under &lt;strong&gt;Service Dependencies&lt;/strong&gt;. This is not hypothetical — it happened on this tutorial&#39;s own capture machine (Visual Studio Community 2022 17.14.44, November 3, 2026), twice in a row, and it persisted after running &lt;code&gt;dotnet user-secrets init&lt;/code&gt; in the project and restarting Visual Studio, the two fixes most often suggested for it.&lt;/p&gt;

&lt;p&gt;The important part: &lt;strong&gt;the failure is in the wizard&#39;s secrets-store step, not in your connection.&lt;/strong&gt; The connection string itself is unaffected — the exact same string connects fine through Server Explorer (Method 1), SQL Server Object Explorer (Method 2), and the companion console app, which is why this tutorial&#39;s demo was verified end to end. If you hit it:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;Do not debug your server, your login or your string — test the same string in Method 1 first; if it connects there, the database side is proven.&lt;/li&gt;&lt;li&gt;Store the string yourself instead: add it to your user secrets (&lt;code&gt;dotnet user-secrets set &quot;ConnectionStrings:DatabaseConnection&quot; &quot;...&quot;&lt;/code&gt;) or an environment variable, and let your code read it from configuration — the pattern the &amp;quot;Get the code&amp;quot; section uses.&lt;/li&gt;&lt;li&gt;Treat the Connected Services dependency as a convenience that may need a Visual Studio repair or update on the affected install, not as a requirement — nothing in your project depends on the wizard having succeeded.&lt;/li&gt;&lt;/ul&gt;

&lt;h2&gt;Practice exercise&lt;/h2&gt;

&lt;blockquote&gt;&lt;p&gt;&lt;strong&gt;Try it yourself (10 minutes, code in the description):&lt;/strong&gt;&lt;/p&gt;&lt;ol&gt;&lt;li&gt;Run SqlConnectCheck against LocalDB — it prints your server version and three demo rows.&lt;/li&gt;&lt;li&gt;In SQL Server Object Explorer, run a New Query against the same instance: &lt;code&gt;CREATE DATABASE PracticeDb;&lt;/code&gt; — then change ONE word in the connection string (the database name) and re-run. PracticeDb starts empty, so watch what the checker does: it creates the DemoCustomers table inside it, seeds the same three rows, prints them back, and ends with the same success line.&lt;/li&gt;&lt;li&gt;Now break it on purpose: mistype the instance name, re-run, and read the error like a detective — which of the post&amp;#x27;s six errors is it?&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;Reply with the @@VERSION line your run printed — I read every one.&lt;/p&gt;&lt;/blockquote&gt;

&lt;h2&gt;FAQ&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Do I need SSMS if I have Visual Studio?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;No — everything here works without it. Install SSMS when the job becomes administering the server itself: backups, jobs, logins, configuration.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Is SQL Server free for this?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Yes, for development and testing: the Developer editions and LocalDB cost nothing and need no key. Developer editions are &lt;strong&gt;not&lt;/strong&gt; licensed for production — Express is the free edition that is.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Does this work on Visual Studio 2026? VS Code?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;2026: yes — same three tools, same clicks. VS Code is a different editor with its own SQL extension; a different path, not covered here.&lt;/p&gt;

&lt;h2&gt;Get the code&lt;/h2&gt;

&lt;p&gt;The companion folder has the C# console checker (&lt;code&gt;SqlConnectCheck&lt;/code&gt;, Microsoft.Data.SqlClient 6.1.7 LTS, connection string from an environment variable), the Python/pyodbc version, &lt;code&gt;setup.sql&lt;/code&gt; exactly as run above, and a README with the six errors in miniature:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;https://github.com/sajjad037/visual-studio-learn-code/tree/main/topic-07-connect-sql-server-vs&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;Sources — the official Microsoft Learn pages behind this post&lt;/h2&gt;

&lt;p&gt;Every version pin and procedure above traces to Microsoft Learn (accessed November 3, 2026):&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;Visual Studio 2022 release notes (17.14.44): &lt;a href=&quot;https://learn.microsoft.com/en-us/visualstudio/releases/2022/release-notes&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;https://learn.microsoft.com/en-us/visualstudio/releases/2022/release-notes&lt;/a&gt;&lt;/li&gt;&lt;li&gt;SSMS 22 release notes (22.11.3): &lt;a href=&quot;https://learn.microsoft.com/en-us/ssms/release-notes-22&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;https://learn.microsoft.com/en-us/ssms/release-notes-22&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Microsoft.Data.SqlClient support lifecycle (6.1.7 LTS, supported to August 14, 2028): &lt;a href=&quot;https://learn.microsoft.com/en-us/sql/connect/ado-net/sqlclient-driver-support-lifecycle?view=sql-server-ver16&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;https://learn.microsoft.com/en-us/sql/connect/ado-net/sqlclient-driver-support-lifecycle?view=sql-server-ver16&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Editions and supported features of SQL Server 2025 (the Enterprise/Standard Developer split): &lt;a href=&quot;https://learn.microsoft.com/en-us/sql/sql-server/editions-and-components-of-sql-server-2025?view=sql-server-ver17&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;https://learn.microsoft.com/en-us/sql/sql-server/editions-and-components-of-sql-server-2025?view=sql-server-ver17&lt;/a&gt;&lt;/li&gt;&lt;li&gt;SQL Server downloads (the free Developer editions): &lt;a href=&quot;https://www.microsoft.com/en-us/sql-server/sql-server-downloads&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;https://www.microsoft.com/en-us/sql-server/sql-server-downloads&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Hardware and software requirements for installing SQL Server 2025: &lt;a href=&quot;https://learn.microsoft.com/en-us/sql/sql-server/install/hardware-and-software-requirements-for-installing-sql-server-2025?view=sql-server-ver17&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;https://learn.microsoft.com/en-us/sql/sql-server/install/hardware-and-software-requirements-for-installing-sql-server-2025?view=sql-server-ver17&lt;/a&gt;&lt;/li&gt;&lt;li&gt;SQL Server Express LocalDB (the automatic MSSQLLocalDB instance, and Microsoft&amp;#x27;s own LocalDB connection-string example): &lt;a href=&quot;https://learn.microsoft.com/en-us/sql/connect/ado-net/sql/sqlclient-support-localdb?view=sql-server-ver17&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;https://learn.microsoft.com/en-us/sql/connect/ado-net/sql/sqlclient-support-localdb?view=sql-server-ver17&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Disclosure: Visual Studio Learn is reader-supported. Some links may be affiliate links, which means the channel may earn a small commission at no extra cost to you. Only tools actually used in the tutorials are ever linked, and affiliate relationships never change a verdict.&lt;/em&gt;&lt;/p&gt;

&lt;div style=&quot;text-align:center;border:1px solid #e3e6ea;border-radius:8px;padding:18px 14px;margin:28px 0;background:#fbfcfd;&quot;&gt;&lt;h3 style=&quot;margin:0 0 6px;&quot;&gt;Enjoying these tutorials?&lt;/h3&gt;&lt;p style=&quot;margin:0 0 6px;&quot;&gt;Get new Visual Studio Learn posts by email — free, no spam:&lt;/p&gt;&lt;style&gt;
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview {
display: flex !important;
flex-direction: column !important;
justify-content: center !important;
margin-top: 30px !important;
padding: clamp(17px, 5%, 40px) clamp(17px, 7%, 50px) !important;
max-width: none !important;
border-radius: 6px !important;
box-shadow: 0 5px 25px rgba(34, 60, 47, 0.25) !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview, .followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview *{
box-sizing: border-box !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-heading {
width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-heading h5{
margin-top: 0 !important;
margin-bottom: 0 !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field {
margin-top: 20px !important;
width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input {
width: 100% !important;
height: 40px !important;
border-radius: 6px !important;
border: 2px solid #e9e8e8 !important;
background-color: #fff;
outline: none !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input {
color: #000000 !important;
font-family: &quot;Montserrat&quot; !important;
font-size: 14px;
font-weight: 400;
line-height: 20px;
text-align: center;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input::placeholder {
color: #000000 !important;
opacity: 1 !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input::-ms-input-placeholder {
color: #000000 !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input::-ms-input-placeholder {
color: #000000 !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button {
margin-top: 10px !important;
width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button button {
width: 100% !important;
height: 40px !important;
border: 0 !important;
border-radius: 6px !important;
line-height: 0px !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button button:hover {
cursor: pointer !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .powered-by-line {
color: #231f20 !important;
font-family: &quot;Montserrat&quot; !important;
font-size: 13px;
font-weight: 400;
line-height: 25px;
text-align: center !important;
text-decoration: none !important;
display: flex !important;
width: 100% !important;
justify-content: center !important;
align-items: center !important;
margin-top: 10px !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .powered-by-line img {
margin-left: 10px !important;
height: 1.13em !important;
max-height: 1.13em !important;
}
&lt;/style&gt;
&lt;div class=&quot;followit--follow-form-container&quot; attr-a attr-b attr-c attr-d attr-e attr-f&gt;
&lt;form data-v-f93f6730=&quot;&quot; action=&quot;https://api.follow.it/subscription-form/SmRpWjEzV1ovU0FOK2R2Y0oxajFBUmtIdW44K1ZiUWZRUm1FZVdpVlRKUHFLSFBKWjE4M1hTSk5yRU5oNmg0RU5zelhuTllNTGU1UmwzRkN0NzZWbnd2YkV5VWFwYnptWFdtekI3eXdwMmhtamtUNit3dlNSblVmRjJxd1JGbjZ8Q05yQWR2eitvYW5HRmNMMzJkbStpM09rNmxXZ0ZjWWhyOERya2VvVXRzZz0=/8&quot; method=&quot;post&quot;&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;form-preview&quot; style=&quot;background-color: rgb(255, 255, 255); border-style: solid; border-width: 1px; border-color: rgb(204, 204, 204); position: relative;&quot;&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;preview-heading&quot;&gt;&lt;h5 data-v-f93f6730=&quot;&quot; style=&quot;font-family: Arial; font-weight: bold; color: rgb(0, 0, 0); font-size: 16px; text-align: center; text-transform: none !important;&quot;&gt;Get new posts by email&lt;/h5&gt;&lt;/div&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;preview-input-field&quot;&gt;&lt;input data-v-f93f6730=&quot;&quot; type=&quot;email&quot; name=&quot;email&quot; required=&quot;&quot; placeholder=&quot;Enter your email&quot; spellcheck=&quot;false&quot; style=&quot;font-family: Arial; font-weight: normal; color: rgb(0, 0, 0); font-size: 14px; text-align: center; background-color: rgb(255, 255, 255); text-transform: none !important;&quot; /&gt;&lt;/div&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;preview-submit-button&quot;&gt;&lt;button data-v-f93f6730=&quot;&quot; type=&quot;submit&quot; style=&quot;font-family: Arial; font-weight: bold; color: rgb(255, 255, 255); font-size: 16px; text-align: center; background-color: rgb(0, 0, 0); text-transform: none !important;&quot;&gt;Subscribe&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/form&gt;&lt;a href=&quot;https://follow.it&quot; class=&quot;powered-by-line&quot;&gt;Powered by &lt;img src=&quot;https://follow.it/images/colored-logo.svg&quot; alt=&quot;follow.it&quot; height=&quot;17px&quot; /&gt;&lt;/a&gt;
 &lt;/div&gt;&lt;p style=&quot;margin:12px 0 0;font-size:14px;&quot;&gt;Follow along: &lt;a href=&quot;https://www.youtube.com/@VisualStudioLearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/youtube/FF0000&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;YouTube&quot; /&gt; YouTube&lt;/a&gt; · &lt;a href=&quot;https://www.facebook.com/VisualStudio.Learn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/facebook/1877F2&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;Facebook&quot; /&gt; Facebook&lt;/a&gt; · &lt;a href=&quot;https://www.instagram.com/visualstudiolearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/instagram/E4405F&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;Instagram&quot; /&gt; Instagram&lt;/a&gt; · &lt;a href=&quot;https://www.tiktok.com/@visualstudiolearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/tiktok/000000&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;TikTok&quot; /&gt; TikTok&lt;/a&gt; · &lt;a href=&quot;https://www.visualstudiolearn.com/feeds/posts/default?alt=rss&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/rss/FFA500&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;RSS&quot; /&gt; RSS&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.visualstudiolearn.com/feeds/1588386196091207986/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.visualstudiolearn.com/2026/11/how-do-i-connect-sql-server-to-visual.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/1588386196091207986'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/1588386196091207986'/><link rel='alternate' type='text/html' href='http://www.visualstudiolearn.com/2026/11/how-do-i-connect-sql-server-to-visual.html' title='How Do I Connect SQL Server to Visual Studio? Three Ways — Server Explorer, Object Explorer &amp; Connected Services (2026)'/><author><name>The Loonie Lesson</name><uri>http://www.blogger.com/profile/02243798795439866634</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRY4V9cFLhqMxHWBOWCGBiGUqjan14Vda_dy5rz-C41kApceM-u6jAxS2_6AIRZtGkQeyiQolq1gCeSBhpo573lYq37MLmMGgSKDQLasowic65VoitivYCzqrmUDUqxJuqCTyzah8JuALYtW0Iz7rXIvIcj-ovhVpEuEhQJ6yG1FUQ-A/s1600/loonie-lesson-logo-profile.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi1AgFzFX-d8QEso15dhZrlVlSh823S1khxTKqcKCBlBOsvcIsGUE050853LHFNJaUw_w-t7pLxdjoqJzz9cdLlbnCp793zdWANhJTuNAbaCOIA93bae5UrLrLNkUZJyfHnRunVnRmsO8O303woI2eT-Gqh5T5TRBO72I-O9J-hTXv-UETa5bRnRXSACr0/s72-c/s1.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8861905861585113921.post-6914977922879264301</id><published>2026-10-31T15:33:09.000-04:00</published><updated>2026-11-22T02:50:53.514-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="AI Agents"/><category scheme="http://www.blogger.com/atom/ns#" term="Open Source"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><category scheme="http://www.blogger.com/atom/ns#" term="Tutorials"/><title type='text'>Does the 118K-Star AI Trading Agent Actually Work? I Installed and Tested TradingAgents v0.7.1 — Open-Source Agent, Tested #1</title><content type='html'>&lt;p&gt;&lt;strong&gt;Search description:&lt;/strong&gt; I installed TradingAgents v0.7.1 — the 118K-star multi-agent trading framework — and tested it for real. Setup, keys, errors, honest verdict.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Labels:&lt;/strong&gt; Python, AI Agents, Open Source, Tutorials&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Versions used in this post — read this first:&lt;/strong&gt; TradingAgents &lt;strong&gt;v0.7.1&lt;/strong&gt; (release tag, commit &lt;code&gt;c4544913a14ed3df5f1ed7b9e8483270ce20682a&lt;/code&gt; — the exact code tested here) · Python &lt;strong&gt;3.12.3&lt;/strong&gt; (the project requires Python 3.11 or later) · Repository figures — &lt;strong&gt;118,913 stars, 22,731 forks, 884 watchers&lt;/strong&gt; — read from the project&#39;s GitHub page on &lt;strong&gt;October 31, 2026&lt;/strong&gt;; this repository gains hundreds of stars a day, so treat those numbers as a dated snapshot, not a permanent fact. Licence: Apache-2.0.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;A framing note before anything else.&lt;/strong&gt; TradingAgents describes itself, in its own documentation, as a framework &quot;designed for research purposes&quot; that &quot;is not intended as financial, investment, or trading advice.&quot; This post holds it to exactly that standard. This is an engineering test — install it, run it, read what it produces — not a recommendation of any stock, strategy, or tool for real money. The framework&#39;s approved orders go to a &lt;em&gt;simulated&lt;/em&gt; exchange. Nothing here is financial advice.&lt;/p&gt;
&lt;h2&gt;The direct answer&lt;/h2&gt;
&lt;p&gt;TradingAgents is a free, open-source Python framework that splits stock analysis across a team of LLM agents modelled on a trading firm: four analysts research in parallel, two researchers debate the evidence bull-versus-bear, a trader proposes a decision, and a risk check plus a portfolio manager can approve or reject it. It installs with a clone, a virtual environment, and one &lt;code&gt;pip install .&lt;/code&gt; — on the machine used for this test that install finished cleanly, with no errors, and the command-line help runs before any API key exists. The one thing you must supply yourself is a language model: a provider API key, a ChatGPT Plus/Pro sign-in, or a local model.&lt;/p&gt;
&lt;p&gt;And the test result, up front, because this series does not bury it: the full pipeline &lt;strong&gt;did run end to end&lt;/strong&gt; — but it took &lt;strong&gt;five attempts&lt;/strong&gt; to get one complete run, and every failure was a lesson (one quirk of my test machine, one dropped connection, and two hard lessons about free-tier API quotas). When it completed, the framework analysed NVIDIA as of October 9, 2026, debated it properly, and issued a decision — &lt;strong&gt;Overweight, buy the dip, with a strict stop-loss&lt;/strong&gt;. The stock then fell straight through that stop within a week. The framework&#39;s directional call was wrong; its risk discipline was the most valuable thing it produced. The full evidence is below.&lt;/p&gt;
&lt;div style=&quot;text-align:center; margin: 24px 0;&quot;&gt;&lt;iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/alakuyGZDNU&quot; title=&quot;Does the 118K-Star AI Trading Agent Actually Work? TradingAgents v0.7.1 Tested&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen&gt;&lt;/iframe&gt;&lt;/div&gt;
&lt;h2&gt;What this test is (and is not)&lt;/h2&gt;
&lt;p&gt;This is the pilot episode of &lt;strong&gt;Open-Source Agent, Tested&lt;/strong&gt;, a series with three fixed rules:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;The version is pinned.&lt;/strong&gt; Every claim below is about v0.7.1 at the commit named above. If the project has released something newer by the time you read this, the post says so rather than quietly drifting.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The run is real.&lt;/strong&gt; The install log, the errors, and the output shown here come from an actual session on an actual machine — not from the project&#39;s documentation. If something fails, the failure is the result and it gets reported as one.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The verdict is honest.&lt;/strong&gt; Star counts measure popularity, not quality. The end of this post separates what is genuinely well-built from what is demo-grade, and says who should bother.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;What it is not: a trading strategy, a signal service, or a promise. See the framing note above.&lt;/p&gt;
&lt;h2&gt;Meet the framework: a firm, not a chatbot&lt;/h2&gt;
&lt;p&gt;Most &quot;AI stock tools&quot; are one model answering one prompt. TradingAgents instead divides the labour the way a trading firm divides it:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Analyst Team&lt;/strong&gt; — four specialists who work in parallel:&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Fundamentals&lt;/em&gt; — the company&#39;s financials.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Sentiment&lt;/em&gt; — news headlines, StockTwits, and Reddit.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;News&lt;/em&gt; — global news and macroeconomic events.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Technical&lt;/em&gt; — chart indicators such as MACD and RSI.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Researcher Team&lt;/strong&gt; — two researchers take the analysts&#39; reports and argue: one builds the bullish case, the other builds the bearish case. The debate is the point — a claim has to survive an opponent before it travels further.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Trader Agent&lt;/strong&gt; — reads the reports and the debate, then composes a concrete proposal: timing and position size.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Risk Management and Portfolio Manager&lt;/strong&gt; — the proposal is checked for volatility and liquidity risk, and the Portfolio Manager makes the final call: approve or reject. Only an approved order reaches the (simulated) exchange.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;That pipeline — specialists gather, opponents argue, a gatekeeper decides — is the design being tested here. Everything else in this post is setup, evidence, and verdict.&lt;/p&gt;
&lt;h2&gt;Environment setup — do this first&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Prerequisites:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Python 3.11 or later.&lt;/strong&gt; This test used Python 3.12.3. Check yours before anything else:&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;code&gt;bash
  python3 --version&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;If it prints 3.10 or lower, install a newer Python first (from python.org, or your OS package manager) — the project&#39;s requirement is 3.11+, and the steps below were verified on 3.12.3.
- &lt;strong&gt;Git&lt;/strong&gt;, to clone the repository.
- &lt;strong&gt;A language-model route&lt;/strong&gt; (covered in its own step below — do not skip it; the analysis cannot run without one).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 1 — clone the repository and pin the tested version:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;git clone https://github.com/TauricResearch/TradingAgents.git
cd TradingAgents
git checkout v0.7.1
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Checking out the tag prints Git&#39;s &quot;detached HEAD&quot; notice. That is informational, not an error — it simply confirms you pinned a release tag instead of following the moving main branch. For a test like this one, the pin is what you want: everyone reading this post can reproduce the exact code.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 2 — create a virtual environment and install:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;python3 -m venv .venv
source .venv/bin/activate
# Windows PowerShell: py -m venv .venv ; .venv\Scripts\Activate.ps1
python -m pip install --upgrade pip
pip install .
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(Prefer &lt;code&gt;uv&lt;/code&gt;? The project also documents &lt;code&gt;uv venv --python 3.13&lt;/code&gt; followed by &lt;code&gt;uv pip install .&lt;/code&gt;. Prefer Docker? It documents &lt;code&gt;docker compose run --rm tradingagents&lt;/code&gt; with your keys in a &lt;code&gt;.env&lt;/code&gt; file. All three routes install the same v0.7.1 code.)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What actually happened on this test machine:&lt;/strong&gt; Python 3.12.3 on Linux, &lt;code&gt;pip install .&lt;/code&gt; completed successfully in roughly four and a half minutes. No errors — none. That is worth stating plainly, because a clean first install is not the open-source default, and this post will not invent friction that did not occur.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 3 — verify the install (no key needed yet):&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;tradingagents --help
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This prints the CLI&#39;s options and its three subcommands (&lt;code&gt;backtest&lt;/code&gt;, &lt;code&gt;login&lt;/code&gt;, &lt;code&gt;logout&lt;/code&gt;). Notably, it runs with &lt;strong&gt;no API key configured&lt;/strong&gt; — the help does not ask for one. The key only becomes necessary when you run an actual analysis, which is the next step.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 4 — give it a language model (zero assumed setup).&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;An analysis run needs an LLM. The framework accepts several routes; pick ONE:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;A provider API key&lt;/strong&gt; — OpenAI, Google, Anthropic, xAI, DeepSeek, DashScope, Zhipu, MiniMax, OpenRouter, Mistral, Moonshot, Groq, NVIDIA, and others are supported. Keys go in a &lt;code&gt;.env&lt;/code&gt; file in the project folder (see below), never in code you commit or share — a key is a password, and anyone who has it can spend your money.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Google / Gemini walkthrough (the route this test used):&lt;/strong&gt;
  1. Go to &lt;strong&gt;aistudio.google.com&lt;/strong&gt; and sign in with a Google account.
  2. Open &lt;strong&gt;Get API key&lt;/strong&gt; and choose &lt;strong&gt;Create API key&lt;/strong&gt; (create or pick a Google Cloud project when prompted).
  3. Copy the key Google shows you. You will not be able to view it again in full later — save it directly into your &lt;code&gt;.env&lt;/code&gt; file (next step), not into a note you might share.
  4. Billing note: this test ran &lt;strong&gt;entirely on the free tier and cost $0&lt;/strong&gt; — but read the run section before assuming free is painless. The Gemini free tier meters requests &lt;strong&gt;per day, per project, per model&lt;/strong&gt; (20 requests/day for gemini-3.8-flash; 15/day for gemini-3.5-flash-lite, at the time of this test), and a single multi-agent analysis makes dozens of model calls. The free tier is a real constraint, not a footnote.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ChatGPT-subscription sign-in (no API key at all)&lt;/strong&gt; — new in v0.7.0: if you already pay for ChatGPT Plus or Pro, run&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;code&gt;bash
  tradingagents login chatgpt&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;and sign in with that plan. (&lt;code&gt;tradingagents logout&lt;/code&gt; revokes the machine&#39;s access again.)
- &lt;strong&gt;A local model&lt;/strong&gt; — an Ollama or other OpenAI-compatible local server works too, if you would rather nothing leaves your machine. Expect slower, weaker analysis from small local models; the framework cannot fix a model that cannot reason.&lt;/p&gt;
&lt;p&gt;Create the &lt;code&gt;.env&lt;/code&gt; file in the project folder with the key(s) for the route you chose, following the variable names in the project&#39;s own &lt;code&gt;.env&lt;/code&gt; example. Keep this file out of Git — it is listed in the project&#39;s ignore rules for a reason.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Step 5 — optional data keys.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Three optional keys unlock extra data sources: &lt;strong&gt;FRED&lt;/strong&gt; (macroeconomic data), &lt;strong&gt;Alpha Vantage&lt;/strong&gt; (market data), and &lt;strong&gt;TypeSafe&lt;/strong&gt; (sentiment screening). None is required for a first run. This test ran &lt;strong&gt;without&lt;/strong&gt; the optional keys, and the effect is visible in the run below: the framework logged that FRED macro data was unavailable and carried on. Add them later, one at a time, so you can tell which source changed the output.&lt;/p&gt;
&lt;h2&gt;How a run works&lt;/h2&gt;
&lt;p&gt;A run is a single command. The flags most people need:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;tradingagents --ticker NVDA --date 2026-09-26 --analysts market,news --save --no-show
&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;--ticker&lt;/code&gt; — the symbol to analyse (for example &lt;code&gt;NVDA&lt;/code&gt;, or &lt;code&gt;0700.HK&lt;/code&gt; for a Hong Kong listing).&lt;/li&gt;
&lt;li&gt;&lt;code&gt;--date&lt;/code&gt; — the analysis date, &lt;code&gt;YYYY-MM-DD&lt;/code&gt;. A past date is the honest way to test: the framework can only use information available up to that date, and you can compare its decision against what the price actually did afterwards.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;--analysts&lt;/code&gt; — a comma-separated subset of the analyst team (for example &lt;code&gt;market,news&lt;/code&gt;); omit it to run the full team.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;--save&lt;/code&gt; / &lt;code&gt;--no-show&lt;/code&gt; — save the full report to disk without interactive prompts; &lt;code&gt;--html&lt;/code&gt; also writes the complete report as a single HTML page, which is the comfortable way to read the whole debate afterwards.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;--portfolio&lt;/code&gt; — a JSON file of current holdings and cash, so the trader, risk, and portfolio agents size decisions against an actual position instead of a blank slate.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;--checkpoint&lt;/code&gt; — saves state after each step so a crashed run can resume instead of starting over. Note this test&#39;s experience in the Errors section: in our session, the checkpoint resume did not pick up and the run restarted from zero. Budget your API requests accordingly.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A separate &lt;code&gt;backtest&lt;/code&gt; subcommand scores past decisions across a grid of tickers and dates.&lt;/p&gt;
&lt;p&gt;Under the hood, one run walks the pipeline from &quot;Meet the framework&quot; in order: analysts in parallel → researcher debate → trader proposal → risk evaluation → portfolio manager&#39;s approve/reject → (simulated) fill. Two details from the v0.7.1 release notes deserve attention, because they are correctness fixes of exactly the kind a hype video skips: decisions are now scored &lt;strong&gt;only on final closing prices&lt;/strong&gt;, settlements are handled across long market holidays, and a model-date bug that could let the system read company filings dated &lt;strong&gt;after&lt;/strong&gt; the run date — in plain terms, peek at the future — is fixed. A framework that patches look-ahead bias is taking backtesting seriously.&lt;/p&gt;
&lt;h2&gt;The test run&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;The protocol, fixed before any output was seen:&lt;/strong&gt; one well-known ticker (&lt;strong&gt;NVDA&lt;/strong&gt;), one analysis date in the past (&lt;strong&gt;Friday, October 9, 2026&lt;/strong&gt; — chosen so the framework only had information available that day, and so its decision could be scored against the real prices that followed), the &lt;strong&gt;full four-analyst team&lt;/strong&gt;, &lt;strong&gt;one researcher debate round and one risk round&lt;/strong&gt;, the complete report saved, and cost and duration measured. Three questions to answer: does it run end to end, does the debate change anything, and what does it cost?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What it actually took: five attempts in 24 minutes.&lt;/strong&gt; The failures are part of the result, and each one is detailed in the Errors section below. In short: attempt 1 died in 7 seconds on a proxy quirk of my test machine; attempt 2 ran ten minutes, completed the analysts and the whole debate, then died at the Research Manager when the Gemini connection dropped; attempt 3 repeated that run and died at the same desk on the free tier&#39;s daily quota (20 requests/day for the deep model); attempt 4 tried a checkpoint resume, which did not resume, and died 33 seconds in on the &lt;em&gt;other&lt;/em&gt; model&#39;s quota (15/day); attempt 5 switched to two untouched free-tier models — &lt;strong&gt;gemini-3.5-flash&lt;/strong&gt; for the deep-thinking agents, &lt;strong&gt;gemini-3.1-flash-lite&lt;/strong&gt; for the quick ones — and &lt;strong&gt;completed the entire pipeline in 107 seconds, exit code 0&lt;/strong&gt;. Everything below is from attempt 5&#39;s saved report. &lt;strong&gt;Measured cost: $0&lt;/strong&gt; — every attempt ran on the free tier. The real currency was quota, not money.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What each stage produced (analysis date Oct 9, 2026; NVDA closed that day at $229.28):&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Market analyst — uptrend intact, momentum cooling.&lt;/strong&gt; Price above the 50-day average ($221.94) and the 200-day ($201.82) says the structure is bullish; but the MACD histogram has narrowed to 0.118, price dipped below the 10-day EMA, and RSI is a neutral 53. Flagged a possible re-test of support at $226.73.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Fundamentals analyst — strongly bullish.&lt;/strong&gt; Quarterly revenue of $96.2 billion, net income of $59.7 billion, over $74 billion of operating cash flow in the first half of the year, $22.4 billion in cash. Its own caveat: growth this large cannot compound forever — watch for deceleration.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Sentiment analyst — neutral, 5.0 out of 10, confidence low — because it had no data.&lt;/strong&gt; For a historical window, its sources (news headlines, StockTwits, Reddit) returned nothing usable — Reddit&#39;s feed was blocked outright (HTTP 403) during these sessions — and the report &lt;strong&gt;says exactly that&lt;/strong&gt; instead of inventing a mood. Honest, and a genuine limitation: sentiment data does not do history, so past-date tests fly blind on this desk.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;News analyst — themes, disclosed gaps.&lt;/strong&gt; Real-time news and macro feeds were unavailable (no FRED key configured; the framework logged the fallback and continued), so the report framed NVDA around AI spending, the Fed&#39;s rate path, supply chains and geopolitics — and disclosed the missing data up front.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The debate — the bear nearly won the room.&lt;/strong&gt; The bull argued the moat (an ecosystem hyperscalers cannot abandon), the fortress balance sheet, and a healthy consolidation above both moving averages. The bear argued a blow-off top: hyperscalers are building their own silicon precisely to break NVIDIA&#39;s pricing power, growth on a $96 billion base is mathematically near-impossible to sustain, capex cycles always turn — and if $226.73 and $221.94 break, momentum selling follows. The &lt;strong&gt;Research Manager sided with the bull&lt;/strong&gt; (financial dominance is current fact; the bear&#39;s case is a future risk) but adopted the bear&#39;s warning levels as the trade&#39;s entry zone and stop.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Trader — BUY.&lt;/strong&gt; Entry $226.73, stop-loss $218.00, size 1.25× standard.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Risk debate — the numbers got moderated.&lt;/strong&gt; The aggressive analyst wanted the full 1.25× (&quot;the real risk is opportunity cost&quot;). The conservative wanted 0.5–0.75× (a stop is only as good as the liquidity that fills it in a fast drop). The neutral proposed the compromise: keep the buy, size at 1.0×, and widen the stop to $215 — at $218, the stop sits less than 1.5× the daily volatility (ATR 5.68) from entry, close enough for ordinary noise to trigger it.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Portfolio Manager — final decision: OVERWEIGHT.&lt;/strong&gt; Enter between &lt;strong&gt;$226.73 and $221.94&lt;/strong&gt;, size &lt;strong&gt;1.0×–1.25×&lt;/strong&gt;, stop-loss &lt;strong&gt;$215–$218&lt;/strong&gt;, horizon &lt;strong&gt;3–6 months&lt;/strong&gt;, &lt;strong&gt;no price target&lt;/strong&gt;. Compare it with the Trader&#39;s proposal and you can see the debate&#39;s fingerprints: both single numbers became ranges — the neutral risk analyst&#39;s compromise, adopted.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;The decision vs. reality.&lt;/strong&gt; NVDA&#39;s actual closes after October 9 (Yahoo Finance data): &lt;strong&gt;$226.32 on Oct 12&lt;/strong&gt; — inside the buy zone, so the entry triggered on the first trading day. &lt;strong&gt;$221.30 on Oct 14&lt;/strong&gt; — below the zone, and below the 50-day average the thesis stood on. &lt;strong&gt;$217.23 on Oct 15&lt;/strong&gt; — inside the decision&#39;s own stop-loss band; a rules-following holder exits there. &lt;strong&gt;$214.13 on Oct 16&lt;/strong&gt; — below the whole band; the exit is complete, at roughly &lt;strong&gt;−4% to −6%&lt;/strong&gt; depending on the entry fill. The slide continued: &lt;strong&gt;$204.64 by Oct 30, −10.7%&lt;/strong&gt; from the analysis-date close. The bear&#39;s fragility warning is almost literally what happened next.&lt;/p&gt;
&lt;p&gt;So the scoreboard is split, and this post reports both halves: &lt;strong&gt;the directional call was wrong&lt;/strong&gt; — the stock broke the support the thesis rested on within four trading days — &lt;strong&gt;and the stop-loss was the most valuable line in the decision&lt;/strong&gt;, because obeying it cut the loss roughly in half versus holding (−4% to −6% out versus −10.7% by month&#39;s end). One run, one ticker, one date: an observation about what this framework did here, not evidence of a repeatable edge or a repeatable failure rate.&lt;/p&gt;
&lt;h2&gt;Errors you may hit&lt;/h2&gt;
&lt;p&gt;Every error below was actually observed during this test (install errors: none — the install was clean), with the exact text where it matters. They fall into three buckets, and the bucket matters: framework behaviour, my machine, and the model provider&#39;s free tier.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Bucket 1 — provider free-tier quotas (the wall you are most likely to hit):&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Daily request quota exhausted — &lt;code&gt;429 RESOURCE_EXHAUSTED&lt;/code&gt;.&lt;/strong&gt; Observed in attempts 3 and 4. The Gemini API free tier allows &lt;strong&gt;20 generate requests per day, per project, per model&lt;/strong&gt; for gemini-3.8-flash, and &lt;strong&gt;15 per day&lt;/strong&gt; for gemini-3.5-flash-lite. Attempt 3&#39;s error named the limit exactly (&quot;Quota exceeded for metric: generativelanguage.googleapis.com/generate_content_free_tier_requests, limit: 20, model: gemini-3.8-flash&quot;) and quoted a retry delay of 52,503 seconds — about 14.6 hours, i.e. &quot;tomorrow.&quot; A multi-agent analysis makes dozens of calls, so a free-tier day can be spent by a single deep run plus retries. Fixes: spread your runs across models (each model has its own bucket — that is what saved attempt 5), wait for the daily reset, or enable a paid tier and watch the per-run cost instead.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Model at capacity — &lt;code&gt;503 UNAVAILABLE&lt;/code&gt;.&lt;/strong&gt; Observed in attempt 3, immediately before the quota error: &quot;This model is currently experiencing high demand. Spikes in demand are usually temporary.&quot; The framework retried the step in a different mode (free text instead of structured output) on its own — good behaviour — but the retry is what met the quota wall. Fix: wait and retry; the framework already does the graceful part for you.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Provider connection drop — &lt;code&gt;RemoteProtocolError: Server disconnected without sending a response.&lt;/code&gt;&lt;/strong&gt; Observed in attempt 2, at the Research Manager, after ten minutes of completed analyst and debate work. The framework&#39;s one automatic free-text retry hit the same drop. Fix: re-run. This is also the practical case for &lt;code&gt;--save&lt;/code&gt; and saved partial reports — attempt 2&#39;s completed stages were on disk, not lost.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Bucket 2 — framework behaviour worth knowing before you budget requests:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Checkpoint resume did not resume (observed, attempt 4).&lt;/strong&gt; The resumed run restarted from the beginning instead of picking up attempt 3&#39;s saved state, and promptly re-spent requests against a fresh quota wall. Treat &lt;code&gt;--checkpoint&lt;/code&gt; as a seatbelt you should still test on your own setup, not as a guarantee — and do not plan a quota-tight session assuming resume will save you.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data vendors fail, and the framework carries on (observed, attempts 2 and 5).&lt;/strong&gt; Yahoo Finance&#39;s news feed timed out twice in attempt 2 (&quot;Vendor &#39;yfinance&#39; unavailable for get_news: Yahoo Finance request failed: Timeout; trying next vendor.&quot;), Reddit&#39;s RSS returned &quot;HTTP Error 403: Blocked&quot; for the sentiment feeds, and the unconfigured FRED source was skipped with a logged note in attempt 5. In every case the run continued with the remaining sources, and the affected analysts disclosed the gap in their reports rather than papering over it. That is the framework behaving &lt;em&gt;well&lt;/em&gt; — but know that a past-date analysis will have a blind sentiment desk.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Bucket 3 — my machine (probably not yours):&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;httpx &lt;code&gt;InvalidURL&lt;/code&gt; crash at startup — &lt;code&gt;ValueError: invalid literal for int() with base 10: &#39;:1]&#39;&lt;/code&gt;.&lt;/strong&gt; Observed in attempt 1; the run died in 7 seconds before any analysis. Cause: this test machine&#39;s proxy environment sets a &lt;code&gt;NO_PROXY&lt;/code&gt; value containing a bracketed IPv6 address, and httpx&#39;s URL parser chokes on it. Framework: innocent. Fix on a similarly proxied machine: clear &lt;code&gt;NO_PROXY&lt;/code&gt;/&lt;code&gt;no_proxy&lt;/code&gt; from the run&#39;s environment. If your machine has no such proxy variables, you will never see this.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;And the basics, verified during setup:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;No LLM provider configured when starting an analysis.&lt;/strong&gt; The help command runs fine without a key, but an unattended analysis start stops with exactly this: &quot;GOOGLE_API_KEY is not set; there is no terminal to ask for it.&quot; (The provider named is whichever one you selected.) Fix: complete Step 4 — add the key to your &lt;code&gt;.env&lt;/code&gt; or finish the ChatGPT sign-in — then re-run.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python older than 3.11.&lt;/strong&gt; The project requires 3.11+. This test ran 3.12.3 throughout, so the old-Python failure itself was not reproduced here; the requirement is the project&#39;s own, stated up front. Fix: install Python 3.11 or newer and rebuild the virtual environment from Step 2.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&quot;Detached HEAD&quot; notice when checking out v0.7.1.&lt;/strong&gt; Observed during this test&#39;s install. It looks like a warning and reads like a problem; it is neither. Git is telling you a tag — a fixed point — is checked out instead of a branch. For reproducing a tested version, that is the goal. No fix needed.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;A provider key in the wrong place.&lt;/strong&gt; Keys belong in the &lt;code&gt;.env&lt;/code&gt; file or your environment — not pasted into a command you later screenshot, and never committed. If a key leaks, revoke it at the provider and issue a new one; treat it like a lost password, because functionally it is one.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;The verdict&lt;/h2&gt;
&lt;p&gt;Judged against the three questions this series fixed before the run:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;1. Did it run end to end? Yes — on the fifth attempt, flawlessly.&lt;/strong&gt; The completed run walked the whole firm — four analysts in parallel (19.9s, 12.4s, 20.6s and 18.5s of wall time on their stages), a real debate, a trader, a three-way risk argument, and a portfolio decision with entry, size, stop and horizon — in &lt;strong&gt;107 seconds&lt;/strong&gt;, unattended, with a complete saved report. The four failures were not the framework&#39;s code: one was my machine&#39;s proxy quirk, one a dropped provider connection, two the free tier&#39;s daily quotas. But &quot;eventually&quot; is part of the grade: a tool you test for free will spend your day in request quotas, and its checkpoint resume did not rescue us when we needed it.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;2. Did the debate change anything? Yes — measurably, in the final numbers.&lt;/strong&gt; The research debate set the direction: the manager heard the bear&#39;s structural case and ruled for the bull, on current financial dominance — while adopting the bear&#39;s break-levels as the entry zone and stop. The risk debate then moderated the trade itself: the Trader&#39;s single 1.25× size and $218 stop came out of the Portfolio Manager as ranges — 1.0×–1.25× and $215–$218 — the neutral analyst&#39;s compromise, adopted verbatim in spirit. The final decision is not the document any single agent proposed. That is the strongest evidence that the multi-agent structure is doing work a single prompt does not.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;3. What did it cost? $0 — and a day&#39;s quota.&lt;/strong&gt; On the free tier, one deep analysis can consume a model&#39;s entire daily allowance (20 requests for the deep model here). Budget accordingly: free is a planning exercise, paid is a per-run line item — and at 107 seconds of pipeline time, the paid version of this test would be cheap in wall-clock terms.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What is genuinely well-built:&lt;/strong&gt; the install (clean, pinned, honest separation between browsing and spending); agents that disclose missing data instead of fabricating it (the sentiment desk&#39;s &quot;we have no data, therefore neutral, low confidence&quot; is the single most trustworthy paragraph in the report); vendor fallbacks that keep a run alive; release hygiene that patches look-ahead bias.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What is demo-grade, today:&lt;/strong&gt; historical sentiment is effectively absent, so past-date tests — the honest kind — run with one desk blind; the news desk without optional data keys works from themes, not facts; checkpoint resume failed its one live test; and the flagship decision this framework produced was directionally wrong within a week, saved from embarrassment only by its own stop-loss. None of that makes it a toy. All of it makes it a research instrument whose output you verify — which is, to be fair, exactly what its authors claim.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Who should bother:&lt;/strong&gt; developers and technically comfortable investors who want to see multi-agent orchestration do real work on a real problem, and who will treat the output as a structured second opinion to be checked — starting with its stop-loss. Who should not: anyone looking for a signal to trade on. The framework&#39;s own documentation told you that before I did.&lt;/p&gt;
&lt;h2&gt;Get the code&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Companion code for this episode&lt;/strong&gt; — the exact commands, environment notes, and run configuration used here, in the channel&#39;s public code repository: https://github.com/sajjad037/visual-studio-learn-code/tree/main/topic-06-tradingagents-tested&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The project under test&lt;/strong&gt; (credit: Tauric Research): https://github.com/TauricResearch/TradingAgents — star it there, not here, if the test convinces you.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Sources&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;TradingAgents repository (repo page: description, framework overview, installation and CLI documentation; star/fork/watcher counts as of October 31, 2026): https://github.com/TauricResearch/TradingAgents&lt;/li&gt;
&lt;li&gt;TradingAgents releases page (v0.7.1 and v0.7.0 release notes — scoring on final closes, long-holiday settlement, model-date fix; ChatGPT-subscription provider, backtest scoring, release wheels): https://github.com/TauricResearch/TradingAgents/releases&lt;/li&gt;
&lt;li&gt;This test&#39;s own install log and run logs (Python 3.12.3; &lt;code&gt;pip install .&lt;/code&gt;; five run attempts with verbatim error text; the completed attempt&#39;s full report bundle), recorded October 31, 2026, summarised in the setup, run and errors sections above.&lt;/li&gt;
&lt;li&gt;Post-run NVDA daily closing prices, Yahoo Finance chart data, fetched October 31, 2026.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;Reminder, in the project&#39;s own spirit and this series&#39; rules: TradingAgents is a research framework. This post is an engineering test report — not financial, investment, or trading advice.&lt;/em&gt;&lt;/p&gt;
&lt;div style=&quot;text-align:center;border:1px solid #e3e6ea;border-radius:8px;padding:18px 14px;margin:28px 0;background:#fbfcfd;&quot;&gt;&lt;h3 style=&quot;margin:0 0 6px;&quot;&gt;Enjoying these tutorials?&lt;/h3&gt;&lt;p style=&quot;margin:0 0 6px;&quot;&gt;Get new Visual Studio Learn posts by email — free, no spam:&lt;/p&gt;&lt;style&gt;
    .followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview {
  display: flex !important;
  flex-direction: column !important;
  justify-content: center !important;
  margin-top: 30px !important;
  padding: clamp(17px, 5%, 40px) clamp(17px, 7%, 50px) !important;
  max-width: none !important;
  border-radius: 6px !important;
  box-shadow: 0 5px 25px rgba(34, 60, 47, 0.25) !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview,
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview *{
  box-sizing: border-box !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-heading {
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-heading h5{
  margin-top: 0 !important;
  margin-bottom: 0 !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field {
  margin-top: 20px !important;
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input {
  width: 100% !important;
  height: 40px !important;
  border-radius: 6px !important;
  border: 2px solid #e9e8e8 !important;
  background-color: #fff;
  outline: none !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input {
  color: #000000 !important;
  font-family: &quot;Montserrat&quot; !important;
  font-size: 14px;
  font-weight: 400;
  line-height: 20px;
  text-align: center;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input::placeholder {
  color: #000000 !important;
  opacity: 1 !important;
}

.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input:-ms-input-placeholder {
  color: #000000 !important;
}

.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input::-ms-input-placeholder {
  color: #000000 !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button {
  margin-top: 10px !important;
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button button {
  width: 100% !important;
  height: 40px !important;
  border: 0 !important;
  border-radius: 6px !important;
  line-height: 0px !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button button:hover {
  cursor: pointer !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .powered-by-line {
  color: #231f20 !important;
  font-family: &quot;Montserrat&quot; !important;
  font-size: 13px !important;
  font-weight: 400 !important;
  line-height: 25px !important;
  text-align: center !important;
  text-decoration: none !important;
  display: flex !important;
  width: 100% !important;
  justify-content: center !important;
  align-items: center !important;
  margin-top: 10px !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .powered-by-line img {
  margin-left: 10px !important;
  height: 1.13em !important;
  max-height: 1.13em !important;
}

  &lt;/style&gt;
  &lt;div class=&quot;followit--follow-form-container&quot; attr-a attr-b attr-c attr-d attr-e attr-f&gt;
    &lt;form data-v-f93f6730=&quot;&quot; action=&quot;https://api.follow.it/subscription-form/SmRpWjEzV1ovU0FOK2R2Y0oxajFBUmtIdW44K1ZiUWZRUm1FZVdpVlRKUHFLSFBKWjE4M1hTSk5yRU5oNmg0RU5zelhuTllNTGU1UmwzRkN0NzZWbnd2YkV5VWFwYnptWFdtekI3eXdwMmhtamtUNit3dlNSblVmRjJxd1JGbjZ8Q05yQWR2eitvYW5HRmNMMzJkbStpM09rNmxXZ0ZjWWhyOERya2VvVXRzZz0=/8&quot; method=&quot;post&quot;&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;form-preview&quot; style=&quot;background-color: rgb(255, 255, 255); border-style: solid; border-width: 1px; border-color: rgb(204, 204, 204); position: relative;&quot;&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;preview-heading&quot;&gt;&lt;h5 data-v-f93f6730=&quot;&quot; style=&quot;font-family: Arial; font-weight: bold; color: rgb(0, 0, 0); font-size: 16px; text-align: center; text-transform: none !important;&quot;&gt;Get new posts by email:&lt;/h5&gt;&lt;/div&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;preview-input-field&quot;&gt;&lt;input data-v-f93f6730=&quot;&quot; type=&quot;email&quot; name=&quot;email&quot; required=&quot;&quot; placeholder=&quot;Enter your email&quot; spellcheck=&quot;false&quot; style=&quot;font-family: Arial; font-weight: normal; color: rgb(0, 0, 0); font-size: 14px; text-align: center; background-color: rgb(255, 255, 255); text-transform: none !important;&quot; /&gt;&lt;/div&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;preview-submit-button&quot;&gt;&lt;button data-v-f93f6730=&quot;&quot; type=&quot;submit&quot; style=&quot;font-family: Arial; font-weight: bold; color: rgb(255, 255, 255); font-size: 16px; text-align: center; background-color: rgb(0, 0, 0); text-transform: none !important;&quot;&gt;Subscribe&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/form&gt;&lt;a href=&quot;https://follow.it&quot; class=&quot;powered-by-line&quot;&gt;Powered by &lt;img src=&quot;https://follow.it/images/colored-logo.svg&quot; alt=&quot;follow.it&quot; height=&quot;17px&quot;/&gt;&lt;/a&gt;
  &lt;/div&gt;&lt;p style=&quot;margin:12px 0 0;font-size:14px;&quot;&gt;Follow along: &lt;a href=&quot;https://www.youtube.com/@VisualStudioLearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/youtube/FF0000&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;YouTube&quot; /&gt; YouTube&lt;/a&gt; · &lt;a href=&quot;https://www.facebook.com/VisualStudio.Learn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/facebook/1877F2&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;Facebook&quot; /&gt; Facebook&lt;/a&gt; · &lt;a href=&quot;https://www.instagram.com/visualstudiolearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/instagram/E4405F&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;Instagram&quot; /&gt; Instagram&lt;/a&gt; · &lt;a href=&quot;https://www.tiktok.com/@visualstudiolearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/tiktok/000000&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;TikTok&quot; /&gt; TikTok&lt;/a&gt; · &lt;a href=&quot;https://www.visualstudiolearn.com/feeds/posts/default?alt=rss&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/rss/FFA500&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;RSS&quot; /&gt; RSS&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.visualstudiolearn.com/feeds/6914977922879264301/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.visualstudiolearn.com/2026/10/does-118k-star-ai-trading-agent.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/6914977922879264301'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/6914977922879264301'/><link rel='alternate' type='text/html' href='http://www.visualstudiolearn.com/2026/10/does-118k-star-ai-trading-agent.html' title='Does the 118K-Star AI Trading Agent Actually Work? I Installed and Tested TradingAgents v0.7.1 — Open-Source Agent, Tested #1'/><author><name>The Loonie Lesson</name><uri>http://www.blogger.com/profile/02243798795439866634</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRY4V9cFLhqMxHWBOWCGBiGUqjan14Vda_dy5rz-C41kApceM-u6jAxS2_6AIRZtGkQeyiQolq1gCeSBhpo573lYq37MLmMGgSKDQLasowic65VoitivYCzqrmUDUqxJuqCTyzah8JuALYtW0Iz7rXIvIcj-ovhVpEuEhQJ6yG1FUQ-A/s1600/loonie-lesson-logo-profile.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://img.youtube.com/vi/alakuyGZDNU/default.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8861905861585113921.post-873346114526941927</id><published>2026-10-29T16:27:21.000-04:00</published><updated>2026-11-22T02:50:52.174-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Azure"/><category scheme="http://www.blogger.com/atom/ns#" term="Cloud"/><category scheme="http://www.blogger.com/atom/ns#" term="FastAPI"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><category scheme="http://www.blogger.com/atom/ns#" term="Tutorials"/><title type='text'>How Do I Deploy a FastAPI App to Azure App Service? Step by Step, From Zero</title><content type='html'>&lt;p&gt;&lt;strong&gt;Versions used in this post — read this first:&lt;/strong&gt; Python 3.12.3 · Azure CLI 2.91.0 · fastapi 0.162.0 · uvicorn 0.54.0 · gunicorn 26.2.0 · uvicorn-worker 0.4.0 · App Service runtime &lt;code&gt;PYTHON:3.12&lt;/code&gt;. Everything runs from one file, &lt;code&gt;main.py&lt;/code&gt;, printed in full below — the exact file in the companion repo. Recorded October 2026; the free-tier limits and prices below are Microsoft&#39;s published figures as of that date.&lt;/p&gt;&lt;h2&gt;The direct answer&lt;/h2&gt;&lt;p&gt;Deploy a FastAPI app to Azure App Service, step by step, on the free tier: put the app in a folder with &lt;code&gt;requirements.txt&lt;/code&gt; at the root, create a resource group, a free (F1) App Service plan, and a Python 3.12 web app, set two app settings, deploy a zip of the code, and — the step most tutorials skip — set the &lt;strong&gt;startup command&lt;/strong&gt;. On Python 3.13 and earlier, App Service auto-detects Django and Flask only; a FastAPI app with no startup command doesn&#39;t error, it silently serves a built-in default page. The startup command is one line, and this post explains every piece of it. At the end your app answers at &lt;a href=&quot;https://vsl-info-api.azurewebsites.net&quot;&gt;https://vsl-info-api.azurewebsites.net&lt;/a&gt; — for $0, with a delete command that removes everything when you&#39;re done.&lt;/p&gt;&lt;h2&gt;The finished thing first&lt;/h2&gt;&lt;p&gt;Before any setup, here is what the finished deployment looks like. One small FastAPI app — an “App Service Info API” — answering from Azure at &lt;a href=&quot;https://vsl-info-api.azurewebsites.net&quot;&gt;https://vsl-info-api.azurewebsites.net&lt;/a&gt;:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;https://vsl-info-api.azurewebsites.net/&quot;&gt;https://vsl-info-api.azurewebsites.net/&lt;/a&gt; — a hello message plus &lt;strong&gt;where the code is running&lt;/strong&gt;: &lt;code&gt;environment: &quot;production&quot;&lt;/code&gt;, &lt;code&gt;hostname: &quot;vsl-info-api.azurewebsites.net&quot;&lt;/code&gt;. The same code on your own machine answers &lt;code&gt;&quot;local&quot;&lt;/code&gt; — that before/after contrast is the whole demo.&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://vsl-info-api.azurewebsites.net/greet/YourName&quot;&gt;https://vsl-info-api.azurewebsites.net/greet/YourName&lt;/a&gt; — a personalised greeting, proving routing and path parameters survived the trip.&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://vsl-info-api.azurewebsites.net/health&quot;&gt;https://vsl-info-api.azurewebsites.net/health&lt;/a&gt; — &lt;code&gt;status: &quot;ok&quot;&lt;/code&gt; plus the Python version and current UTC time, the endpoint you hit to prove the app is really up.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;The deploy itself took about 2 minutes 40 seconds from zip upload to live answers, on the free F1 plan. Now, rewind.&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhzgHu6zkO5XmaAhOBAW1UL_5eDQ3y2FoJ8i7RplrxHCFAb19i6aKwLbQ5lZhRUv7nuPGcY1VlAPEzBYwl9E19C66dwJ9jqFV1ArJQr_16H7dwPwt2TEwEmJYOA_U7FWlrfry6syRX7iUz-F-cWKUAmi5BoFfzZR08hF6YjgkuYlUoFLd_uuvYLT3kKbCQ/s1919/portal-overview.png&quot; style=&quot;display: block; padding: 1em 0; text-align: center; &quot;&gt;&lt;img alt=&quot;Azure portal overview of the vsl-info-api web app, status Running, on the free F1 plan&quot; border=&quot;0&quot; width=&quot;400&quot; data-original-height=&quot;998&quot; data-original-width=&quot;1919&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhzgHu6zkO5XmaAhOBAW1UL_5eDQ3y2FoJ8i7RplrxHCFAb19i6aKwLbQ5lZhRUv7nuPGcY1VlAPEzBYwl9E19C66dwJ9jqFV1ArJQr_16H7dwPwt2TEwEmJYOA_U7FWlrfry6syRX7iUz-F-cWKUAmi5BoFfzZR08hF6YjgkuYlUoFLd_uuvYLT3kKbCQ/s400/portal-overview.png&quot;/&gt;&lt;/a&gt;&lt;/div&gt;&lt;p style=&quot;text-align: center; font-size: 14px;&quot;&gt;Azure portal overview of the vsl-info-api web app, status Running, on the free F1 plan.&lt;/p&gt;&lt;h2&gt;What you will build&lt;/h2&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;A deployment-check API&lt;/strong&gt; — one file, &lt;code&gt;main.py&lt;/code&gt;, three endpoints, no database, no secrets. Nothing to leak on a screenshot.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;The deploy&lt;/strong&gt; — resource group, free plan, web app, app settings, zip deploy, startup command, verification from the browser and the log stream.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;The shortcut&lt;/strong&gt; — &lt;code&gt;az webapp up&lt;/code&gt;, the one command that does the whole creation-and-deploy in one go, shown after the long way so you know what it did.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;The cleanup&lt;/strong&gt; — one command that deletes everything, and the honest note that this post&#39;s own resources were deleted after filming.&lt;/li&gt;&lt;/ul&gt;&lt;h2&gt;Environment setup — do this first&lt;/h2&gt;&lt;p&gt;&lt;strong&gt;Prerequisites:&lt;/strong&gt; Python 3.12 (this post uses 3.12.3), a terminal, and an Azure account. The free account is enough; everything here runs on the free F1 tier and costs $0.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Create the Azure free account, click by click:&lt;/strong&gt;&lt;/p&gt;&lt;ol&gt;&lt;li&gt;Go to &lt;strong&gt;azure.microsoft.com/free&lt;/strong&gt; and choose &lt;strong&gt;Try Azure for free&lt;/strong&gt;.&lt;/li&gt;&lt;li&gt;Sign in with a &lt;strong&gt;Microsoft account or a GitHub account&lt;/strong&gt; (create one if you don&#39;t have one).&lt;/li&gt;&lt;li&gt;Fill in the profile details.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Identity verification:&lt;/strong&gt; a &lt;strong&gt;phone number&lt;/strong&gt; and a &lt;strong&gt;credit card or non-prepaid debit card&lt;/strong&gt;. The card is only checked for identity — a &lt;strong&gt;~$1 temporary authorization hold&lt;/strong&gt; may appear and is removed. You are charged nothing unless you later choose pay-as-you-go pricing.&lt;/li&gt;&lt;li&gt;Accept the agreement and finish.&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;The offer, as Microsoft documents it: a &lt;strong&gt;US$200 credit for your first 30 days&lt;/strong&gt;, plus amounts of popular services free for 12 months (and some always free). If you don&#39;t upgrade within 30 days, or when the credit runs out, the account and services are disabled until you do — nothing keeps billing in the background. This tutorial never needs the credit at all: the F1 plan is $0, and the free account exists here only as the door in.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Install the Azure CLI, then sign in:&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Install the Azure CLI for your operating system from Microsoft&#39;s install page (learn.microsoft.com — “Install the Azure CLI”), then verify and sign in:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;az --version        # expect azure-cli 2.91.0
az login            # opens a browser sign-in — complete it there
az account show     # read this output: it names the subscription
                    # you are actually about to deploy to&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Two optional checks while you&#39;re here. The authoritative list of Python runtimes App Service currently offers is a command, not a page — run it and note that &lt;code&gt;PYTHON:3.12&lt;/code&gt; is in it:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;az webapp list-runtimes --os-type linux --runtime python --output table&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;(At this post&#39;s production run, on October 28, 2026, the Python rows of that output were &lt;code&gt;PYTHON|3.15&lt;/code&gt;, &lt;code&gt;PYTHON|3.14&lt;/code&gt;, &lt;code&gt;PYTHON|3.13&lt;/code&gt;, &lt;code&gt;PYTHON|3.12&lt;/code&gt;, and &lt;code&gt;PYTHON|3.11&lt;/code&gt;, all Active, plus &lt;code&gt;PYTHON|3.10&lt;/code&gt;, marked Near end of life (support ends 2026-10-31). The list moves — run the command rather than trusting any printed copy, including this one.)&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Create the Python environment and run the app locally first:&lt;/strong&gt;&lt;/p&gt;&lt;pre&gt;&lt;code&gt;mkdir fastapi-info &amp;amp;&amp;amp; cd fastapi-info
python3 -m venv .venv &amp;amp;&amp;amp; source .venv/bin/activate
# Windows PowerShell: py -m venv .venv ; .venv\Scripts\Activate.ps1
pip install -r requirements.txt&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;(The code goes in this folder as &lt;code&gt;main.py&lt;/code&gt; and &lt;code&gt;requirements.txt&lt;/code&gt; — both printed in full below, and the companion repo has them as ready-made files.)&lt;/p&gt;&lt;pre&gt;&lt;code&gt;uvicorn main:app --reload&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Open http://127.0.0.1:8000/ — it answers &lt;code&gt;&quot;environment&quot;: &quot;local&quot;&lt;/code&gt;, &lt;code&gt;&quot;hostname&quot;: &quot;localhost&quot;&lt;/code&gt;. Try &lt;code&gt;/health&lt;/code&gt; and &lt;code&gt;/greet/YourName&lt;/code&gt; too. &lt;strong&gt;Do not skip the local run&lt;/strong&gt;: it separates “my code is broken” from “my deploy is broken” before Azure is involved. The local run for this post, real output:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;{&quot;app&quot;:&quot;App Service Info API&quot;,&quot;message&quot;:&quot;Hello from FastAPI&quot;,&quot;environment&quot;:&quot;local&quot;,&quot;hostname&quot;:&quot;localhost&quot;}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;Verify-it-works checkpoint:&lt;/strong&gt; local app answers at &lt;code&gt;http://127.0.0.1:8000/&lt;/code&gt;, and &lt;code&gt;az account show&lt;/code&gt; names the subscription you intend to use. Both on screen before you touch Azure&#39;s portal.&lt;/p&gt;&lt;h2&gt;The app — one file, three endpoints&lt;/h2&gt;&lt;pre&gt;&lt;code&gt;&quot;&quot;&quot;App Service Info API: the smallest app that proves a deploy worked.

Companion code for the Visual Studio Learn post and video
&quot;How Do I Deploy a FastAPI App to Azure App Service? Step by Step,
From Zero&quot;.

This one-file FastAPI app exists to make deployment concepts visible:

- GET /        reports where the code is running. It reads the
               APP_ENVIRONMENT app setting (which we set to
               &quot;production&quot; in Azure) and WEBSITE_HOSTNAME, a variable
               App Service defines automatically. Locally, with neither
               set, the same code answers &quot;local&quot; — the before/after
               contrast is the whole demo.
- GET /health  status, the Python version, and the current UTC time:
               the endpoint you hit to prove the app is really up.
- GET /greet/{name}
               a personalised greeting — proof that routing and path
               parameters survived the trip to the cloud.

Requires: Python 3.10 or newer and the packages in requirements.txt.
Run locally:  uvicorn main:app --reload
In Azure it is served by Gunicorn with the Uvicorn worker class — see
startup.txt, which holds the exact command the post sets in App
Service.
&quot;&quot;&quot;

import logging
import os
import platform
from datetime import datetime, timezone

from fastapi import FastAPI

logger = logging.getLogger(&quot;info_api&quot;)
logging.basicConfig(level=logging.INFO, format=&quot;%(levelname)s %(message)s&quot;)

app = FastAPI(title=&quot;App Service Info API&quot;)


def environment() -&amp;gt; str:
    &quot;&quot;&quot;Where this process believes it is running.

    APP_ENVIRONMENT is an ordinary app setting: unset locally (so the
    default &quot;local&quot; answers), set to &quot;production&quot; in Azure.
    &quot;&quot;&quot;
    return os.environ.get(&quot;APP_ENVIRONMENT&quot;, &quot;local&quot;)


def hostname() -&amp;gt; str:
    &quot;&quot;&quot;The site&#39;s public hostname, when the platform provides one.

    App Service auto-defines WEBSITE_HOSTNAME for every web app.
    Locally it does not exist, so the honest answer is &quot;localhost&quot;.
    &quot;&quot;&quot;
    return os.environ.get(&quot;WEBSITE_HOSTNAME&quot;, &quot;localhost&quot;)


@app.get(&quot;/&quot;)
def read_root() -&amp;gt; dict:
    &quot;&quot;&quot;Say hello, and say where from — the deploy&#39;s proof page.&quot;&quot;&quot;
    return {
        &quot;app&quot;: &quot;App Service Info API&quot;,
        &quot;message&quot;: &quot;Hello from FastAPI&quot;,
        &quot;environment&quot;: environment(),
        &quot;hostname&quot;: hostname(),
    }


@app.get(&quot;/health&quot;)
def health() -&amp;gt; dict:
    &quot;&quot;&quot;Liveness plus runtime identity: status, Python version, UTC now.&quot;&quot;&quot;
    return {
        &quot;status&quot;: &quot;ok&quot;,
        &quot;python&quot;: platform.python_version(),
        &quot;utc_time&quot;: datetime.now(timezone.utc).isoformat(),
    }


@app.get(&quot;/greet/{name}&quot;)
def greet(name: str) -&amp;gt; dict:
    &quot;&quot;&quot;A greeting with the visitor&#39;s name — routing, proven live.&quot;&quot;&quot;
    return {
        &quot;greeting&quot;: f&quot;Hello, {name}!&quot;,
        &quot;environment&quot;: environment(),
        &quot;hostname&quot;: hostname(),
    }&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Two design choices to notice. The app reads &lt;strong&gt;app settings as ordinary environment variables&lt;/strong&gt; — that is how App Service configuration reaches code, and it is why setting &lt;code&gt;APP_ENVIRONMENT&lt;/code&gt; later flips the answer. And it has &lt;strong&gt;no database, no key, no secret&lt;/strong&gt;: the demo&#39;s only moving parts are the deploy itself.&lt;/p&gt;&lt;h2&gt;The two files Azure cares about&lt;/h2&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;requirements.txt&lt;/code&gt; — at the project root, or the build fails:&lt;/strong&gt;&lt;/p&gt;&lt;pre&gt;&lt;code&gt;fastapi==0.162.0
uvicorn==0.54.0
gunicorn==26.2.0
uvicorn-worker==0.4.0&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;App Service&#39;s build looks for the dependency file at the root of the project (or the zip). Without it, the build reports: &lt;em&gt;“Could not find setup.py or requirements.txt; Not running pip install.”&lt;/em&gt; That error text, verbatim, is the giveaway that the file is in the wrong place.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;startup.txt&lt;/code&gt; — the startup command, the step most tutorials skip:&lt;/strong&gt;&lt;/p&gt;&lt;pre&gt;&lt;code&gt;gunicorn --bind=0.0.0.0:8000 --timeout 600 --workers 2 -k uvicorn_worker.UvicornWorker main:app --access-logfile &#39;-&#39; --error-logfile &#39;-&#39;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Piece by piece, because each one is a decision:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Why Gunicorn at all?&lt;/strong&gt; App Service&#39;s built-in Python container already runs apps under Gunicorn, behind an Nginx reverse proxy. The startup command customises that, it doesn&#39;t replace the platform.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Why can&#39;t Gunicorn run FastAPI alone?&lt;/strong&gt; Its default worker is a synchronous WSGI worker. Pointed at an ASGI app, it fails in the log stream with &lt;code&gt;TypeError: FastAPI.__call__() missing 1 required positional argument: &#39;send&#39;&lt;/code&gt;. The fix is the &lt;strong&gt;Uvicorn worker class&lt;/strong&gt;, which speaks ASGI.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Why &lt;code&gt;uvicorn_worker.UvicornWorker&lt;/code&gt;, not the &lt;code&gt;uvicorn.workers&lt;/code&gt; class older tutorials show?&lt;/strong&gt; In uvicorn 0.54.0 the &lt;code&gt;uvicorn.workers&lt;/code&gt; module is deprecated — it still runs, but it logs a deprecation warning pointing at the new &lt;code&gt;uvicorn-worker&lt;/code&gt; package. This post uses the new class; the old one is the fallback an older tutorial&#39;s command will show you.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Why port 8000?&lt;/strong&gt; The built-in Python image exposes port 8000 by default. The app must listen there.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Why 2 workers, not the 4 many tutorials show?&lt;/strong&gt; The free F1 plan has 1 GB of RAM shared across the plan. Two workers leave headroom; four would not teach you anything extra.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;What is &lt;em&gt;not&lt;/em&gt; in this command, deliberately: &lt;code&gt;WEBSITES_PORT&lt;/code&gt;.&lt;/strong&gt; That app setting is for custom containers, not the built-in images — it has no effect inside the container, and setting it is cargo-cult configuration this post corrects on sight.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;And the callout that decides the whole walkthrough: &lt;strong&gt;on Python 3.14 and later, App Service auto-detects and runs FastAPI apps with no startup command at all.&lt;/strong&gt; We pin Python 3.12 anyway, because 3.12 is a currently documented runtime (Microsoft&#39;s own PostgreSQL tutorials run on it) and because the startup command is the skill every existing app still needs. If you deploy on 3.14, skip the startup-command step — and know that skipping it is the platform&#39;s new behaviour, not a shortcut you invented.&lt;/p&gt;&lt;h2&gt;Step 1 — Create the resources (portal and CLI, side by side)&lt;/h2&gt;&lt;p&gt;&lt;strong&gt;The names below are the exact ones from this post&#39;s own run&lt;/strong&gt; — resource group &lt;code&gt;rg-vsl-topic05-cus&lt;/code&gt;, plan &lt;code&gt;plan-vsl-topic05&lt;/code&gt;, app &lt;code&gt;vsl-info-api&lt;/code&gt;. Pick your own equivalents; the app name in particular must be globally unique across Azure, because it becomes your public address.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Portal:&lt;/strong&gt; Azure portal → &lt;strong&gt;Create a resource&lt;/strong&gt; → &lt;strong&gt;Web App&lt;/strong&gt;. Resource group: create new, &lt;code&gt;rg-vsl-topic05-cus&lt;/code&gt;. Region: &lt;strong&gt;Central US&lt;/strong&gt; — on a brand-new free account the region is not a free choice; see the quota note under the CLI commands below. Publishing model: &lt;strong&gt;Code&lt;/strong&gt;. Runtime stack: &lt;strong&gt;Python 3.12&lt;/strong&gt;. Operating system: &lt;strong&gt;Linux&lt;/strong&gt; (Python on App Service runs on Linux only — Windows Python is no longer supported). Pricing plan: create new, and choose the &lt;strong&gt;Free (F1)&lt;/strong&gt; tier. Review + create.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;CLI — the same resources, in three commands:&lt;/strong&gt;&lt;/p&gt;&lt;pre&gt;&lt;code&gt;# A resource group to hold everything (delete the group = delete all)
az group create --name rg-vsl-topic05-cus --location centralus

# The FREE plan — the --sku F1 part is what keeps this at $0
az appservice plan create --name plan-vsl-topic05 \
  --resource-group rg-vsl-topic05-cus --sku F1 --is-linux \
  --location centralus

# The web app on Python 3.12 (the name must be globally unique;
# the app&#39;s public address becomes &amp;lt;name&amp;gt;.azurewebsites.net)
az webapp create --name vsl-info-api --resource-group rg-vsl-topic05-cus \
  --plan plan-vsl-topic05 --runtime &quot;PYTHON:3.12&quot;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Confirm the runtime landed where you think it did:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;az webapp config show --name vsl-info-api --resource-group rg-vsl-topic05-cus \
  --query linuxFxVersion&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;The region note, from this post&#39;s own run — read it before you pick a region.&lt;/strong&gt; This walkthrough was produced on a brand-new free-trial account, and creating the F1 plan failed in &lt;strong&gt;Canada Central&lt;/strong&gt;, &lt;strong&gt;East US&lt;/strong&gt;, and &lt;strong&gt;West US 2&lt;/strong&gt;: the account&#39;s free-tier quota there was 0, and the error said so. &lt;strong&gt;Central US&lt;/strong&gt; had quota and accepted the plan immediately. If your region refuses the free plan, you have two free moves: switch regions (Central US worked here), or request the free quota increase for your subscription. What not to do is “fix” it by choosing a paid tier — the quota is an account-youth quirk, not a price of entry.&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNW0wLjesxcsRGJzn4o3LDW-m2aDhFF6XLCgtNvFTO9jKW00pCU80VDHN0S-ejB8H6xOQcvf-YOKbhEYaGVmiYf5EB-BRFLVq-IsQCXe1COhzLgop332zvezo_eYx7ADg0W82Fa_DkE7mRIfTvGVYI35Oz2VZfgMEQOnT4RfSyDvPKwj6XGV3Ys7xcNKY/s1919/portal-rg.png&quot; style=&quot;display: block; padding: 1em 0; text-align: center; &quot;&gt;&lt;img alt=&quot;The rg-vsl-topic05-cus resource group holding the plan and the web app&quot; border=&quot;0&quot; width=&quot;400&quot; data-original-height=&quot;998&quot; data-original-width=&quot;1919&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNW0wLjesxcsRGJzn4o3LDW-m2aDhFF6XLCgtNvFTO9jKW00pCU80VDHN0S-ejB8H6xOQcvf-YOKbhEYaGVmiYf5EB-BRFLVq-IsQCXe1COhzLgop332zvezo_eYx7ADg0W82Fa_DkE7mRIfTvGVYI35Oz2VZfgMEQOnT4RfSyDvPKwj6XGV3Ys7xcNKY/s400/portal-rg.png&quot;/&gt;&lt;/a&gt;&lt;/div&gt;&lt;p style=&quot;text-align: center; font-size: 14px;&quot;&gt;The rg-vsl-topic05-cus resource group holding the plan and the web app.&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdVCH5vBofy1KdB_V5hiJN3md6Mjoc2-X2iFj5dMOdBAAR8vrRuijnhvWOyORN47xwY2HY8wFAMRm0TqRpHL4_I7gkCNbyk7yCb3b_H56y00n2HOzUwQlIQMoWsWDN0ygoRYJSp4vlbVfVHm5qeq5RWPmf3J452Yw8YLlGwoKbOAc31XXTqFB4r9hlltw/s1919/portal-plan.png&quot; style=&quot;display: block; padding: 1em 0; text-align: center; &quot;&gt;&lt;img alt=&quot;The plan-vsl-topic05 App Service plan page showing the Free F1 pricing tier&quot; border=&quot;0&quot; width=&quot;400&quot; data-original-height=&quot;998&quot; data-original-width=&quot;1919&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdVCH5vBofy1KdB_V5hiJN3md6Mjoc2-X2iFj5dMOdBAAR8vrRuijnhvWOyORN47xwY2HY8wFAMRm0TqRpHL4_I7gkCNbyk7yCb3b_H56y00n2HOzUwQlIQMoWsWDN0ygoRYJSp4vlbVfVHm5qeq5RWPmf3J452Yw8YLlGwoKbOAc31XXTqFB4r9hlltw/s400/portal-plan.png&quot;/&gt;&lt;/a&gt;&lt;/div&gt;&lt;p style=&quot;text-align: center; font-size: 14px;&quot;&gt;The plan-vsl-topic05 App Service plan page showing the Free F1 pricing tier.&lt;/p&gt;&lt;h2&gt;Step 2 — App settings&lt;/h2&gt;&lt;p&gt;Two settings, both set before the deploy:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;az webapp config appsettings set --name vsl-info-api \
  --resource-group rg-vsl-topic05-cus \
  --settings SCM_DO_BUILD_DURING_DEPLOYMENT=true APP_ENVIRONMENT=production&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;(Portal: your web app → &lt;strong&gt;Settings&lt;/strong&gt; → &lt;strong&gt;Environment variables&lt;/strong&gt; → &lt;strong&gt;App settings&lt;/strong&gt; tab, add both, &lt;strong&gt;Save&lt;/strong&gt;. Older portal versions called this &lt;strong&gt;Configuration → Application settings&lt;/strong&gt;.)&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;&lt;code&gt;SCM_DO_BUILD_DURING_DEPLOYMENT=true&lt;/code&gt;&lt;/strong&gt; tells the deployment to run the Oryx build: activate a virtual environment and &lt;code&gt;pip install&lt;/code&gt; requirements.txt. A raw zip deploy must set this itself — without it, the classic symptom is &lt;code&gt;ModuleNotFoundError&lt;/code&gt; at startup, because the packages were never installed.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;&lt;code&gt;APP_ENVIRONMENT=production&lt;/code&gt;&lt;/strong&gt; is the &lt;code&gt;/&lt;/code&gt; endpoint&#39;s proof: it is what flips the local answer to the Azure one.&lt;/li&gt;&lt;/ul&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgsAQ3v0V6DjAiBQOP49JzfzwITdSgC3gINe3ji6x1Rz6CWptLqVECUijCiAwitOToYHWepzatcllOnMfDng-6J2MMHvFNaBEqkBJF6rFOA4z7gD8peff_K_ya6fC6_tBjJ2TLwM6e9rryWoVINpurW2aJPnUmGkiZWd_HHZ-j9DUvQJpx2AOEF3MjJS20/s1919/portal-appsettings.png&quot; style=&quot;display: block; padding: 1em 0; text-align: center; &quot;&gt;&lt;img alt=&quot;App settings showing APP_ENVIRONMENT set to production&quot; border=&quot;0&quot; width=&quot;400&quot; data-original-height=&quot;998&quot; data-original-width=&quot;1919&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgsAQ3v0V6DjAiBQOP49JzfzwITdSgC3gINe3ji6x1Rz6CWptLqVECUijCiAwitOToYHWepzatcllOnMfDng-6J2MMHvFNaBEqkBJF6rFOA4z7gD8peff_K_ya6fC6_tBjJ2TLwM6e9rryWoVINpurW2aJPnUmGkiZWd_HHZ-j9DUvQJpx2AOEF3MjJS20/s400/portal-appsettings.png&quot;/&gt;&lt;/a&gt;&lt;/div&gt;&lt;p style=&quot;text-align: center; font-size: 14px;&quot;&gt;App settings showing APP_ENVIRONMENT set to production.&lt;/p&gt;&lt;h2&gt;Step 3 — Deploy the zip&lt;/h2&gt;&lt;p&gt;Build the zip with the dependency file at the archive root — and &lt;strong&gt;no &lt;code&gt;.venv&lt;/code&gt;, no &lt;code&gt;.git&lt;/code&gt;, no &lt;code&gt;.env&lt;/code&gt; inside it&lt;/strong&gt;. Deploying your local virtual environment is the most common self-inflicted deploy failure in this whole walkthrough:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;zip deploy.zip main.py requirements.txt startup.txt
az webapp deploy --name vsl-info-api --resource-group rg-vsl-topic05-cus \
  --type zip --src-path deploy.zip&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Deployment can take a few minutes; the production run for this post took about 2 minutes 40 seconds.&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6Azrz8wIzwdxveOtHSo6qy7BcPyDT3A7tW3O4YCb89GMK5HS4xM5ML9Hsx3VLSPJyvmy11HBtQSOaKCQTiUVzf_clS1VpVilrNbG2FcyTu7_036CKK6B_2EgELDv1jQS5IfnjlOIlydvvOA6i0Ir8MOONIHJiFJH2ml5kQlHABHblclD0NFNs3roJYzU/s1919/portal-deploy.png&quot; style=&quot;display: block; padding: 1em 0; text-align: center; &quot;&gt;&lt;img alt=&quot;Deployment Center log showing the zip deployment succeeded&quot; border=&quot;0&quot; width=&quot;400&quot; data-original-height=&quot;998&quot; data-original-width=&quot;1919&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6Azrz8wIzwdxveOtHSo6qy7BcPyDT3A7tW3O4YCb89GMK5HS4xM5ML9Hsx3VLSPJyvmy11HBtQSOaKCQTiUVzf_clS1VpVilrNbG2FcyTu7_036CKK6B_2EgELDv1jQS5IfnjlOIlydvvOA6i0Ir8MOONIHJiFJH2ml5kQlHABHblclD0NFNs3roJYzU/s400/portal-deploy.png&quot;/&gt;&lt;/a&gt;&lt;/div&gt;&lt;p style=&quot;text-align: center; font-size: 14px;&quot;&gt;Deployment Center log showing the zip deployment succeeded.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Then set the startup command before you judge the result:&lt;/strong&gt; portal → your web app → &lt;strong&gt;Configuration&lt;/strong&gt; → &lt;strong&gt;Stack settings&lt;/strong&gt; tab → &lt;strong&gt;Startup Command&lt;/strong&gt;, paste the line from &lt;code&gt;startup.txt&lt;/code&gt;, &lt;strong&gt;Save&lt;/strong&gt;, then restart the app and wait ~20 seconds. (The &lt;strong&gt;General settings&lt;/strong&gt; tab next to it holds platform and debugging options — the startup command is not there.)&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgdIyBLtKgCz1x90_kyZUsH9th6aRqA63KUAKA0sS6KYiad4yRymbaRQyqqjEzvAvtMcnb1UOOK07THZzHPhx-RvIjtICYY0ao4WHEJrQAEIxIXhiajwtnWAiroH7jMFtizf5AJrrurEqdlXsMWQybZRFWArgfbAVd_Fm9DbxaDjYhEi3Yubw_9wq0J-pg/s1919/portal-config.png&quot; style=&quot;display: block; padding: 1em 0; text-align: center; &quot;&gt;&lt;img alt=&quot;Configuration, Stack settings tab, with the gunicorn startup command for FastAPI&quot; border=&quot;0&quot; width=&quot;400&quot; data-original-height=&quot;998&quot; data-original-width=&quot;1919&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgdIyBLtKgCz1x90_kyZUsH9th6aRqA63KUAKA0sS6KYiad4yRymbaRQyqqjEzvAvtMcnb1UOOK07THZzHPhx-RvIjtICYY0ao4WHEJrQAEIxIXhiajwtnWAiroH7jMFtizf5AJrrurEqdlXsMWQybZRFWArgfbAVd_Fm9DbxaDjYhEi3Yubw_9wq0J-pg/s400/portal-config.png&quot;/&gt;&lt;/a&gt;&lt;/div&gt;&lt;p style=&quot;text-align: center; font-size: 14px;&quot;&gt;Configuration, Stack settings tab, with the gunicorn startup command for FastAPI.&lt;/p&gt;&lt;h2&gt;Step 4 — The staged failure (deliberate, and the point of the post)&lt;/h2&gt;&lt;p&gt;Before the startup command was set, the first deploy of this exact app did not error. It served the platform&#39;s built-in default page — the generic one titled &lt;strong&gt;“Microsoft Azure App Service - Welcome”&lt;/strong&gt; — because App Service auto-detects Django and Flask only, and a FastAPI app with no startup command falls through to the platform&#39;s read-only default app. We reproduced this on purpose during production, by clearing the startup command on the working app and restarting it: same deploy, same code, default page — then restored the command and watched the real app come back. If you see a default page after a deploy that reported success, you have not failed: you have reproduced the platform&#39;s documented behaviour, and the fix is the startup command plus a restart.&lt;/p&gt;&lt;p&gt;After the restart, the same URL answers the real app. Verify like an engineer — all three endpoints, in the browser:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;https://vsl-info-api.azurewebsites.net/&quot;&gt;https://vsl-info-api.azurewebsites.net/&lt;/a&gt; now reports &lt;code&gt;&quot;environment&quot;: &quot;production&quot;&lt;/code&gt; and &lt;code&gt;&quot;hostname&quot;: &quot;vsl-info-api.azurewebsites.net&quot;&lt;/code&gt; — the local &lt;code&gt;&quot;local&quot;&lt;/code&gt;/&lt;code&gt;&quot;localhost&quot;&lt;/code&gt; answer, replaced by the Azure one, from the same code.&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://vsl-info-api.azurewebsites.net/greet/YourName&quot;&gt;https://vsl-info-api.azurewebsites.net/greet/YourName&lt;/a&gt; — your greeting, from Azure.&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;https://vsl-info-api.azurewebsites.net/health&quot;&gt;https://vsl-info-api.azurewebsites.net/health&lt;/a&gt; — &lt;code&gt;status: &quot;ok&quot;&lt;/code&gt;, Python 3.12.15 (the patch level the &lt;code&gt;PYTHON:3.12&lt;/code&gt; image currently runs — your local 3.12.x and Azure&#39;s are the same 3.12 line, not necessarily the same patch), the current UTC time.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Then read the log stream while you hit the app, so the deploy stops being a black box:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;az webapp log config --name vsl-info-api --resource-group rg-vsl-topic05-cus
az webapp log tail --name vsl-info-api --resource-group rg-vsl-topic05-cus&lt;/code&gt;&lt;/pre&gt;&lt;h2&gt;The shortcut — &lt;code&gt;az webapp up&lt;/code&gt;&lt;/h2&gt;&lt;p&gt;Everything above, in one command, for a fresh folder:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;az webapp up&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;az webapp up&lt;/code&gt; is a current, generally available Azure CLI command: it creates the resource group, plan, and web app and deploys the code from your local folder, and Python apps are created as Linux apps. Use it when you already understand the steps — the long way above is the version that teaches you what the shortcut did, including which startup command and app settings it leaves you still needing to set yourself.&lt;/p&gt;&lt;h2&gt;Errors you may hit&lt;/h2&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;The site shows a default/placeholder page, not your app&lt;/strong&gt; — the page titled “Microsoft Azure App Service - Welcome”. FastAPI wasn&#39;t detected (no startup command on Python 3.13 or earlier) and App Service ran its built-in default app. Fix: set the startup command, restart, wait ~20 seconds. One related trap, learned on this post&#39;s own run: &lt;code&gt;az webapp config set --startup-file &quot;&quot;&lt;/code&gt; looks like it clears the startup command but silently leaves the old one in place — to actually clear it, use the portal&#39;s Configuration page or the management API.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;&lt;code&gt;TypeError: FastAPI.__call__() missing 1 required positional argument: &#39;send&#39;&lt;/code&gt;&lt;/strong&gt; in the log stream — Gunicorn&#39;s default sync worker met an ASGI app. Fix: the Uvicorn worker class in the startup command (Step: the two files Azure cares about). This exact error is why that class is in the command.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;“Could not find setup.py or requirements.txt; Not running pip install.”&lt;/strong&gt; — the dependency file is not at the project root / zip root. Fix: move it to the root and redeploy.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;&lt;code&gt;ModuleNotFoundError&lt;/code&gt; at startup&lt;/strong&gt; — the packages were never installed, usually because the build never ran or a local venv was deployed with the code. Fix: set &lt;code&gt;SCM_DO_BUILD_DURING_DEPLOYMENT=true&lt;/code&gt; and redeploy; never ship your venv.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;“Service Unavailable” / the first hit hangs&lt;/strong&gt; — two possible causes. The mild one is a cold start: F1 has no Always On, the app unloads when idle, and Microsoft&#39;s docs warn the app “might take longer to start up when you use free tiers… and become responsive after you refresh”. The serious one is Gunicorn up but the app not answering — check the log stream before changing anything (&lt;code&gt;az webapp log tail&lt;/code&gt;).&lt;/li&gt;&lt;li&gt;&lt;strong&gt;The app stops responding part-way through the day&lt;/strong&gt; — the F1 60-CPU-minutes-per-day quota is exhausted. It resets the next day. That is the free tier&#39;s ceiling working as designed; the first paid tier removes it.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;A DeprecationWarning about &lt;code&gt;uvicorn.workers&lt;/code&gt; in the logs&lt;/strong&gt; — not a failure. You (or an older tutorial) used the class from the old location; uvicorn 0.54.0 says it moved to the &lt;code&gt;uvicorn-worker&lt;/code&gt; package. Switch before a future uvicorn removes it.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;The container perpetually restarts&lt;/strong&gt; — the platform&#39;s warmup request must get some HTTP status back. An app that never answers (wrong port, crash on import) restarts forever. Slow starters get a lever: the &lt;code&gt;WEBSITES_CONTAINER_START_TIME_LIMIT&lt;/code&gt; app setting (default 230 seconds, maximum 1800).&lt;/li&gt;&lt;/ul&gt;&lt;h2&gt;What it costs&lt;/h2&gt;&lt;p&gt;&lt;strong&gt;F1 is $0&lt;/strong&gt; — and it is a real, currently listed plan, intended for trials, experimentation, and learning the service, with the limits to match:&lt;/p&gt;&lt;table&gt;&lt;tr&gt;&lt;th&gt;F1 (Free) limits&lt;/th&gt;&lt;th&gt;Value&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Compute&lt;/td&gt;&lt;td&gt;Shared, 60 CPU minutes per day (also capped at 3 minutes per 5 minutes)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Memory&lt;/td&gt;&lt;td&gt;1,024 MB per plan&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Storage&lt;/td&gt;&lt;td&gt;1 GB per plan&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Apps per plan&lt;/td&gt;&lt;td&gt;10&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Bandwidth&lt;/td&gt;&lt;td&gt;165 MB per day&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Custom domains&lt;/td&gt;&lt;td&gt;0 — the &lt;code&gt;*.azurewebsites.net&lt;/code&gt; name only (its HTTPS certificate is included)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Always On&lt;/td&gt;&lt;td&gt;No — the app unloads when idle&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;SLA&lt;/td&gt;&lt;td&gt;None&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;p&gt;The first paid step up, &lt;strong&gt;B1 in Central US, was US$0.018 per hour — about US$13.14 per month&lt;/strong&gt; — when this post was prepared (Microsoft&#39;s Retail Prices API, October 2026); B1 gives you a dedicated core, 1.75 GB RAM, and 10 GB storage. (For contrast, Windows B1 in the same region was US$0.075/hour — the Linux/Windows gap is one reason this walkthrough is Linux-only, beside Linux being the only option for Python.)&lt;/p&gt;&lt;p&gt;One billing warning, quoted from Microsoft&#39;s pricing page FAQ because people learn it the expensive way: &lt;strong&gt;“Rates listed apply to apps in stopped state. Please delete apps that are not in use or update tier to Free to avoid charges.”&lt;/strong&gt; App Service plans are charged while they exist, including stopped apps. Deleting is the reliable off switch — which is the next section.&lt;/p&gt;&lt;h2&gt;Delete it when you&#39;re done (yes, really)&lt;/h2&gt;&lt;p&gt;Everything in this walkthrough lives in one resource group, so one command removes all of it — the plan, the app, the settings, everything:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;az group delete --name rg-vsl-topic05-cus --yes&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Two honest notes to close the cost story. First: &lt;strong&gt;this post&#39;s own deployment was deleted this way after filming.&lt;/strong&gt; The video&#39;s live site is gone by the time you watch it — that is the owner&#39;s rule for this channel (free tier only, and delete resources when you&#39;re not using them) applied to itself, not a gap in the tutorial. Second: nothing is lost by deleting. The companion repo folder plus the commands in this post redeploy the app from scratch any time you want it back, in the same sitting. Treat free-tier resources as disposable by default, and the cloud bill for learning stays exactly $0.&lt;/p&gt;&lt;h2&gt;Where to go next&lt;/h2&gt;&lt;p&gt;This same one-file app is the running example for the rest of the ship-and-run mini-series: &lt;strong&gt;containerise it with Docker&lt;/strong&gt; (Topic #9), &lt;strong&gt;deploy it on every push with GitHub Actions&lt;/strong&gt; through Deployment Center — which commits a workflow file to your repo and authenticates with a Microsoft Entra identity (Topic #13) — and &lt;strong&gt;watch it with Prometheus and Grafana&lt;/strong&gt; (Topic #10). On App Service itself, the next steps in order: a custom domain and its certificate when you move to a Basic-or-higher plan (the free tier supports none), and the log stream as a daily habit rather than an emergency tool.&lt;/p&gt;&lt;h2&gt;Get the code&lt;/h2&gt;&lt;p&gt;The complete &lt;code&gt;main.py&lt;/code&gt;, pinned &lt;code&gt;requirements.txt&lt;/code&gt;, the &lt;code&gt;startup.txt&lt;/code&gt; command file, and this post&#39;s setup as a README live in the companion repository:&lt;br&gt;&lt;a href=&quot;https://github.com/sajjad037/visual-studio-learn-code/tree/main/topic-05-deploy-fastapi-azure&quot;&gt;https://github.com/sajjad037/visual-studio-learn-code/tree/main/topic-05-deploy-fastapi-azure&lt;/a&gt;&lt;/p&gt;&lt;h2&gt;Sources&lt;/h2&gt;&lt;ul&gt;&lt;li&gt;Python on Azure App Service: container characteristics, startup process, default page behaviour, troubleshooting — &lt;a href=&quot;https://learn.microsoft.com/en-us/azure/app-service/configure-language-python&quot;&gt;https://learn.microsoft.com/en-us/azure/app-service/configure-language-python&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Deploy Python (FastAPI) — Microsoft&#39;s quickstart: zip deploy route, &lt;code&gt;SCM_DO_BUILD_DURING_DEPLOYMENT&lt;/code&gt;, the Python 3.14 FastAPI auto-detection note — &lt;a href=&quot;https://learn.microsoft.com/en-us/azure/app-service/quickstart-python&quot;&gt;https://learn.microsoft.com/en-us/azure/app-service/quickstart-python&lt;/a&gt;&lt;/li&gt;&lt;li&gt;App Service on Linux FAQ (ports, &lt;code&gt;WEBSITES_PORT&lt;/code&gt;, warmup and container start time limit) — &lt;a href=&quot;https://learn.microsoft.com/en-us/azure/app-service/faq-app-service-linux&quot;&gt;https://learn.microsoft.com/en-us/azure/app-service/faq-app-service-linux&lt;/a&gt;&lt;/li&gt;&lt;li&gt;App Service limits, incl. the F1 quota rows — &lt;a href=&quot;https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/azure-subscription-service-limits&quot;&gt;https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/azure-subscription-service-limits&lt;/a&gt;&lt;/li&gt;&lt;li&gt;App Service (Linux) pricing, incl. the Free plan and the stopped-apps FAQ — &lt;a href=&quot;https://azure.microsoft.com/en-us/pricing/details/app-service/linux/&quot;&gt;https://azure.microsoft.com/en-us/pricing/details/app-service/linux/&lt;/a&gt;&lt;/li&gt;&lt;li&gt;B1 Central US prices (Linux US$0.018/hour, Windows US$0.075/hour) — Azure Retail Prices API, &lt;a href=&quot;https://prices.azure.com/api/retail/prices&quot;&gt;https://prices.azure.com/api/retail/prices&lt;/a&gt; (accessed October 28, 2026)&lt;/li&gt;&lt;li&gt;Azure free account offer and signup requirements — &lt;a href=&quot;https://azure.microsoft.com/en-us/free/&quot;&gt;https://azure.microsoft.com/en-us/free/&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;code&gt;az webapp up&lt;/code&gt; and the Azure CLI webapp command reference — &lt;a href=&quot;https://learn.microsoft.com/en-us/cli/azure/webapp&quot;&gt;https://learn.microsoft.com/en-us/cli/azure/webapp&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Deploying a Python FastAPI app to App Service on Linux — Microsoft App Service support team blog, 2024-04-23 — &lt;a href=&quot;https://azureossd.github.io/2024/04/23/Deploying-a-Python-FastAPI-app-to-App-Service-Linux/&quot;&gt;https://azureossd.github.io/2024/04/23/Deploying-a-Python-FastAPI-app-to-App-Service-Linux/&lt;/a&gt;&lt;/li&gt;&lt;li&gt;FastAPI deployment: server workers — &lt;a href=&quot;https://fastapi.tiangolo.com/deployment/server-workers/&quot;&gt;https://fastapi.tiangolo.com/deployment/server-workers/&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Uvicorn worker deprecation — the uvicorn 0.54.0 package itself, and the replacement package — &lt;a href=&quot;https://pypi.org/project/uvicorn-worker/&quot;&gt;https://pypi.org/project/uvicorn-worker/&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Package versions — &lt;a href=&quot;https://pypi.org/project/fastapi/&quot;&gt;https://pypi.org/project/fastapi/&lt;/a&gt; (0.162.0), &lt;a href=&quot;https://pypi.org/project/uvicorn/&quot;&gt;https://pypi.org/project/uvicorn/&lt;/a&gt; (0.54.0), &lt;a href=&quot;https://pypi.org/project/gunicorn/&quot;&gt;https://pypi.org/project/gunicorn/&lt;/a&gt; (26.2.0), https://pypi.org/project/azure-cli releases via &lt;a href=&quot;https://github.com/Azure/azure-cli/releases&quot;&gt;https://github.com/Azure/azure-cli/releases&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div style=&quot;text-align: center;&quot;&gt;&lt;h3&gt;Enjoying these tutorials?&lt;/h3&gt;&lt;p&gt;Get new Visual Studio Learn posts by email — free, no spam:&lt;/p&gt;&lt;form action=&quot;https://api.follow.it/subscription-form/SmRpWjEzV1ovU0FOK2R2Y0oxajFBUmtIdW44K1ZiUWZRUm1FZVdpVlRKUHFLSFBKWjE4M1hTSk5yRU5oNmg0RU5zelhuTllNTGU1UmwzRkN0NzZWbnd2YkV5VWFwYnptWFdtekI3eXdwMmhtamtUNit3dlNSblVmRjJxd1JGbjZ8Q05yQWR2eitvYW5HRmNMMzJkbStpM09rNmxXZ0ZjWWhyOERya2VvVXRzZz0=/8&quot; method=&quot;post&quot;&gt;&lt;div class=&quot;form-preview&quot; style=&quot;background-color: white; border-color: rgb(204, 204, 204); border-style: solid; border-width: 1px; position: relative;&quot;&gt;&lt;div class=&quot;preview-heading&quot;&gt;&lt;h5 style=&quot;color: black; font-family: Arial; font-size: 16px; font-weight: bold; text-align: center; text-transform: none;&quot;&gt;Get new posts by email:&lt;/h5&gt;&lt;/div&gt;&lt;div class=&quot;preview-input-field&quot;&gt;&lt;input name=&quot;email&quot; required=&quot;&quot; spellcheck=&quot;false&quot; style=&quot;background-color: white; color: black; font-family: Arial; font-size: 14px; font-weight: normal; text-align: center; text-transform: none;&quot; type=&quot;email&quot; /&gt;&lt;/div&gt;&lt;div class=&quot;preview-submit-button&quot;&gt;&lt;button style=&quot;background-color: black; color: white; font-family: Arial; font-size: 16px; font-weight: bold; text-align: center; text-transform: none;&quot; type=&quot;submit&quot;&gt;Subscribe&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/form&gt;&lt;a class=&quot;powered-by-line&quot; href=&quot;https://follow.it&quot;&gt;Powered by &lt;img alt=&quot;follow.it&quot; height=&quot;17px&quot; src=&quot;https://follow.it/images/colored-logo.svg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p style=&quot;font-size: 14px; margin: 12px 0px 0px;&quot;&gt;Follow along: &lt;a href=&quot;https://www.youtube.com/@VisualStudioLearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/youtube/FF0000&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;YouTube&quot; /&gt; YouTube&lt;/a&gt; · &lt;a href=&quot;https://www.facebook.com/VisualStudio.Learn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/facebook/1877F2&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;Facebook&quot; /&gt; Facebook&lt;/a&gt; · &lt;a href=&quot;https://www.instagram.com/visualstudiolearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/instagram/E4405F&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;Instagram&quot; /&gt; Instagram&lt;/a&gt; · &lt;a href=&quot;https://www.tiktok.com/@visualstudiolearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/tiktok/000000&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;TikTok&quot; /&gt; TikTok&lt;/a&gt; · &lt;a href=&quot;https://www.visualstudiolearn.com/feeds/posts/default?alt=rss&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/rss/FFA500&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;RSS&quot; /&gt; RSS&lt;/a&gt;&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.visualstudiolearn.com/feeds/873346114526941927/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.visualstudiolearn.com/2026/10/how-do-i-deploy-fastapi-app-to-azure.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/873346114526941927'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/873346114526941927'/><link rel='alternate' type='text/html' href='http://www.visualstudiolearn.com/2026/10/how-do-i-deploy-fastapi-app-to-azure.html' title='How Do I Deploy a FastAPI App to Azure App Service? Step by Step, From Zero'/><author><name>The Loonie Lesson</name><uri>http://www.blogger.com/profile/02243798795439866634</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRY4V9cFLhqMxHWBOWCGBiGUqjan14Vda_dy5rz-C41kApceM-u6jAxS2_6AIRZtGkQeyiQolq1gCeSBhpo573lYq37MLmMGgSKDQLasowic65VoitivYCzqrmUDUqxJuqCTyzah8JuALYtW0Iz7rXIvIcj-ovhVpEuEhQJ6yG1FUQ-A/s1600/loonie-lesson-logo-profile.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhzgHu6zkO5XmaAhOBAW1UL_5eDQ3y2FoJ8i7RplrxHCFAb19i6aKwLbQ5lZhRUv7nuPGcY1VlAPEzBYwl9E19C66dwJ9jqFV1ArJQr_16H7dwPwt2TEwEmJYOA_U7FWlrfry6syRX7iUz-F-cWKUAmi5BoFfzZR08hF6YjgkuYlUoFLd_uuvYLT3kKbCQ/s72-c/portal-overview.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8861905861585113921.post-7852476395742725949</id><published>2026-10-27T09:10:29.000-04:00</published><updated>2026-11-22T02:50:50.782-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="AI Agents"/><category scheme="http://www.blogger.com/atom/ns#" term="Automation"/><category scheme="http://www.blogger.com/atom/ns#" term="Gemini"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><category scheme="http://www.blogger.com/atom/ns#" term="Tutorials"/><title type='text'>I Built an AI Agent That Reads Release Notes So I Don&#39;t Have To</title><content type='html'>&lt;p&gt;&lt;strong&gt;Versions used in this post — read this first:&lt;/strong&gt; Python 3.12.3 · requests 2.34.2 · google-genai 2.33.0 · Model: &lt;code&gt;gemini-3.6-flash&lt;/code&gt;. Everything runs from one file, &lt;code&gt;digest_agent.py&lt;/code&gt;, printed in full below — the exact file in the companion repo.&lt;/p&gt;&lt;h2&gt;The direct answer&lt;/h2&gt;&lt;p&gt;Build an AI agent in Python, step by step, that reads release notes for you. One small Python file is enough to stop reading changelogs by hand. Think of the agent as the smallest useful harness — three steps with a strict contract between them: &lt;strong&gt;fetch&lt;/strong&gt;, &lt;strong&gt;summarize&lt;/strong&gt;, &lt;strong&gt;write&lt;/strong&gt;. It fetches the newest GitHub release of each project it tracks from the project&#39;s public releases feed, sends the release notes to Gemini with an instruction to return three summary bullets plus any breaking changes as JSON, and writes one Markdown file: &lt;code&gt;digest.md&lt;/code&gt;, with the breaking changes collected at the top. Schedule it for 7:15 every morning and yesterday&#39;s releases are a one-page read with your coffee. No GitHub account, no token, no framework — the only credential is a Gemini API key, and the free tier covers this easily.&lt;/p&gt;&lt;h2&gt;What you will build&lt;/h2&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;A feed fetcher&lt;/strong&gt; — reads &lt;code&gt;https://github.com/&amp;lt;owner&amp;gt;/&amp;lt;repo&amp;gt;/releases.atom&lt;/code&gt; for CPython, FastAPI, and the Azure CLI. The first entry in a releases feed is the newest published release. Feeds are public web pages, so nothing here touches the GitHub REST API and its 60-requests-per-hour unauthenticated limit.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;A summariser&lt;/strong&gt; — one function that strips the notes&#39; HTML to text, truncates anything very long, and asks Gemini for exactly three bullets and a breaking-changes list, as JSON.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;A digest writer&lt;/strong&gt; — assembles &lt;code&gt;digest.md&lt;/code&gt;: a dated header, a ⚠ Breaking changes section across all projects, one section per project (version, date, bullets, link), and a footer with the run&#39;s real token counts and estimated cost.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;A schedule&lt;/strong&gt; — the same script run daily by Windows Task Scheduler or cron (both shown below).&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Two design choices are deliberate. The agent reads &lt;strong&gt;feeds, not the API&lt;/strong&gt;: simpler, unauthenticated, and nothing to rate-limit at three projects a day. And it has &lt;strong&gt;no memory&lt;/strong&gt;: it always summarises the newest release in each feed, whether or not you read yesterday&#39;s digest. Remembering where it left off is the first upgrade, listed at the end.&lt;/p&gt;&lt;h2&gt;Environment setup — do this first&lt;/h2&gt;&lt;p&gt;&lt;strong&gt;Prerequisites:&lt;/strong&gt; Python 3.10 or newer (this post uses 3.12.3), a terminal, and a Gemini API key. The free tier is enough; no card is needed.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Get your Gemini API key, click by click:&lt;/strong&gt;&lt;/p&gt;&lt;ol&gt;&lt;li&gt;Go to &lt;strong&gt;aistudio.google.com&lt;/strong&gt; and sign in with a Google account.&lt;/li&gt;&lt;li&gt;Click &lt;strong&gt;Get API key&lt;/strong&gt; in the left sidebar (the key icon).&lt;/li&gt;&lt;li&gt;Click &lt;strong&gt;Create API key&lt;/strong&gt;, choose or create a Google Cloud project when asked, and click &lt;strong&gt;Create&lt;/strong&gt;.&lt;/li&gt;&lt;li&gt;Copy the key that appears — it stays visible in that same key list.&lt;/li&gt;&lt;li&gt;Keep it secret like a password: never paste it into code, a screenshot, or a public repository. This agent reads it only from an environment variable.&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;&lt;strong&gt;Create the project and install the pinned dependencies:&lt;/strong&gt;&lt;/p&gt;&lt;pre&gt;&lt;code&gt;mkdir release-digest &amp;amp;&amp;amp; cd release-digest
python3 -m venv .venv &amp;amp;&amp;amp; source .venv/bin/activate
# Windows PowerShell: py -m venv .venv ; .venv\Scripts\Activate.ps1
pip install requests==2.34.2 google-genai==2.33.0&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;(The code goes in this folder as &lt;code&gt;digest_agent.py&lt;/code&gt; — it is printed in full in the steps below, and the companion repo has it as a ready-made file.)&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Set the key, then verify the setup:&lt;/strong&gt;&lt;/p&gt;&lt;pre&gt;&lt;code&gt;export GEMINI_API_KEY=&amp;lt;redacted&amp;gt;
# Windows PowerShell: $env:GEMINI_API_KEY=&amp;lt;redacted&amp;gt;
python -c &quot;import requests, google.genai; print(&#39;setup ok&#39;)&quot;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;If that prints &lt;code&gt;setup ok&lt;/code&gt;, the environment is ready. Every error the run itself can throw is listed in &quot;Errors you may hit&quot; near the end.&lt;/p&gt;&lt;h2&gt;Step 1 — Config, data shapes, and stripping the HTML&lt;/h2&gt;&lt;p&gt;The top of the file is everything you are allowed to tweak: the model name as one constant (you will see why in the errors section), the list of projects to watch, the feed URL pattern, and the published per-token rates the cost footer uses. Below that, two small dataclasses — &lt;code&gt;Release&lt;/code&gt; (what a feed hands us) and &lt;code&gt;Summary&lt;/code&gt; (what the model hands back) — and a minimal HTML-to-text converter, because feed content arrives as HTML and Gemini should read words, not markup.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&quot;&quot;&quot;Release-notes digest agent: read the changelogs so you don&#39;t have to.

Companion code for the Visual Studio Learn post and video
&quot;I Built an AI Agent That Reads Release Notes So I Don&#39;t Have To&quot;.

Once a run, the agent fetches the latest GitHub release of each project
in PROJECTS below, asks Gemini to summarise the release notes in three
bullets and flag anything breaking, and writes one Markdown digest
(digest.md) with a breaking-changes section at the top.

Requires: Python 3.10 or newer, the packages in requirements.txt, and a
Gemini API key in the GEMINI_API_KEY environment variable. See README.md.
&quot;&quot;&quot;

import json
import logging
import os
import sys
import xml.etree.ElementTree as ET
from dataclasses import dataclass, field
from datetime import date
from html.parser import HTMLParser

import requests
from google import genai

logger = logging.getLogger(&quot;digest_agent&quot;)
logging.basicConfig(level=logging.INFO, format=&quot;%(levelname)s %(message)s&quot;)

MODEL = &quot;gemini-3.6-flash&quot;

# The projects to watch: (display name, GitHub &quot;owner/repo&quot;). Add or
# remove lines here — nothing else in the file needs to change.
PROJECTS = [
    (&quot;CPython&quot;, &quot;python/cpython&quot;),
    (&quot;FastAPI&quot;, &quot;tiangolo/fastapi&quot;),
    (&quot;Azure CLI&quot;, &quot;Azure/azure-cli&quot;),
]

FEED_URL = &quot;https://github.com/{repo}/releases.atom&quot;
USER_AGENT = &quot;release-digest-agent/1.0 (Visual Studio Learn tutorial)&quot;
FETCH_TIMEOUT_SECONDS = 30
MAX_NOTES_CHARS = 12_000  # truncate very long notes before the model call

# Published pay-as-you-go rates for gemini-3.6-flash, US$ per 1M tokens.
INPUT_RATE_PER_MILLION = 0.75
OUTPUT_RATE_PER_MILLION = 3.75

OUTPUT_FILE = &quot;digest.md&quot;

ATOM = {&quot;a&quot;: &quot;http://www.w3.org/2005/Atom&quot;}
# Tags that mean &quot;new line&quot; when release-note HTML is flattened to text.
BLOCK_TAGS = {&quot;p&quot;, &quot;br&quot;, &quot;div&quot;, &quot;li&quot;, &quot;ul&quot;, &quot;ol&quot;, &quot;h1&quot;, &quot;h2&quot;, &quot;h3&quot;, &quot;h4&quot;, &quot;pre&quot;, &quot;tr&quot;}&lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code&gt;@dataclass
class Release:
    &quot;&quot;&quot;The latest release of one project, as published in its feed.&quot;&quot;&quot;

    project: str
    title: str  # feed title, e.g. &quot;v3.13.18&quot; or &quot;Azure CLI 2.91.0&quot;
    published: str  # ISO date from the feed, e.g. &quot;2026-10-25&quot;
    url: str
    notes: str  # release notes as plain text (HTML already stripped)


@dataclass
class Summary:
    &quot;&quot;&quot;Gemini&#39;s reading of one release, plus what the call cost.&quot;&quot;&quot;

    bullets: list[str] = field(default_factory=list)
    breaking: list[str] = field(default_factory=list)
    input_tokens: int = 0
    output_tokens: int = 0


class _TextExtractor(HTMLParser):
    &quot;&quot;&quot;Minimal HTML-to-text: keep the words, drop the markup.&quot;&quot;&quot;

    def __init__(self) -&amp;gt; None:
        super().__init__()
        self.parts: list[str] = []

    def handle_starttag(self, tag, attrs):
        if tag in BLOCK_TAGS:
            self.parts.append(&quot;\n&quot;)

    def handle_data(self, data):
        self.parts.append(data)


def html_to_text(html: str) -&amp;gt; str:
    &quot;&quot;&quot;Flatten release-note HTML to readable plain text.&quot;&quot;&quot;
    extractor = _TextExtractor()
    extractor.feed(html)
    lines = (line.strip() for line in &quot;&quot;.join(extractor.parts).splitlines())
    return &quot;\n&quot;.join(line for line in lines if line)&lt;/code&gt;&lt;/pre&gt;&lt;h2&gt;Step 2 — Fetch the newest release from the feed&lt;/h2&gt;&lt;p&gt;One function per project: GET the feed with a polite user agent and a timeout, parse the XML, take the first entry, and flatten its HTML body to text. &lt;code&gt;raise_for_status()&lt;/code&gt; means a dead or renamed repo fails loudly in the log instead of producing a quietly empty digest.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;def fetch_latest_release(project: str, repo: str) -&amp;gt; Release:
    &quot;&quot;&quot;Fetch the newest entry of a project&#39;s GitHub releases Atom feed.

    The feed lists the 10 most recently published releases; the first
    entry is the newest. No GitHub account or token is needed — the
    feed is a public web page, not the rate-limited REST API.
    &quot;&quot;&quot;
    url = FEED_URL.format(repo=repo)
    logger.info(&quot;Fetching %s&quot;, url)
    response = requests.get(
        url, headers={&quot;User-Agent&quot;: USER_AGENT}, timeout=FETCH_TIMEOUT_SECONDS
    )
    response.raise_for_status()

    root = ET.fromstring(response.content)
    entry = root.find(&quot;a:entry&quot;, ATOM)
    if entry is None:
        raise ValueError(f&quot;{repo}: the releases feed has no entries&quot;)

    title = entry.findtext(&quot;a:title&quot;, default=&quot;&quot;, namespaces=ATOM).strip()
    updated = entry.findtext(&quot;a:updated&quot;, default=&quot;&quot;, namespaces=ATOM)
    link = entry.find(&quot;a:link&quot;, ATOM)
    content = entry.find(&quot;a:content&quot;, ATOM)
    notes_html = content.text if content is not None and content.text else &quot;&quot;

    return Release(
        project=project,
        title=title,
        published=updated[:10],
        url=link.get(&quot;href&quot;, &quot;&quot;) if link is not None else &quot;&quot;,
        notes=html_to_text(notes_html),
    )&lt;/code&gt;&lt;/pre&gt;&lt;h2&gt;Step 3 — Ask Gemini for bullets and breaking changes&lt;/h2&gt;&lt;p&gt;This is the only function that talks to the model, and its contract is strict: work only from the notes text provided, return JSON and nothing else — exactly three summary bullets, plus a list of changes that could break existing code, config, or command lines. Two honesty guards live here. If a release ships with &lt;strong&gt;no notes at all&lt;/strong&gt;, the function returns a line saying so &lt;em&gt;without&lt;/em&gt; spending a model call. And if the model&#39;s answer is not the expected JSON, the code keeps the raw text as a single bullet and logs a warning, rather than crashing the morning run.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;def summarize_release(client: genai.Client, release: Release) -&amp;gt; Summary:
    &quot;&quot;&quot;Ask Gemini for three bullets and any breaking changes in a release.

    The model only ever sees the notes text fetched above — it is told
    to work from that text alone and to say so when the notes are thin.
    Its answer comes back as JSON and is parsed defensively.
    &quot;&quot;&quot;
    if not release.notes.strip():
        # Nothing to summarise: don&#39;t spend a model call on an empty page.
        logger.info(&quot;%s: no release notes published — skipping the model call&quot;,
                    release.project)
        return Summary(bullets=[&quot;No release notes were published with this release.&quot;])

    prompt = f&quot;&quot;&quot;You are summarising a software release for a working developer.

Project: {release.project}
Release: {release.title} ({release.published})

Release notes (plain text, converted from the project&#39;s own feed):
---
{release.notes[:MAX_NOTES_CHARS]}
---

Answer with ONLY a JSON object, no markdown fences, in exactly this shape:
{{&quot;summary&quot;: [&quot;bullet&quot;, &quot;bullet&quot;, &quot;bullet&quot;],
  &quot;breaking&quot;: [&quot;one line per breaking change, or empty list&quot;]}}

Rules:
- &quot;summary&quot; is exactly 3 short bullets, based only on the notes above.
- If the notes contain no real change details, say that honestly in one
  bullet instead of inventing changes.
- &quot;breaking&quot; lists only changes that could break existing code, config,
  or command lines — behaviour changes count. Ignore download hashes,
  links, and housekeeping.
&quot;&quot;&quot;&lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code&gt;    response = client.models.generate_content(model=MODEL, contents=prompt)
    usage = response.usage_metadata
    text = (response.text or &quot;&quot;).strip()
    if text.startswith(&quot;```&quot;):  # strip code fences if the model adds them
        text = text.strip(&quot;`&quot;)
        text = text[text.find(&quot;{&quot;):]

    try:
        data = json.loads(text)
        bullets = [str(b) for b in data.get(&quot;summary&quot;, [])][:3]
        breaking = [str(b) for b in data.get(&quot;breaking&quot;, [])]
    except (json.JSONDecodeError, AttributeError):
        logger.warning(&quot;%s: model answer was not the expected JSON; &quot;
                       &quot;keeping the raw text as a single bullet&quot;, release.project)
        bullets = [text] if text else [&quot;The model returned an empty answer.&quot;]
        breaking = []

    return Summary(
        bullets=bullets,
        breaking=breaking,
        input_tokens=usage.prompt_token_count or 0,
        output_tokens=usage.candidates_token_count or 0,
    )


def estimate_cost(input_tokens: int, output_tokens: int) -&amp;gt; float:
    &quot;&quot;&quot;Estimated US$ cost of the run at the published per-token rates.&quot;&quot;&quot;
    return (
        input_tokens * INPUT_RATE_PER_MILLION
        + output_tokens * OUTPUT_RATE_PER_MILLION
    ) / 1_000_000&lt;/code&gt;&lt;/pre&gt;&lt;h2&gt;Step 4 — Assemble the digest and run&lt;/h2&gt;&lt;p&gt;&lt;code&gt;build_digest&lt;/code&gt; puts the breaking changes first — across every project — because that is the section that decides whether today is a reading day or an upgrading day. &lt;code&gt;main&lt;/code&gt; loops the projects, and a failure in one (a feed down, a repo renamed) is logged and skipped, not fatal: a digest with two of three projects beats no digest. Only if every project fails does the run exit without writing a file.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;def build_digest(entries: list[tuple[Release, Summary]]) -&amp;gt; str:
    &quot;&quot;&quot;Assemble the one-page Markdown digest from all summaries.&quot;&quot;&quot;
    today = date.today().strftime(&quot;%A, %B %d, %Y&quot;)
    total_in = sum(s.input_tokens for _, s in entries)
    total_out = sum(s.output_tokens for _, s in entries)

    lines = [f&quot;# Release Digest — {today}&quot;, &quot;&quot;]

    breaking_lines = [
        f&quot;- **{release.project} {release.title}** — {item}&quot;
        for release, summary in entries
        for item in summary.breaking
    ]
    lines += [&quot;## ⚠ Breaking changes&quot;, &quot;&quot;]
    lines += breaking_lines or [&quot;None flagged in this run.&quot;]
    lines.append(&quot;&quot;)

    for release, summary in entries:
        lines += [
            f&quot;## {release.project} — {release.title}&quot;,
            &quot;&quot;,
            f&quot;Released: {release.published} · [Release notes]({release.url})&quot;,
            &quot;&quot;,
        ]
        lines += [f&quot;- {bullet}&quot; for bullet in summary.bullets]
        lines.append(&quot;&quot;)

    lines += [
        &quot;---&quot;,
        f&quot;*Generated by digest_agent.py · Model: {MODEL} · &quot;
        f&quot;Tokens: {total_in:,} in / {total_out:,} out · &quot;
        f&quot;Estimated cost: ${estimate_cost(total_in, total_out):.6f}*&quot;,
    ]
    return &quot;\n&quot;.join(lines) + &quot;\n&quot;&lt;/code&gt;&lt;/pre&gt;&lt;pre&gt;&lt;code&gt;def main() -&amp;gt; int:
    api_key = os.environ.get(&quot;GEMINI_API_KEY&quot;)
    if not api_key:
        logger.error(&quot;GEMINI_API_KEY is not set. Get a key in Google AI &quot;
                     &quot;Studio and set it in the environment — see README.md.&quot;)
        return 1

    client = genai.Client(api_key=api_key)
    entries: list[tuple[Release, Summary]] = []
    failures = 0
    for project, repo in PROJECTS:
        try:
            release = fetch_latest_release(project, repo)
            entries.append((release, summarize_release(client, release)))
            logger.info(&quot;%s: %s (%s)&quot;, project, release.title, release.published)
        except Exception:
            failures += 1
            logger.exception(&quot;%s: fetch or summary failed — continuing &quot;
                             &quot;with the remaining projects&quot;, project)

    if not entries:
        logger.error(&quot;Every project failed; no digest written.&quot;)
        return 1

    with open(OUTPUT_FILE, &quot;w&quot;, encoding=&quot;utf-8&quot;) as fh:
        fh.write(build_digest(entries))
    logger.info(&quot;Wrote %s (%d project(s), %d failure(s))&quot;,
                OUTPUT_FILE, len(entries), failures)
    return 0


if __name__ == &quot;__main__&quot;:
    sys.exit(main())&lt;/code&gt;&lt;/pre&gt;&lt;h2&gt;Step 5 — Run it, and read the digest&lt;/h2&gt;&lt;pre&gt;&lt;code&gt;python digest_agent.py&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The log shows each feed fetched and each release found, and the run writes &lt;code&gt;digest.md&lt;/code&gt; in the same folder. When this post was prepared (Sunday, October 25, 2026), the three feeds&#39; newest releases were &lt;strong&gt;CPython v3.13.18&lt;/strong&gt; (published that same day), &lt;strong&gt;FastAPI 0.162.0&lt;/strong&gt; (October 23), and &lt;strong&gt;Azure CLI 2.91.0&lt;/strong&gt; (October 5) — the agent re-fetches on every run, so your digest always carries whatever is newest that morning.&lt;/p&gt;&lt;p&gt;Two of the three feeds teach an honest lesson about release notes. CPython&#39;s GitHub release body is a one-liner (&quot;Python 3.15.2&quot;) — its real changelog lives on docs.python.org — and the Azure CLI&#39;s body is mostly a link to its full notes page plus download hashes. The prompt tells Gemini to say so instead of inventing changes, and to ignore hashes; the summaries for those two are thin &lt;em&gt;because the notes are thin&lt;/em&gt;, and the digest does not pretend otherwise.&lt;/p&gt;&lt;p&gt;Here is the complete &lt;code&gt;digest.md&lt;/code&gt; from the live production run of this exact file, made on Monday, October 26, 2026 — real Gemini bullets, a real breaking-change flag, and the run&#39;s real token counts in the footer. One change from prep is visible in it: the CPython feed&#39;s newest entry had advanced to v3.15.2 (published October 25, after the prep fetch), while FastAPI and Azure CLI stood still — the feed reports what was published last, and the digest prints it as-is:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;# Release Digest — Monday, October 26, 2026

## ⚠ Breaking changes

- **FastAPI 0.162.0** — FastAPI no longer infers False for omitted boolean form fields; unsubmitted fields without a default will now raise a missing-field error instead of evaluating to False.

## CPython — v3.15.2

Released: 2026-10-25 · [Release notes](https://github.com/python/cpython/releases/tag/v3.15.2)

- Python 3.15.2 release announced on 2026-10-25.
- The provided release notes contain no detailed changelog or change descriptions.
- No specific features, bug fixes, or updates were documented in the notes.

## FastAPI — 0.162.0

Released: 2026-10-23 · [Release notes](https://github.com/fastapi/fastapi/releases/tag/0.162.0)

- Omitted bool and bool | None form fields now use declared defaults or raise missing-field errors instead of automatically inferring False.
- Applies across individual form parameters, form models, and form model unions, bringing behavior in line with standard Pydantic logic.
- Omitted boolean fields are now excluded from model_dump(exclude_unset=True) and no longer count as submitted fields during union selection.

## Azure CLI — Azure CLI 2.91.0

Released: 2026-10-05 · [Release notes](https://github.com/Azure/azure-cli/releases/tag/azure-cli-2.91.0)

- The release notes contain no detailed change log or feature notes.
- Checksums (SHA256) are provided for official Linux, macOS, and Windows release artifacts.
- Artifacts cover arm64 and x86_64 architectures along with MSI and ZIP packages.

---
*Generated by digest_agent.py · Model: gemini-3.6-flash · Tokens: 1,313 in / 252 out · Estimated cost: $0.001930*&lt;/code&gt;&lt;/pre&gt;&lt;h2&gt;Scheduling it daily&lt;/h2&gt;&lt;p&gt;&lt;strong&gt;Windows — Task Scheduler:&lt;/strong&gt; Create Basic Task → Daily, your morning time → Action: Start a program → Program: &lt;code&gt;C:\path\to\release-digest\.venv\Scripts\python.exe&lt;/code&gt;, Arguments: &lt;code&gt;digest_agent.py&lt;/code&gt;, Start in: &lt;code&gt;C:\path\to\release-digest&lt;/code&gt;. Set &lt;code&gt;GEMINI_API_KEY&lt;/code&gt; once as a user environment variable (Settings → Environment Variables) — the scheduled task does not inherit your open PowerShell window&#39;s variables.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;macOS / Linux — cron:&lt;/strong&gt;&lt;/p&gt;&lt;pre&gt;&lt;code&gt;15 7 * * * cd /path/to/release-digest &amp;amp;&amp;amp; GEMINI_API_KEY=&amp;lt;redacted&amp;gt; .venv/bin/python digest_agent.py &amp;gt;&amp;gt; digest.log 2&amp;gt;&amp;amp;1&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;(On a shared machine, keep the key in a protected file or secret store rather than the crontab.)&lt;/p&gt;&lt;h2&gt;Errors you may hit&lt;/h2&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;&lt;code&gt;GEMINI_API_KEY is not set&lt;/code&gt;&lt;/strong&gt; — the agent stops before doing any work. The variable must exist in the same shell (or scheduled environment) that runs the script; setting it in one terminal does not set it in another, and Task Scheduler tasks never see your interactive shell&#39;s variables.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;A 404 naming the model — &quot;&lt;code&gt;gemini-…&lt;/code&gt; is no longer available to new users&quot;&lt;/strong&gt; — Google retires model names for &lt;em&gt;new&lt;/em&gt; API keys first: the model can be listed in the docs and still refuse a fresh key. This exact error is why the model is a single constant at the top of the file. Verified example: &lt;code&gt;gemini-2.5-flash&lt;/code&gt; returned it to newly created keys in October 2026. Fix: open Google&#39;s models page, take the current Flash model it names, update &lt;code&gt;MODEL&lt;/code&gt;. This post pins &lt;code&gt;gemini-3.6-flash&lt;/code&gt;, verified current and stable in Google&#39;s docs on October 26, 2026 — the pin moved from &lt;code&gt;gemini-3.8-flash&lt;/code&gt; during production, for the reason in the 503 entry below.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;429 / quota errors from Gemini&lt;/strong&gt; — the free tier has request limits; three small summaries a day sit far below them, but a brand-new key can be throttled and heavy same-day experimenting adds up. Wait for the daily reset, or enable billing.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;503 UNAVAILABLE — &quot;The model is currently experiencing high demand&quot;&lt;/strong&gt; — the model itself is temporarily overloaded; this is not your key, your code, or your quota. This exact error shaped this post: during production on October 26, 2026, &lt;code&gt;gemini-3.8-flash&lt;/code&gt; returned 503s (and quota 429s under retry load) across ten runs of this exact file, completing only single-project digests. The fix applied is the one the file is designed for — the pin moved to &lt;code&gt;gemini-3.6-flash&lt;/code&gt;, one constant, identical published rates — and the unchanged-everything-else file completed the full capture the same day. For a scheduled daily job the softer fix also works: wait, and let the next run try again.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;One project logs &quot;fetch or summary failed&quot; but the run completes&lt;/strong&gt; — that feed was unreachable, the repo was renamed, or the feed returned no entries. A renamed repo empties its old feed; update the path in &lt;code&gt;PROJECTS&lt;/code&gt;.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;The ⚠ section says &quot;None flagged in this run&quot;&lt;/strong&gt; — that is a result, not an error: the model found nothing in the notes that could break existing code. Skim the bullets before trusting it.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Oddly thin bullets for CPython or the Azure CLI&lt;/strong&gt; — expected (see Step 5): their GitHub bodies carry almost no prose. The agent summarises the feed it is given, no more.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Digest shows an older version line than you expected&lt;/strong&gt; — feeds order by &lt;em&gt;publish time&lt;/em&gt;, not version number. On October 25, 2026, CPython published several releases the same day — security fixes for older branches (v3.13.18 among them) and v3.15.2 on the current line; the digest&#39;s CPython line is whichever was published last — v3.15.2 — not the day&#39;s highest-profile release. The digest always prints the version and date so the ordering is visible, and for a &quot;what shipped most recently&quot; digest that ordering is the honest one.&lt;/li&gt;&lt;/ul&gt;&lt;h2&gt;Where to go next&lt;/h2&gt;&lt;p&gt;Three upgrades, in the order a daily tool earns them: &lt;strong&gt;memory&lt;/strong&gt; (a small state file recording the last release seen per project, so the agent only summarises what is actually new and can say &quot;nothing new today&quot;), &lt;strong&gt;delivery&lt;/strong&gt; (mail or message the digest to yourself instead of leaving it as a file — the agent from Topic #1 has the tool-calling pattern for it), and &lt;strong&gt;more sources&lt;/strong&gt; (any GitHub project is one line in &lt;code&gt;PROJECTS&lt;/code&gt;; projects that publish proper changelogs — frameworks, CLIs, SDKs — digest best). Previous in this series of agent builds: Topic #1&#39;s tool-calling agent and Topic #2&#39;s MCP server; the PDF Q&amp;amp;A app (Small AI App #1) is the same Gemini pattern pointed at documents instead of feeds.&lt;/p&gt;&lt;h2&gt;Get the code&lt;/h2&gt;&lt;p&gt;The complete &lt;code&gt;digest_agent.py&lt;/code&gt;, pinned &lt;code&gt;requirements.txt&lt;/code&gt;, and this post&#39;s setup as a README live in the companion repository:&lt;br&gt;&lt;a href=&quot;https://github.com/sajjad037/visual-studio-learn-code/tree/main/topic-04-release-digest&quot;&gt;https://github.com/sajjad037/visual-studio-learn-code/tree/main/topic-04-release-digest&lt;/a&gt;&lt;/p&gt;&lt;h2&gt;Sources&lt;/h2&gt;&lt;ul&gt;&lt;li&gt;CPython releases feed — &lt;a href=&quot;https://github.com/python/cpython/releases.atom&quot;&gt;https://github.com/python/cpython/releases.atom&lt;/a&gt; (latest at prep: v3.13.18, 2026-10-25)&lt;/li&gt;&lt;li&gt;FastAPI releases feed — &lt;a href=&quot;https://github.com/tiangolo/fastapi/releases.atom&quot;&gt;https://github.com/tiangolo/fastapi/releases.atom&lt;/a&gt; (latest at prep: 0.162.0, 2026-10-23)&lt;/li&gt;&lt;li&gt;Azure CLI releases feed — &lt;a href=&quot;https://github.com/Azure/azure-cli/releases.atom&quot;&gt;https://github.com/Azure/azure-cli/releases.atom&lt;/a&gt; (latest at prep: Azure CLI 2.91.0, 2026-10-05)&lt;/li&gt;&lt;li&gt;GitHub REST API rate limits (60 requests/hour unauthenticated, the reason this agent uses feeds) — &lt;a href=&quot;https://api.github.com/rate_limit&quot;&gt;https://api.github.com/rate_limit&lt;/a&gt; and GitHub&#39;s API docs&lt;/li&gt;&lt;li&gt;Gemini models (gemini-3.6-flash listed as Stable) — &lt;a href=&quot;https://ai.google.dev/gemini-api/docs/models&quot;&gt;https://ai.google.dev/gemini-api/docs/models&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Gemini API pricing ($0.75 / $3.75 per 1M tokens for gemini-3.6-flash through Dec 31, 2026) — &lt;a href=&quot;https://ai.google.dev/gemini-api/docs/pricing&quot;&gt;https://ai.google.dev/gemini-api/docs/pricing&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Package versions — &lt;a href=&quot;https://pypi.org/project/requests/&quot;&gt;https://pypi.org/project/requests/&lt;/a&gt; (2.34.2), &lt;a href=&quot;https://pypi.org/project/google-genai/&quot;&gt;https://pypi.org/project/google-genai/&lt;/a&gt; (2.33.0)&lt;/li&gt;&lt;/ul&gt;&lt;div style=&quot;text-align:center;border:1px solid #e3e6ea;border-radius:8px;padding:18px 14px;margin:28px 0;background:#fbfcfd;&quot;&gt;&lt;h3 style=&quot;margin:0 0 6px;&quot;&gt;Enjoying these tutorials?&lt;/h3&gt;&lt;p style=&quot;margin:0 0 6px;&quot;&gt;Get new Visual Studio Learn posts by email — free, no spam:&lt;/p&gt;&lt;style&gt;
    .followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview {
  display: flex !important;
  flex-direction: column !important;
  justify-content: center !important;
  margin-top: 30px !important;
  padding: clamp(17px, 5%, 40px) clamp(17px, 7%, 50px) !important;
  max-width: none !important;
  border-radius: 6px !important;
  box-shadow: 0 5px 25px rgba(34, 60, 47, 0.25) !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview,
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview *{
  box-sizing: border-box !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-heading {
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-heading h5{
  margin-top: 0 !important;
  margin-bottom: 0 !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field {
  margin-top: 20px !important;
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input {
  width: 100% !important;
  height: 40px !important;
  border-radius: 6px !important;
  border: 2px solid #e9e8e8 !important;
  background-color: #fff;
  outline: none !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input {
  color: #000000 !important;
  font-family: &quot;Montserrat&quot; !important;
  font-size: 14px;
  font-weight: 400;
  line-height: 20px;
  text-align: center;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input::placeholder {
  color: #000000 !important;
  opacity: 1 !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input:-ms-input-placeholder {
  color: #000000 !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input::-ms-input-placeholder {
  color: #000000 !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button {
  margin-top: 10px !important;
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button button {
  width: 100% !important;
  height: 40px !important;
  border: 0 !important;
  border-radius: 6px !important;
  line-height: 0px !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button button:hover {
  cursor: pointer !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .powered-by-line {
  color: #231f20 !important;
  font-family: &quot;Montserrat&quot; !important;
  font-size: 13px !important;
  font-weight: 400 !important;
  line-height: 25px !important;
  text-align: center !important;
  text-decoration: none !important;
  display: flex !important;
  width: 100% !important;
  justify-content: center !important;
  align-items: center;
  margin-top: 10px !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .powered-by-line img {
  margin-left: 10px !important;
  height: 1.13em !important;
  max-height: 1.13em !important;
}
  &lt;/style&gt;
  &lt;div class=&quot;followit--follow-form-container&quot; attr-a attr-b attr-c attr-d attr-e attr-f&gt;
    &lt;form data-v-f93f6730=&quot;&quot; action=&quot;https://api.follow.it/subscription-form/SmRpWjEzV1ovU0FOK2R2Y0oxajFBUmtIdW44K1ZiUWZRUm1FZVdpVlRKUHFLSFBKWjE4M1hTSk5yRU5oNmg0RU5zelhuTllNTGU1UmwzRkN0NzZWbnd2YkV5VWFwYnptWFdtekI3eXdwMmhtamtUNit3dlNSblVmRjJxd1JGbjZ8Q05yQWR2eitvYW5HRmNMMzJkbStpM09rNmxXZ0ZjWWhyOERya2VvVXRzZz0=/8&quot; method=&quot;post&quot;&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;form-preview&quot; style=&quot;background-color: rgb(255, 255, 255); border-style: solid; border-width: 1px; border-color: rgb(204, 204, 204); position: relative;&quot;&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;preview-heading&quot;&gt;&lt;h5 data-v-f93f6730=&quot;&quot; style=&quot;font-family: Arial; font-weight: bold; color: rgb(0, 0, 0); font-size: 16px; text-align: center; text-transform: none !important;&quot;&gt;Get new posts by email:&lt;/h5&gt;&lt;/div&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;preview-input-field&quot;&gt;&lt;input data-v-f93f6730=&quot;&quot; type=&quot;email&quot; name=&quot;email&quot; required=&quot;&quot; placeholder=&quot;Enter your email&quot; spellcheck=&quot;false&quot; style=&quot;font-family: Arial; font-weight: normal; color: rgb(0, 0, 0); font-size: 14px; text-align: center; background-color: rgb(255, 255, 255); text-transform: none !important;&quot; /&gt;&lt;/div&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;preview-submit-button&quot;&gt;&lt;button data-v-f93f6730=&quot;&quot; type=&quot;submit&quot; style=&quot;font-family: Arial; font-weight: bold; color: rgb(255, 255, 255); font-size: 16px; text-align: center; background-color: rgb(0, 0, 0); text-transform: none !important;&quot;&gt;Subscribe&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/form&gt;&lt;a href=&quot;https://follow.it&quot; class=&quot;powered-by-line&quot;&gt;Powered by &lt;img src=&quot;https://follow.it/images/colored-logo.svg&quot; alt=&quot;follow.it&quot; height=&quot;17px&quot;/&gt;&lt;/a&gt;
  &lt;/div&gt;&lt;p style=&quot;margin:12px 0 0;font-size:14px;&quot;&gt;Follow along: &lt;a href=&quot;https://www.youtube.com/@VisualStudioLearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/youtube/FF0000&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;YouTube&quot; /&gt; YouTube&lt;/a&gt; · &lt;a href=&quot;https://www.facebook.com/VisualStudio.Learn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/facebook/1877F2&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;Facebook&quot; /&gt; Facebook&lt;/a&gt; · &lt;a href=&quot;https://www.instagram.com/visualstudiolearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/instagram/E4405F&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;Instagram&quot; /&gt; Instagram&lt;/a&gt; · &lt;a href=&quot;https://www.tiktok.com/@visualstudiolearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/tiktok/000000&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;TikTok&quot; /&gt; TikTok&lt;/a&gt; · &lt;a href=&quot;https://www.visualstudiolearn.com/feeds/posts/default?alt=rss&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/rss/FFA500&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;RSS&quot; /&gt; RSS&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.visualstudiolearn.com/feeds/7852476395742725949/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.visualstudiolearn.com/2026/10/i-built-ai-agent-that-reads-release.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/7852476395742725949'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/7852476395742725949'/><link rel='alternate' type='text/html' href='http://www.visualstudiolearn.com/2026/10/i-built-ai-agent-that-reads-release.html' title='I Built an AI Agent That Reads Release Notes So I Don&#39;t Have To'/><author><name>The Loonie Lesson</name><uri>http://www.blogger.com/profile/02243798795439866634</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRY4V9cFLhqMxHWBOWCGBiGUqjan14Vda_dy5rz-C41kApceM-u6jAxS2_6AIRZtGkQeyiQolq1gCeSBhpo573lYq37MLmMGgSKDQLasowic65VoitivYCzqrmUDUqxJuqCTyzah8JuALYtW0Iz7rXIvIcj-ovhVpEuEhQJ6yG1FUQ-A/s1600/loonie-lesson-logo-profile.png'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8861905861585113921.post-670509940866109806</id><published>2026-10-24T09:07:00.000-04:00</published><updated>2026-11-22T02:50:49.472-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="AI Agents"/><category scheme="http://www.blogger.com/atom/ns#" term="FastAPI"/><category scheme="http://www.blogger.com/atom/ns#" term="Gemini"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><category scheme="http://www.blogger.com/atom/ns#" term="Small AI Apps"/><category scheme="http://www.blogger.com/atom/ns#" term="Tutorials"/><title type='text'>Small AI App #1: Build a PDF Q&amp;A App with FastAPI + Gemini (Upload a PDF, Ask It Questions)</title><content type='html'>
&lt;!-- Topic #3 — Small AI App #1: Build a PDF Q&amp;A App with FastAPI + Gemini (Upload a PDF, Ask It Questions)
     Labels: AI Agents, Python, FastAPI, Gemini, Small AI Apps, Tutorials
     Search description: Build a PDF Q&amp;A app with FastAPI and Gemini: upload a PDF, extract and chunk its text, ask questions, and see the real token cost per answer.
     Permalink slug: small-ai-app-1-build-pdf-qa-app-with --&gt;
&lt;p&gt;&lt;strong&gt;Versions used in this post — read this first:&lt;/strong&gt; Python 3.12.3 · FastAPI 0.161.1 · uvicorn 0.54.0 · pypdf 6.21.2 · google-genai 2.33.0 · python-multipart 0.0.32 · Model: &lt;code&gt;gemini-3.8-flash&lt;/code&gt;. Everything runs from one file, &lt;code&gt;app.py&lt;/code&gt;, and the code below is the exact code that was run and tested for this post.&lt;/p&gt;
&lt;!-- VIDEO EMBED: at staging, replace VIDEO_ID in the src below with the uploaded video&#39;s YouTube ID. --&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;iframe allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/QWOKffkimuU&quot; width=&quot;560&quot;&gt;&lt;/iframe&gt;&lt;/div&gt;
&lt;h2&gt;The direct answer&lt;/h2&gt;
&lt;p&gt;Yes — one small FastAPI file is enough. Upload a PDF and the server extracts its text with pypdf, splits it into overlapping chunks, and stores them in memory. Ask a question and it picks the chunks sharing the most words with it, sends only those to Gemini with a strict instruction to answer from them alone, and returns the answer with the token counts the API reported and what they cost. No vector database, nothing on top of FastAPI — and if no chunk matches, the app does not call the model at all.&lt;/p&gt;
&lt;p&gt;This is the first episode of &lt;strong&gt;Small AI Apps&lt;/strong&gt;: complete, runnable applications built with FastAPI and Gemini, each small enough to finish in under an hour and honest about what each run costs.&lt;/p&gt;
&lt;h2&gt;What you will build&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;POST /upload&lt;/code&gt; — accepts one PDF, extracts and chunks its text, returns a document id plus the real page, character, and chunk counts.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;POST /ask&lt;/code&gt; — answers one question from the best-matching chunks and reports chunks used, input tokens, output tokens, and estimated cost.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;GET /&lt;/code&gt; — a one-page web UI: pick a file, upload it, ask questions, read the cost line under every answer.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;GET /health&lt;/code&gt; — a liveness check, because every app you run should have one.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Two design choices are deliberate. Retrieval is &lt;strong&gt;keyword overlap, not embeddings&lt;/strong&gt;: simpler, free, and honest about its limits — it finds chunks that use your question&#39;s words and can miss a paraphrase. Storage is &lt;strong&gt;in memory&lt;/strong&gt;: restart the server and uploads are gone. Both are right-sized for a first app, and both return at the end as the first production upgrades.&lt;/p&gt;
&lt;h2&gt;Get the code&lt;/h2&gt;
&lt;p&gt;All the code in this post lives in the companion GitHub repository, in the &lt;a href=&quot;https://github.com/sajjad037/visual-studio-learn-code/tree/main/topic-03-pdf-qa-app&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;topic-03-pdf-qa-app&lt;/a&gt; folder — the exact &lt;code&gt;app.py&lt;/code&gt; printed below, the pinned requirements file, and a README that covers prerequisites, environment setup including the key walkthrough, install, and how to run and use the app. If you would rather follow along here, every step is also written out below.&lt;/p&gt;
&lt;h2&gt;Environment setup — do this first&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Prerequisites:&lt;/strong&gt; Python 3.10 or newer (this post uses 3.12.3), a terminal, and a Gemini API key. The free tier is enough; no card is needed.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Get your Gemini API key, click by click:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Go to &lt;strong&gt;aistudio.google.com&lt;/strong&gt; and sign in with a Google account.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Get API key&lt;/strong&gt; in the left sidebar (the key icon).&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Create API key&lt;/strong&gt;, choose or create a Google Cloud project when asked, and click &lt;strong&gt;Create&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Copy the key that appears — it stays visible in that same key list.&lt;/li&gt;
&lt;li&gt;Keep it secret like a password: never paste it into code, a screenshot, or a public repository. This app reads it only from an environment variable.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Create the project and install the pinned dependencies:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;mkdir pdf-qa &amp;amp;&amp;amp; cd pdf-qa
python3 -m venv .venv &amp;amp;&amp;amp; source .venv/bin/activate
# Windows PowerShell: py -m venv .venv ; .venv\Scripts\Activate.ps1
pip install fastapi==0.161.1 &amp;quot;uvicorn[standard]==0.54.0&amp;quot; pypdf==6.21.2 google-genai==2.33.0 python-multipart==0.0.32
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(&lt;code&gt;python-multipart&lt;/code&gt; is easy to forget: without it, FastAPI cannot accept file uploads at all.)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Set the key, then verify the setup:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;export GEMINI_API_KEY=&amp;quot;paste-your-key-here&amp;quot;
# Windows PowerShell: $env:GEMINI_API_KEY = &amp;quot;paste-your-key-here&amp;quot;
python -c &amp;quot;import fastapi, pypdf, google.genai; print(&amp;#x27;setup ok&amp;#x27;)&amp;quot;
&lt;/code&gt;&lt;/pre&gt;


&lt;h2&gt;Step 1 — The document pipeline: extract, chunk, retrieve&lt;/h2&gt;
&lt;p&gt;Save the code in this step and the next two as one file, &lt;code&gt;app.py&lt;/code&gt;. This first part turns an uploaded PDF into searchable chunks:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&amp;quot;&amp;quot;&amp;quot;Small AI App #1: a PDF Q&amp;amp;A app built with FastAPI and Gemini.

Companion code for the Visual Studio Learn post and video
&amp;quot;Small AI App #1: Build a PDF Q&amp;amp;A App with FastAPI + Gemini&amp;quot;.

Upload a PDF, the app extracts and chunks its text, and each question is
answered by Gemini using only the chunks that match the question — with
the token count and estimated cost of every answer reported back.

Requires: Python 3.12, the packages in requirements.txt, and a Gemini
API key in the GEMINI_API_KEY environment variable. See README.md.
&amp;quot;&amp;quot;&amp;quot;

import logging
import os
import re
import uuid
from dataclasses import dataclass, field
from io import BytesIO

from fastapi import FastAPI, File, HTTPException, UploadFile
from fastapi.responses import HTMLResponse
from google import genai
from pydantic import BaseModel
from pypdf import PdfReader

logger = logging.getLogger(&amp;quot;pdf_qa&amp;quot;)
logging.basicConfig(level=logging.INFO)

MODEL = &amp;quot;gemini-3.8-flash&amp;quot;
MAX_FILE_BYTES = 10 * 1024 * 1024  # 10 MB upload limit
CHUNK_SIZE = 1000  # characters per chunk
CHUNK_OVERLAP = 150  # characters shared between neighbouring chunks
TOP_CHUNKS = 4  # chunks sent to the model with each question

# Published pay-as-you-go rates for gemini-3.8-flash, US$ per 1M tokens.
INPUT_RATE_PER_MILLION = 0.75
OUTPUT_RATE_PER_MILLION = 3.75


@dataclass
class StoredDocument:
    &amp;quot;&amp;quot;&amp;quot;One uploaded PDF: its name, page count, and text chunks.&amp;quot;&amp;quot;&amp;quot;

    filename: str
    pages: int
    chunks: list[str] = field(default_factory=list)


# In-memory store: doc_id -&amp;gt; document. Deliberately simple — restarting
# the server forgets every upload, which is fine for a learning app and
# is the first thing to replace (with real storage) in a production one.
DOCUMENTS: dict[str, StoredDocument] = {}

app = FastAPI(title=&amp;quot;PDF Q&amp;amp;A — Small AI App #1&amp;quot;)


def extract_text(pdf_bytes: bytes) -&amp;gt; tuple[str, int]:
    &amp;quot;&amp;quot;&amp;quot;Extract all text from PDF bytes. Returns (text, page_count).

    Raises ValueError when the file cannot be parsed as a PDF at all.
    A scanned, image-only PDF parses fine but yields almost no text —
    the caller treats that as its own, separate failure.
    &amp;quot;&amp;quot;&amp;quot;
    try:
        reader = PdfReader(BytesIO(pdf_bytes))
    except Exception as exc:
        raise ValueError(f&amp;quot;Could not read that file as a PDF: {exc}&amp;quot;) from exc
    pages = [page.extract_text() or &amp;quot;&amp;quot; for page in reader.pages]
    return &amp;quot;\n&amp;quot;.join(pages), len(pages)


def chunk_text(text: str, size: int = CHUNK_SIZE, overlap: int = CHUNK_OVERLAP) -&amp;gt; list[str]:
    &amp;quot;&amp;quot;&amp;quot;Split text into overlapping character chunks.

    Overlap exists so a sentence that straddles a chunk boundary still
    appears whole in at least one chunk the model can read.
    &amp;quot;&amp;quot;&amp;quot;
    cleaned = re.sub(r&amp;quot;\s+&amp;quot;, &amp;quot; &amp;quot;, text).strip()
    chunks = []
    start = 0
    while start &amp;lt; len(cleaned):
        chunks.append(cleaned[start : start + size])
        start += size - overlap
    return [c for c in chunks if c.strip()]


STOPWORDS = {
    &amp;quot;the&amp;quot;, &amp;quot;and&amp;quot;, &amp;quot;for&amp;quot;, &amp;quot;are&amp;quot;, &amp;quot;was&amp;quot;, &amp;quot;what&amp;quot;, &amp;quot;when&amp;quot;, &amp;quot;where&amp;quot;, &amp;quot;which&amp;quot;,
    &amp;quot;does&amp;quot;, &amp;quot;did&amp;quot;, &amp;quot;how&amp;quot;, &amp;quot;why&amp;quot;, &amp;quot;who&amp;quot;, &amp;quot;this&amp;quot;, &amp;quot;that&amp;quot;, &amp;quot;with&amp;quot;, &amp;quot;from&amp;quot;,
    &amp;quot;have&amp;quot;, &amp;quot;has&amp;quot;, &amp;quot;can&amp;quot;, &amp;quot;its&amp;quot;, &amp;quot;it&amp;quot;, &amp;quot;is&amp;quot;, &amp;quot;in&amp;quot;, &amp;quot;on&amp;quot;, &amp;quot;of&amp;quot;, &amp;quot;to&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;an&amp;quot;,
}


def _meaningful_words(text: str) -&amp;gt; set[str]:
    &amp;quot;&amp;quot;&amp;quot;Lowercase word set with stopwords removed.

    Without this, a question like &amp;quot;What is the…&amp;quot; matches every chunk in
    the document on the words &amp;quot;what&amp;quot; and &amp;quot;the&amp;quot; alone, and the scoring
    below would measure grammar instead of topic.
    &amp;quot;&amp;quot;&amp;quot;
    return {
        w
        for w in re.findall(r&amp;quot;[a-z0-9]+&amp;quot;, text.lower())
        if len(w) &amp;gt; 2 and w not in STOPWORDS
    }


def relevant_chunks(chunks: list[str], question: str, limit: int = TOP_CHUNKS) -&amp;gt; list[str]:
    &amp;quot;&amp;quot;&amp;quot;Pick the chunks that share the most words with the question.

    This is plain keyword overlap, not semantic search — no embeddings,
    no vector database. It is honest about its limits: it finds chunks
    that use the question&amp;#x27;s words, and misses paraphrases. That trade is
    the right size for a first app; embeddings are the upgrade path.
    &amp;quot;&amp;quot;&amp;quot;
    words = _meaningful_words(question)
    if not words:
        return []
    scored = []
    for index, chunk in enumerate(chunks):
        score = len(words &amp;amp; _meaningful_words(chunk))
        if score:
            scored.append((score, index, chunk))
    scored.sort(key=lambda item: (-item[0], item[1]))
    return [chunk for _, _, chunk in scored[:limit]]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Three details carry this section. &lt;strong&gt;Chunks overlap by 150 characters&lt;/strong&gt; so a sentence that straddles a boundary still appears whole somewhere the model can read it. &lt;strong&gt;Stopwords are stripped before scoring&lt;/strong&gt; — without that, “What is the watering schedule?” matches every chunk on &lt;em&gt;what&lt;/em&gt;, &lt;em&gt;is&lt;/em&gt;, and &lt;em&gt;the&lt;/em&gt;, and the scoring measures grammar instead of topic; that bug appeared in the first test run of this exact code and was fixed before publication. And &lt;strong&gt;extraction and “no text found” are different failures&lt;/strong&gt;: a corrupt file raises in &lt;code&gt;extract_text&lt;/code&gt;, while a scanned PDF parses fine and yields no words — Step 2 gives that case its own, clearer error.&lt;/p&gt;


&lt;h2&gt;Step 2 — The two endpoints&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;class AskRequest(BaseModel):
    doc_id: str
    question: str


@app.get(&amp;quot;/health&amp;quot;)
def health() -&amp;gt; dict:
    &amp;quot;&amp;quot;&amp;quot;Tiny liveness check: confirms the server is up.&amp;quot;&amp;quot;&amp;quot;
    return {&amp;quot;status&amp;quot;: &amp;quot;ok&amp;quot;, &amp;quot;documents_loaded&amp;quot;: len(DOCUMENTS)}


@app.post(&amp;quot;/upload&amp;quot;)
async def upload(file: UploadFile = File(...)) -&amp;gt; dict:
    &amp;quot;&amp;quot;&amp;quot;Accept one PDF, extract and chunk its text, and store it.

    Returns the document id the client must send with every question,
    plus the page and chunk counts so the reader can see the pipeline
    actually did the work.
    &amp;quot;&amp;quot;&amp;quot;
    if not (file.filename or &amp;quot;&amp;quot;).lower().endswith(&amp;quot;.pdf&amp;quot;):
        raise HTTPException(status_code=400, detail=&amp;quot;Please upload a PDF file.&amp;quot;)
    pdf_bytes = await file.read()
    if len(pdf_bytes) &amp;gt; MAX_FILE_BYTES:
        raise HTTPException(
            status_code=413,
            detail=f&amp;quot;That file is over the {MAX_FILE_BYTES // (1024 * 1024)} MB limit.&amp;quot;,
        )
    try:
        text, page_count = extract_text(pdf_bytes)
    except ValueError as exc:
        raise HTTPException(status_code=400, detail=str(exc)) from exc
    chunks = chunk_text(text)
    if not chunks:
        raise HTTPException(
            status_code=422,
            detail=(
                &amp;quot;No readable text was found in that PDF. Scanned or &amp;quot;
                &amp;quot;image-only PDFs need OCR before an app like this can &amp;quot;
                &amp;quot;read them.&amp;quot;
            ),
        )
    doc_id = uuid.uuid4().hex[:8]
    DOCUMENTS[doc_id] = StoredDocument(
        filename=file.filename, pages=page_count, chunks=chunks
    )
    logger.info(&amp;quot;stored %s: %d pages, %d chunks&amp;quot;, file.filename, page_count, len(chunks))
    return {
        &amp;quot;doc_id&amp;quot;: doc_id,
        &amp;quot;filename&amp;quot;: file.filename,
        &amp;quot;pages&amp;quot;: page_count,
        &amp;quot;characters&amp;quot;: len(text),
        &amp;quot;chunks&amp;quot;: len(chunks),
    }


@app.post(&amp;quot;/ask&amp;quot;)
def ask(request: AskRequest) -&amp;gt; dict:
    &amp;quot;&amp;quot;&amp;quot;Answer one question from the stored chunks of one document.

    Only the best-matching chunks are sent to Gemini — never the whole
    document — which is what keeps each question cheap and inside the
    model&amp;#x27;s context limits. The response carries the real token counts
    the API reported and the cost they work out to.
    &amp;quot;&amp;quot;&amp;quot;
    document = DOCUMENTS.get(request.doc_id)
    if document is None:
        raise HTTPException(
            status_code=404,
            detail=&amp;quot;Unknown document. Upload a PDF first (uploads are forgotten when the server restarts).&amp;quot;,
        )
    chunks = relevant_chunks(document.chunks, request.question)
    if not chunks:
        return {
            &amp;quot;answer&amp;quot;: (
                &amp;quot;No passage in this document shares words with that &amp;quot;
                &amp;quot;question, so nothing was sent to the model. Try &amp;quot;
                &amp;quot;rephrasing with words the document itself uses.&amp;quot;
            ),
            &amp;quot;chunks_used&amp;quot;: 0,
            &amp;quot;input_tokens&amp;quot;: 0,
            &amp;quot;output_tokens&amp;quot;: 0,
            &amp;quot;estimated_cost_usd&amp;quot;: 0.0,
        }

    context = &amp;quot;\n\n---\n\n&amp;quot;.join(chunks)
    prompt = (
        &amp;quot;Answer the question using only the document passages below. &amp;quot;
        &amp;quot;If the passages do not contain the answer, say so plainly &amp;quot;
        &amp;quot;instead of guessing.\n\n&amp;quot;
        f&amp;quot;Passages:\n{context}\n\nQuestion: {request.question}&amp;quot;
    )
    api_key = os.environ.get(&amp;quot;GEMINI_API_KEY&amp;quot;)
    if not api_key:
        raise HTTPException(
            status_code=500,
            detail=&amp;quot;GEMINI_API_KEY is not set on the server. See the setup steps in the README.&amp;quot;,
        )
    try:
        client = genai.Client(api_key=api_key)
        response = client.models.generate_content(model=MODEL, contents=prompt)
    except Exception as exc:  # surface API failures as a clean 502, not a stack trace
        logger.exception(&amp;quot;Gemini call failed&amp;quot;)
        raise HTTPException(status_code=502, detail=f&amp;quot;Gemini API error: {exc}&amp;quot;) from exc

    usage = response.usage_metadata
    input_tokens = usage.prompt_token_count or 0
    output_tokens = usage.candidates_token_count or 0
    cost = (
        input_tokens * INPUT_RATE_PER_MILLION
        + output_tokens * OUTPUT_RATE_PER_MILLION
    ) / 1_000_000
    logger.info(
        &amp;quot;ask on %s: %d chunks, %d in / %d out tokens&amp;quot;,
        request.doc_id,
        len(chunks),
        input_tokens,
        output_tokens,
    )
    return {
        &amp;quot;answer&amp;quot;: response.text,
        &amp;quot;chunks_used&amp;quot;: len(chunks),
        &amp;quot;input_tokens&amp;quot;: input_tokens,
        &amp;quot;output_tokens&amp;quot;: output_tokens,
        &amp;quot;estimated_cost_usd&amp;quot;: round(cost, 6),
    }
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Read &lt;code&gt;/ask&lt;/code&gt; closely, because it is where the money is spent — and saved. Only the four best-matching chunks ever reach the model, never the whole document, which keeps each question small whatever the PDF’s size. If &lt;strong&gt;no&lt;/strong&gt; chunk matches, the endpoint answers without an API call: in testing, asking the sample handbook &lt;em&gt;“What is the airspeed velocity of an unladen swallow?”&lt;/em&gt; returned exactly that no-passage answer with &lt;code&gt;chunks_used: 0&lt;/code&gt; and zero tokens — grounding enforced by code, not by hoping the prompt behaves. When a call does happen, the answer returns with the API’s own usage numbers, and the cost is simple arithmetic at the published &lt;code&gt;gemini-3.8-flash&lt;/code&gt; rates (US$0.75 per million input tokens, US$3.75 per million output). In the live run for this post, the question &lt;em&gt;“When is the lottery draw, and what does a plot cost?”&lt;/em&gt; came back with the handbook’s own facts — the draw takes place on March 30, 2026, and each plot costs 45 dollars for the season — answered from a single chunk at 277 input tokens and 35 output tokens: an estimated &lt;strong&gt;US$0.000339&lt;/strong&gt;, roughly a thirtieth of a cent. The app prints the exact figure for &lt;em&gt;your&lt;/em&gt; question. API failures return as a clean &lt;code&gt;502&lt;/code&gt; carrying the API’s own error text, and a missing key returns a &lt;code&gt;500&lt;/code&gt; that names exactly what to set — both quoted verbatim below.&lt;/p&gt;


&lt;h2&gt;Step 3 — The web page&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;@app.get(&amp;quot;/&amp;quot;, response_class=HTMLResponse)
def home() -&amp;gt; str:
    &amp;quot;&amp;quot;&amp;quot;Serve the one-page UI: upload a PDF, then ask it questions.&amp;quot;&amp;quot;&amp;quot;
    return &amp;quot;&amp;quot;&amp;quot;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang=&amp;quot;en&amp;quot;&amp;gt;
&amp;lt;head&amp;gt;
&amp;lt;meta charset=&amp;quot;utf-8&amp;quot;&amp;gt;
&amp;lt;title&amp;gt;PDF Q&amp;amp;amp;A — Small AI App #1&amp;lt;/title&amp;gt;
&amp;lt;style&amp;gt;
  body { font-family: system-ui, sans-serif; max-width: 720px; margin: 40px auto; padding: 0 16px; }
  h1 { font-size: 24px; }
  .box { border: 1px solid #ddd; border-radius: 8px; padding: 16px; margin: 16px 0; }
  #answer { white-space: pre-wrap; }
  .meta { color: #555; font-size: 14px; }
  button { padding: 8px 16px; cursor: pointer; }
  input[type=text] { width: 75%; padding: 8px; }
&amp;lt;/style&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
&amp;lt;h1&amp;gt;PDF Q&amp;amp;amp;A&amp;lt;/h1&amp;gt;
&amp;lt;p&amp;gt;Upload a PDF, then ask it questions. Answers come only from the
document, and every answer shows what it cost.&amp;lt;/p&amp;gt;

&amp;lt;div class=&amp;quot;box&amp;quot;&amp;gt;
  &amp;lt;input type=&amp;quot;file&amp;quot; id=&amp;quot;pdf&amp;quot; accept=&amp;quot;application/pdf&amp;quot;&amp;gt;
  &amp;lt;button onclick=&amp;quot;uploadPdf()&amp;quot;&amp;gt;Upload PDF&amp;lt;/button&amp;gt;
  &amp;lt;p class=&amp;quot;meta&amp;quot; id=&amp;quot;uploadStatus&amp;quot;&amp;gt;No document uploaded yet.&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;

&amp;lt;div class=&amp;quot;box&amp;quot;&amp;gt;
  &amp;lt;input type=&amp;quot;text&amp;quot; id=&amp;quot;question&amp;quot; placeholder=&amp;quot;Ask a question about the document&amp;quot;&amp;gt;
  &amp;lt;button onclick=&amp;quot;askQuestion()&amp;quot;&amp;gt;Ask&amp;lt;/button&amp;gt;
  &amp;lt;p id=&amp;quot;answer&amp;quot;&amp;gt;&amp;lt;/p&amp;gt;
  &amp;lt;p class=&amp;quot;meta&amp;quot; id=&amp;quot;askMeta&amp;quot;&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;

&amp;lt;script&amp;gt;
let docId = null;

async function uploadPdf() {
  const fileInput = document.getElementById(&amp;#x27;pdf&amp;#x27;);
  const status = document.getElementById(&amp;#x27;uploadStatus&amp;#x27;);
  if (!fileInput.files.length) { status.textContent = &amp;#x27;Choose a PDF first.&amp;#x27;; return; }
  const form = new FormData();
  form.append(&amp;#x27;file&amp;#x27;, fileInput.files[0]);
  status.textContent = &amp;#x27;Uploading and reading…&amp;#x27;;
  const res = await fetch(&amp;#x27;/upload&amp;#x27;, { method: &amp;#x27;POST&amp;#x27;, body: form });
  const data = await res.json();
  if (!res.ok) { status.textContent = &amp;#x27;Upload failed: &amp;#x27; + data.detail; return; }
  docId = data.doc_id;
  status.textContent = &amp;#x27;Loaded &amp;quot;&amp;#x27; + data.filename + &amp;#x27;&amp;quot;: &amp;#x27; + data.pages +
    &amp;#x27; pages, &amp;#x27; + data.characters + &amp;#x27; characters, split into &amp;#x27; +
    data.chunks + &amp;#x27; chunks. You can ask questions now.&amp;#x27;;
}

async function askQuestion() {
  const answer = document.getElementById(&amp;#x27;answer&amp;#x27;);
  const meta = document.getElementById(&amp;#x27;askMeta&amp;#x27;);
  if (!docId) { answer.textContent = &amp;#x27;Upload a PDF first.&amp;#x27;; return; }
  const question = document.getElementById(&amp;#x27;question&amp;#x27;).value;
  answer.textContent = &amp;#x27;Thinking…&amp;#x27;;
  const res = await fetch(&amp;#x27;/ask&amp;#x27;, {
    method: &amp;#x27;POST&amp;#x27;,
    headers: { &amp;#x27;Content-Type&amp;#x27;: &amp;#x27;application/json&amp;#x27; },
    body: JSON.stringify({ doc_id: docId, question: question })
  });
  const data = await res.json();
  if (!res.ok) { answer.textContent = &amp;#x27;Error: &amp;#x27; + data.detail; meta.textContent = &amp;#x27;&amp;#x27;; return; }
  answer.textContent = data.answer;
  meta.textContent = &amp;#x27;Used &amp;#x27; + data.chunks_used + &amp;#x27; chunks · &amp;#x27; +
    data.input_tokens + &amp;#x27; input tokens · &amp;#x27; + data.output_tokens +
    &amp;#x27; output tokens · about $&amp;#x27; + data.estimated_cost_usd.toFixed(6);
}
&amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;&amp;quot;&amp;quot;&amp;quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The page is deliberately plain: one file input, one question box, and a status line that repeats the server’s own counts back to you. That line is your first debugging tool — it proves the server read the file &lt;em&gt;before&lt;/em&gt; you blame the model for a bad answer.&lt;/p&gt;


&lt;h2&gt;Step 4 — Run it and watch it work&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;uvicorn app:app --reload
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Open &lt;strong&gt;http://127.0.0.1:8000&lt;/strong&gt;. The test run behind this post uploaded a two-page community-garden handbook written as a sample document. The server’s real response:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-text&quot;&gt;{&quot;doc_id&quot;: &quot;979808d3&quot;, &quot;filename&quot;: &quot;sample.pdf&quot;, &quot;pages&quot;: 2, &quot;characters&quot;: 856, &quot;chunks&quot;: 2}
&lt;/code&gt;&lt;/pre&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;1080&quot; data-original-width=&quot;1920&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYLYITUJGToLf1DCMYbYTf9pJcYSq0jLTfFCH1etAhXG64KD5cw4xUoL04SPgeCXogMqwWmCIXyAm7wSvx_6AS12oZr3beQWcCp5O0-pytlLazq8kGpm5rHYiDcVlZdHwdKpWHN4hpYYJ-VV76g7p6ABGk_qV62aRdX22N2CpvLctU4QM0KiG0wE5B70k/s1600/screenshot-1-upload.png&quot; /&gt;&lt;/div&gt;
&lt;p&gt;Two pages became 856 characters became two chunks — small numbers, because the sample is small, but the pipeline is identical at 200 pages. The same flow works without the browser:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;curl -F &amp;quot;file=@sample.pdf&amp;quot; http://127.0.0.1:8000/upload
curl -X POST http://127.0.0.1:8000/ask -H &amp;quot;Content-Type: application/json&amp;quot; \
  -d &amp;#x27;{&amp;quot;doc_id&amp;quot;: &amp;quot;YOUR_DOC_ID&amp;quot;, &amp;quot;question&amp;quot;: &amp;quot;When is the lottery draw?&amp;quot;}&amp;#x27;
&lt;/code&gt;&lt;/pre&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;1080&quot; data-original-width=&quot;1920&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiJRZJ4ltxzfXxq_6Mt4Md63yFqph7q5JHZ81_OpICgRTdKIq3ou9Zu56omNzSj3pLfB_U91rbB5-8Xt3Czp17cwby5pkEa_M8Vwnkz1iloeEIb7nXaic8VQTO8SzuqNWlRpqhpo6tD_bEJteuqH13ljLRNrwTSxEcuTWUKYKMxY13_2rJEJwDEogaUn7w/s1600/screenshot-2-live-response.png&quot; /&gt;&lt;/div&gt;


&lt;h2&gt;Errors you may hit&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;422&lt;/code&gt; — “No readable text was found in that PDF.”&lt;/strong&gt; The file parsed but has no text layer — the classic scanned or image-only PDF. This app does not OCR; run the document through an OCR tool first. (Verified: an image-only test PDF returns exactly this.)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;413&lt;/code&gt; — “That file is over the 10 MB limit.”&lt;/strong&gt; The cap is a constant at the top of &lt;code&gt;app.py&lt;/code&gt;. Raise it deliberately if needed — a bigger PDF means more chunks, and retrieval, not the context window, keeps questions cheap.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;400&lt;/code&gt; — “Please upload a PDF file.”&lt;/strong&gt; The filename does not end in &lt;code&gt;.pdf&lt;/code&gt;. A renamed text file fails here or in extraction, with “Could not read that file as a PDF”.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;404&lt;/code&gt; — “Unknown document.”&lt;/strong&gt; The id is wrong, or the server restarted and forgot every upload — the in-memory store working as designed. Upload again.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;500&lt;/code&gt; — “GEMINI_API_KEY is not set on the server.”&lt;/strong&gt; The key is missing from the &lt;em&gt;server’s&lt;/em&gt; environment. Setting it in a different terminal does not count; set it in the window running &lt;code&gt;uvicorn&lt;/code&gt;, then restart.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;502&lt;/code&gt; — “Gemini API error: …”&lt;/strong&gt; The call failed — invalid key, exhausted free-tier quota, or a network problem. The text after the colon is the API’s own error; fix that, not the app.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;A &lt;code&gt;404&lt;/code&gt; inside that 502 — “This model … is no longer available to new users.”&lt;/strong&gt; Google retires model names for new keys first. If your key is recent and the error names a replacement model, update the &lt;code&gt;MODEL&lt;/code&gt; constant at the top of &lt;code&gt;app.py&lt;/code&gt; to the one it names. This post pins &lt;code&gt;gemini-3.8-flash&lt;/code&gt; for exactly this reason: the first test run pointed at &lt;code&gt;gemini-2.5-flash&lt;/code&gt;, and a freshly created key could not reach it at all.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The answer misses your question’s meaning.&lt;/strong&gt; Retrieval found chunks that share words but not intent, or the passage uses different words than your question. Rephrase with the document’s own vocabulary — or take it as the signal to build the embeddings upgrade below.&lt;/p&gt;
&lt;h2&gt;Where to go next&lt;/h2&gt;
&lt;p&gt;Three upgrades, in the order a production version needs them: &lt;strong&gt;persistent storage&lt;/strong&gt; (a database or disk store instead of the in-memory dictionary), &lt;strong&gt;embeddings-based retrieval&lt;/strong&gt; (semantic matching instead of word overlap), and &lt;strong&gt;authentication&lt;/strong&gt; (this app trusts everyone who can reach the port).&lt;/p&gt;
&lt;p&gt;Previous: MCP Servers Explained — Give Your AI Agent Real Tools. Next in Small AI Apps: decided by what readers actually run — tell us in the comments.&lt;/p&gt;


&lt;div style=&quot;text-align:center;border:1px solid #e3e6ea;border-radius:8px;padding:18px 14px;margin:28px 0;background:#fbfcfd;&quot;&gt;&lt;h3 style=&quot;margin:0 0 6px;&quot;&gt;Enjoying these tutorials?&lt;/h3&gt;&lt;p style=&quot;margin:0 0 6px;&quot;&gt;Get new Visual Studio Learn posts by email — free, no spam:&lt;/p&gt;&lt;style&gt;
    .followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview {
  display: flex !important;
  flex-direction: column !important;
  justify-content: center !important;
  margin-top: 30px !important;
  padding: clamp(17px, 5%, 40px) clamp(17px, 7%, 50px) !important;
  max-width: none !important;
  border-radius: 6px !important;
  box-shadow: 0 5px 25px rgba(34, 60, 47, 0.25) !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview,
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview *{
  box-sizing: border-box !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-heading {
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-heading h5{
  margin-top: 0 !important;
  margin-bottom: 0 !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field {
  margin-top: 20px !important;
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input {
  width: 100% !important;
  height: 40px !important;
  border-radius: 6px !important;
  border: 2px solid #e9e8e8 !important;
  background-color: #fff;
  outline: none !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input {
  color: #000000 !important;
  font-family: &quot;Montserrat&quot; !important;
  font-size: 14px;
  font-weight: 400;
  line-height: 20px;
  text-align: center;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input::placeholder {
  color: #000000 !important;
  opacity: 1 !important;
}

.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input:-ms-input-placeholder {
  color: #000000 !important;
}

.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input::-ms-input-placeholder {
  color: #000000 !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button {
  margin-top: 10px !important;
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button button {
  width: 100% !important;
  height: 40px !important;
  border: 0 !important;
  border-radius: 6px !important;
  line-height: 0px !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button button:hover {
  cursor: pointer !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .powered-by-line {
  color: #231f20 !important;
  font-family: &quot;Montserrat&quot; !important;
  font-size: 13px !important;
  font-weight: 400 !important;
  line-height: 25px !important;
  text-align: center !important;
  text-decoration: none !important;
  display: flex !important;
  width: 100% !important;
  justify-content: center !important;
  align-items: center !important;
  margin-top: 10px !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .powered-by-line img {
  margin-left: 10px !important;
  height: 1.13em !important;
  max-height: 1.13em !important;
}

  &lt;/style&gt;
  &lt;div class=&quot;followit--follow-form-container&quot; attr-a attr-b attr-c attr-d attr-e attr-f&gt;
    &lt;form data-v-f93f6730=&quot;&quot; action=&quot;https://api.follow.it/subscription-form/SmRpWjEzV1ovU0FOK2R2Y0oxajFBUmtIdW44K1ZiUWZRUm1FZVdpVlRKUHFLSFBKWjE4M1hTSk5yRU5oNmg0RU5zelhuTllNTGU1UmwzRkN0NzZWbnd2YkV5VWFwYnptWFdtekI3eXdwMmhtamtUNit3dlNSblVmRjJxd1JGbjZ8Q05yQWR2eitvYW5HRmNMMzJkbStpM09rNmxXZ0ZjWWhyOERya2VvVXRzZz0=/8&quot; method=&quot;post&quot;&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;form-preview&quot; style=&quot;background-color: rgb(255, 255, 255); border-style: solid; border-width: 1px; border-color: rgb(204, 204, 204); position: relative;&quot;&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;preview-heading&quot;&gt;&lt;h5 data-v-f93f6730=&quot;&quot; style=&quot;font-family: Arial; font-weight: bold; color: rgb(0, 0, 0); font-size: 16px; text-align: center; text-transform: none !important;&quot;&gt;Get new posts by email:&lt;/h5&gt;&lt;/div&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;preview-input-field&quot;&gt;&lt;input data-v-f93f6730=&quot;&quot; type=&quot;email&quot; name=&quot;email&quot; required=&quot;&quot; placeholder=&quot;Enter your email&quot; spellcheck=&quot;false&quot; style=&quot;font-family: Arial; font-weight: normal; color: rgb(0, 0, 0); font-size: 14px; text-align: center; background-color: rgb(255, 255, 255); text-transform: none !important;&quot; /&gt;&lt;/div&gt;&lt;div data-v-f93f6730=&quot;&quot; class=&quot;preview-submit-button&quot;&gt;&lt;button data-v-f93f6730=&quot;&quot; type=&quot;submit&quot; style=&quot;font-family: Arial; font-weight: bold; color: rgb(255, 255, 255); font-size: 16px; text-align: center; background-color: rgb(0, 0, 0); text-transform: none !important;&quot;&gt;Subscribe&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/form&gt;&lt;a href=&quot;https://follow.it&quot; class=&quot;powered-by-line&quot;&gt;Powered by &lt;img src=&quot;https://follow.it/images/colored-logo.svg&quot; alt=&quot;follow.it&quot; height=&quot;17px&quot;/&gt;&lt;/a&gt;
  &lt;/div&gt;&lt;p style=&quot;margin:12px 0 0;font-size:14px;&quot;&gt;Follow along: &lt;a href=&quot;https://www.youtube.com/@VisualStudioLearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/youtube/FF0000&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;YouTube&quot; /&gt; YouTube&lt;/a&gt; · &lt;a href=&quot;https://www.facebook.com/VisualStudio.Learn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/facebook/1877F2&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;Facebook&quot; /&gt; Facebook&lt;/a&gt; · &lt;a href=&quot;https://www.instagram.com/visualstudiolearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/instagram/E4405F&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;Instagram&quot; /&gt; Instagram&lt;/a&gt; · &lt;a href=&quot;https://www.tiktok.com/@visualstudiolearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/tiktok/000000&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;TikTok&quot; /&gt; TikTok&lt;/a&gt; · &lt;a href=&quot;https://www.visualstudiolearn.com/feeds/posts/default?alt=rss&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/rss/FFA500&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;RSS&quot; /&gt; RSS&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://www.visualstudiolearn.com/feeds/670509940866109806/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.visualstudiolearn.com/2026/10/small-ai-app-1-build-pdf-qa-app-with.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/670509940866109806'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/670509940866109806'/><link rel='alternate' type='text/html' href='http://www.visualstudiolearn.com/2026/10/small-ai-app-1-build-pdf-qa-app-with.html' title='Small AI App #1: Build a PDF Q&amp;A App with FastAPI + Gemini (Upload a PDF, Ask It Questions)'/><author><name>The Loonie Lesson</name><uri>http://www.blogger.com/profile/02243798795439866634</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRY4V9cFLhqMxHWBOWCGBiGUqjan14Vda_dy5rz-C41kApceM-u6jAxS2_6AIRZtGkQeyiQolq1gCeSBhpo573lYq37MLmMGgSKDQLasowic65VoitivYCzqrmUDUqxJuqCTyzah8JuALYtW0Iz7rXIvIcj-ovhVpEuEhQJ6yG1FUQ-A/s1600/loonie-lesson-logo-profile.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://img.youtube.com/vi/QWOKffkimuU/default.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8861905861585113921.post-8981327061337745507</id><published>2026-10-22T09:07:00.000-04:00</published><updated>2026-11-22T02:50:48.107-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="AI Agents"/><category scheme="http://www.blogger.com/atom/ns#" term="MCP"/><category scheme="http://www.blogger.com/atom/ns#" term="Model Context &#xa;Protocol"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><category scheme="http://www.blogger.com/atom/ns#" term="Tutorials"/><title type='text'>MCP Servers Explained: Give Your AI Agent Real Tools</title><content type='html'>&lt;p&gt;&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;div class=&quot;BLOG_html_snippet&quot;&gt;&lt;p&gt;&lt;strong&gt;Versions used in this post — read this first:&lt;/strong&gt; Python 3.12 · MCP Python SDK &lt;code&gt;mcp&lt;/code&gt; 2.3.0 (specification revision 2026-07-28) · Transport: stdio · No API key, no account. Version note that matters: SDK v2 renamed the class most older tutorials use — code importing &lt;code&gt;FastMCP&lt;/code&gt; was written for v1 and will not run on v2. This post uses &lt;code&gt;MCPServer&lt;/code&gt; throughout.&lt;/p&gt;
&lt;iframe allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/SA-J4OARAzk&quot; width=&quot;560&quot;&gt;&lt;/iframe&gt;
&lt;h2&gt;The direct answer&lt;/h2&gt;
&lt;p&gt;MCP — the Model Context Protocol — is a standard way for an AI application to discover and call tools that live outside the model. Instead of every framework inventing its own plugin format, a tool is packaged once as an MCP server, and any MCP-aware host can list its tools, read their input schemas, and call them over a defined protocol.&lt;/p&gt;
&lt;p&gt;In the previous post, the tools lived inside the same file as the agent. That works for one program. MCP is for when a tool should outlive it — usable by a second agent, a different host, or next month&#39;s project.&lt;/p&gt;
&lt;h2&gt;Host, client, server — in plain words&lt;/h2&gt;
&lt;p&gt;Three roles appear in every MCP setup, and most confusion comes from mixing them up:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Host&lt;/strong&gt; — the application the person uses: an AI coding assistant, a chat app with tool support, or your own agent program. It owns the model connection.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Client&lt;/strong&gt; — the connector inside the host, one per server. It opens the connection, performs the handshake, and forwards tool calls.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Server&lt;/strong&gt; — a separate program that offers tools and answers when called. It has no model of its own. Ours offers two tools over a local SQLite database. Client and server exchange JSON-RPC messages, here over &lt;strong&gt;stdio&lt;/strong&gt;: the server is a subprocess, talking over its standard input and output. Streamable HTTP exists for shared servers; stdio is the right start — no port, no hosting, nothing exposed beyond your machine.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;What you will build&lt;/h2&gt;
&lt;p&gt;A server named &lt;code&gt;project-notes&lt;/code&gt; with two read-only tools over a tiny notes database the code creates and seeds itself — &lt;strong&gt;search_notes&lt;/strong&gt; (finds notes containing a keyword) and &lt;strong&gt;recent_notes&lt;/strong&gt; (lists the newest) — plus a small Python client that starts the server and calls both: the same handshake a full AI host performs, with no model in between.&lt;/p&gt;
&lt;h2&gt;Get the code&lt;/h2&gt;
&lt;p&gt;All the code in this post lives in the companion GitHub repository, in the &lt;a href=&quot;https://github.com/sajjad037/visual-studio-learn-code/tree/main/topic-02-mcp-server&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;topic-02-mcp-server&lt;/a&gt; folder — with a README that covers prerequisites, environment setup, install, and how to run and use it. If you would rather follow along here, every step is also written out below.&lt;/p&gt;
&lt;h2&gt;Step 1 — Set up (no key, no account, nothing to buy)&lt;/h2&gt;
&lt;p&gt;This build needs &lt;strong&gt;no API key and no account&lt;/strong&gt;: an MCP server is ordinary local code, and testing it involves no model at all. If you later connect it to an AI host, signing in is the host&#39;s own setup.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Check you have Python 3.10 or newer (this post uses 3.12): run &lt;code&gt;python --version&lt;/code&gt;. On Windows, if the Store opens instead, install Python from python.org and tick &quot;Add python.exe to PATH&quot;.&lt;/li&gt;
&lt;li&gt;Create a folder called &lt;code&gt;mcp-notes&lt;/code&gt;, open a terminal in it, and create a virtual environment:
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;# macOS / Linux
python3 -m venv .venv &amp;amp;&amp;amp; source .venv/bin/activate

# Windows PowerShell
py -m venv .venv ; .venv\Scripts\Activate.ps1
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;Install the SDK, pinned (the &lt;code&gt;[cli]&lt;/code&gt; extra adds the &lt;code&gt;mcp&lt;/code&gt; command Step 3 uses):
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;pip install &quot;mcp[cli]==2.3.0&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;Step 2 — Write the server&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&quot;&quot;&quot;A tiny MCP server that searches a local project-notes database.&quot;&quot;&quot;

import json
import sqlite3
from pathlib import Path

from mcp.server import MCPServer

DB_PATH = Path(__file__).with_name(&quot;notes.db&quot;)

SEED_NOTES = [
    (&quot;Deploy checklist&quot;, &quot;Build the image, run the tests, tag the release, then deploy.&quot;, &quot;deploy&quot;),
    (&quot;Database backup&quot;, &quot;Take a full backup before any schema change and verify the restore.&quot;, &quot;database&quot;),
    (&quot;Grafana alert idea&quot;, &quot;Alert when the error rate stays above two percent for ten minutes.&quot;, &quot;monitoring&quot;),
    (&quot;FastAPI startup fix&quot;, &quot;The startup error came from a missing environment variable, not the code.&quot;, &quot;fastapi&quot;),
]


def _connect() -&amp;gt; sqlite3.Connection:
    &quot;&quot;&quot;Open the notes database, creating and seeding it on first use.&quot;&quot;&quot;
    conn = sqlite3.connect(DB_PATH)
    conn.execute(
        &quot;CREATE TABLE IF NOT EXISTS notes &quot;
        &quot;(id INTEGER PRIMARY KEY, title TEXT, body TEXT, tag TEXT)&quot;
    )
    count = conn.execute(&quot;SELECT COUNT(*) FROM notes&quot;).fetchone()[0]
    if count == 0:
        conn.executemany(
            &quot;INSERT INTO notes (title, body, tag) VALUES (?, ?, ?)&quot;, SEED_NOTES
        )
        conn.commit()
    return conn


mcp = MCPServer(&quot;project-notes&quot;)


@mcp.tool()
def search_notes(keyword: str, limit: int = 5) -&amp;gt; str:
    &quot;&quot;&quot;Search project notes for a keyword in the title or body.

    Args:
        keyword: Word or phrase to look for, for example &quot;backup&quot;.
        limit: Maximum number of notes to return, between 1 and 20.
    &quot;&quot;&quot;
    limit = max(1, min(limit, 20))
    like = f&quot;%{keyword}%&quot;
    with _connect() as conn:
        rows = conn.execute(
            &quot;SELECT id, title, body, tag FROM notes &quot;
            &quot;WHERE title LIKE ? OR body LIKE ? ORDER BY id LIMIT ?&quot;,
            (like, like, limit),
        ).fetchall()
    if not rows:
        return json.dumps({&quot;matches&quot;: [], &quot;message&quot;: f&quot;No notes match &#39;{keyword}&#39;.&quot;})
    return json.dumps(
        {
            &quot;matches&quot;: [
                {&quot;id&quot;: r[0], &quot;title&quot;: r[1], &quot;body&quot;: r[2], &quot;tag&quot;: r[3]} for r in rows
            ]
        }
    )


@mcp.tool()
def recent_notes(limit: int = 3) -&amp;gt; str:
    &quot;&quot;&quot;List the most recently added project notes.

    Args:
        limit: How many recent notes to return, between 1 and 20.
    &quot;&quot;&quot;
    limit = max(1, min(limit, 20))
    with _connect() as conn:
        rows = conn.execute(
            &quot;SELECT id, title, tag FROM notes ORDER BY id DESC LIMIT ?&quot;, (limit,)
        ).fetchall()
    return json.dumps(
        {&quot;notes&quot;: [{&quot;id&quot;: r[0], &quot;title&quot;: r[1], &quot;tag&quot;: r[2]} for r in rows]}
    )


if __name__ == &quot;__main__&quot;:
    mcp.run(transport=&quot;stdio&quot;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Save it as &lt;code&gt;server.py&lt;/code&gt;. Three things do more work than they appear to. &lt;strong&gt;The type hints are the contract:&lt;/strong&gt; &lt;code&gt;keyword: str&lt;/code&gt; and &lt;code&gt;limit: int = 5&lt;/code&gt; become the tool&#39;s JSON input schema automatically — no schema is written by hand. &lt;strong&gt;The docstring is the description the model reads&lt;/strong&gt;, so write it as instructions to a careful colleague. And &lt;strong&gt;the SQL is parameterized&lt;/strong&gt;: tool arguments are untrusted input — they may come from a model acting on text someone else wrote — and binding the keyword as a parameter keeps a hostile keyword from becoming hostile SQL. Both tools are read-only, and &lt;code&gt;limit&lt;/code&gt; is clamped.&lt;/p&gt;
&lt;h2&gt;Step 3 — Look at your server in the Inspector&lt;/h2&gt;
&lt;p&gt;Before writing client code, inspect the server the way a host sees it:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;mcp dev server.py
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This starts the MCP Inspector, a local web page that connects as a client. Open the address it prints, choose &lt;strong&gt;List Tools&lt;/strong&gt;, and you should see both tools with their generated schemas. Call &lt;code&gt;search_notes&lt;/code&gt; with the keyword &lt;code&gt;backup&lt;/code&gt;. If it works here, the server half is done — later failures live in the client or host configuration.&lt;/p&gt;
&lt;p&gt;Running &lt;code&gt;python server.py&lt;/code&gt; directly appears to do nothing. That is correct, not a hang: a stdio server waits silently for a client to speak first.&lt;/p&gt;
&lt;h2&gt;Step 4 — Connect a client and call a tool&lt;/h2&gt;
&lt;p&gt;Save this as &lt;code&gt;client.py&lt;/code&gt; in the same folder:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;&quot;&quot;&quot;A small MCP client that connects to server.py and calls its tools.&quot;&quot;&quot;

import asyncio
import sys

from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client


async def main() -&amp;gt; None:
    params = StdioServerParameters(
        command=sys.executable,
        args=[&quot;server.py&quot;],
    )
    async with stdio_client(params) as (read, write):
        async with ClientSession(read, write) as session:
            await session.initialize()

            tools = await session.list_tools()
            print(&quot;Tools:&quot;, [tool.name for tool in tools.tools])

            result = await session.call_tool(
                &quot;search_notes&quot;, arguments={&quot;keyword&quot;: &quot;backup&quot;}
            )
            print(&quot;search_notes -&amp;gt;&quot;, result.content[0].text)

            result = await session.call_tool(
                &quot;recent_notes&quot;, arguments={&quot;limit&quot;: 2}
            )
            print(&quot;recent_notes -&amp;gt;&quot;, result.content[0].text)


if __name__ == &quot;__main__&quot;:
    asyncio.run(main())
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note &lt;code&gt;command=sys.executable&lt;/code&gt;: the client starts the server with the same Python running the client, so the server inherits the venv where the SDK is installed. Pointing a host at a &lt;code&gt;python&lt;/code&gt; that resolves elsewhere is the most common reason a working server &quot;cannot connect.&quot;&lt;/p&gt;
&lt;p&gt;Run it with &lt;code&gt;python client.py&lt;/code&gt;. Actual output, run against SDK 2.3.0:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-text&quot;&gt;Tools: [&#39;search_notes&#39;, &#39;recent_notes&#39;]
search_notes -&amp;gt; {&quot;matches&quot;: [{&quot;id&quot;: 2, &quot;title&quot;: &quot;Database backup&quot;, &quot;body&quot;: &quot;Take a full backup before any schema change and verify the restore.&quot;, &quot;tag&quot;: &quot;database&quot;}]}
recent_notes -&amp;gt; {&quot;notes&quot;: [{&quot;id&quot;: 4, &quot;title&quot;: &quot;FastAPI startup fix&quot;, &quot;tag&quot;: &quot;fastapi&quot;}, {&quot;id&quot;: 3, &quot;title&quot;: &quot;Grafana alert idea&quot;, &quot;tag&quot;: &quot;monitoring&quot;}]}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Read it as a conversation: subprocess started, handshake completed, two tools discovered, two calls answered with parseable JSON. Put a model in the host&#39;s seat and nothing about the server changes. That separation is the entire point of the protocol.&lt;/p&gt;
&lt;p&gt;To use the server from a real host, give the host the same launch information in its configuration (file locations differ per host; use absolute paths):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-json&quot;&gt;{
  &quot;mcpServers&quot;: {
    &quot;project-notes&quot;: {
      &quot;command&quot;: &quot;C:\\path\\to\\mcp-notes\\.venv\\Scripts\\python.exe&quot;,
      &quot;args&quot;: [&quot;C:\\path\\to\\mcp-notes\\server.py&quot;]
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Save, fully restart the host, and the two tools should appear in its tool list.&lt;/p&gt;
&lt;h2&gt;Errors you may hit&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;ModuleNotFoundError: No module named &#39;mcp.server.fastmcp&#39;&lt;/code&gt;, with a note that FastMCP was renamed.&lt;/strong&gt; You — or your tutorial — are running v1 code on SDK v2. Change the import to &lt;code&gt;from mcp.server import MCPServer&lt;/code&gt;, as here, or pin the old line with &lt;code&gt;pip install &quot;mcp&amp;lt;2&quot;&lt;/code&gt;. The generations cannot be mixed.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The server &quot;hangs&quot; and prints nothing.&lt;/strong&gt; It is waiting on stdio for a client; test it with &lt;code&gt;mcp dev&lt;/code&gt; or &lt;code&gt;client.py&lt;/code&gt;. The mirror-image mistake is worse: a debugging &lt;code&gt;print()&lt;/code&gt; inside the server corrupts the JSON-RPC stream on stdout. Log to stderr, or not at all.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;A validation error names the argument you got wrong.&lt;/strong&gt; These are real outputs from this server: a missing argument returns &lt;code&gt;Field required&lt;/code&gt; for &lt;code&gt;keyword&lt;/code&gt;; passing &lt;code&gt;&quot;five&quot;&lt;/code&gt; where an integer belongs returns &lt;code&gt;Input should be a valid integer, unable to parse string as an integer&lt;/code&gt;. Both arrive as tool errors, not crashes, so the caller can retry corrected. An unregistered name returns &lt;code&gt;Unknown tool:&lt;/code&gt; plus the name — print &lt;code&gt;list_tools()&lt;/code&gt; first, as &lt;code&gt;client.py&lt;/code&gt; does.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The host cannot start the server, or finds nothing.&lt;/strong&gt; In order: the Python launched is not the venv&#39;s Python (use its full path); the &lt;code&gt;server.py&lt;/code&gt; path is relative or wrong (use absolute paths, then restart the host — most read this configuration only at startup); or the SDK sits in a different environment. The Step 3 test separates these: if &lt;code&gt;mcp dev&lt;/code&gt; works from your venv, the server is innocent and the launch line is at fault.&lt;/p&gt;
&lt;h2&gt;Where to go next&lt;/h2&gt;
&lt;p&gt;Grow this server as real work pulls it: swap the seed data for your own notes, add a third tool only when a real question needs it, and keep every tool narrow, read-only until a write is required, and honest in its docstring.&lt;/p&gt;
&lt;p&gt;Previous: building the tool-calling loop by hand, in Python. Next: a complete small AI app — a PDF Q&amp;amp;A tool built with FastAPI.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&amp;nbsp;&lt;p&gt;&lt;/p&gt;

&lt;div style=&quot;background: rgb(251, 252, 253); border-radius: 8px; border: 1px solid rgb(227, 230, 234); margin: 28px 0px; padding: 18px 14px; text-align: center;&quot;&gt;&lt;h3 style=&quot;margin: 0px 0px 6px;&quot;&gt;Enjoying these tutorials?&lt;/h3&gt;&lt;p style=&quot;margin: 0px 0px 6px;&quot;&gt;Get new Visual Studio Learn posts by email — free, no spam:&lt;/p&gt;&lt;style&gt;
    .followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview {
  display: flex !important;
  flex-direction: column !important;
  justify-content: center !important;
  margin-top: 30px !important;
  padding: clamp(17px, 5%, 40px) clamp(17px, 7%, 50px) !important;
  max-width: none !important;
  border-radius: 6px !important;
  box-shadow: 0 5px 25px rgba(34, 60, 47, 0.25) !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview,
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview *{
  box-sizing: border-box !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-heading {
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-heading h5{
  margin-top: 0 !important;
  margin-bottom: 0 !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field {
  margin-top: 20px !important;
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input {
  width: 100% !important;
  height: 40px !important;
  border-radius: 6px !important;
  border: 2px solid #e9e8e8 !important;
  background-color: #fff;
  outline: none !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input {
  color: #000000 !important;
  font-family: &quot;Montserrat&quot; !important;
  font-size: 14px;
  font-weight: 400;
  line-height: 20px;
  text-align: center;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input::placeholder {
  color: #000000 !important;
  opacity: 1 !important;
}

.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input:-ms-input-placeholder {
  color: #000000 !important;
}

.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input::-ms-input-placeholder {
  color: #000000 !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button {
  margin-top: 10px !important;
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button button {
  width: 100% !important;
  height: 40px !important;
  border: 0 !important;
  border-radius: 6px !important;
  line-height: 0px !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button button:hover {
  cursor: pointer !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button {
  margin-top: 10px !important;
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .powered-by-line {
  color: #231f20 !important;
  font-family: &quot;Montserrat&quot; !important;
  font-size: 13px;
  font-weight: 400;
  line-height: 25px;
  text-align: center;
  text-decoration: none;
  display: flex;
  width: 100%;
  justify-content: center;
  align-items: center;
  margin-top: 10px;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .powered-by-line img {
  margin-left: 10px !important;
  height: 1.13em !important;
  max-height: 1.13em !important;
}

  &lt;/style&gt;
  &lt;div attr-a=&quot;&quot; attr-b=&quot;&quot; attr-c=&quot;&quot; attr-d=&quot;&quot; attr-e=&quot;&quot; attr-f=&quot;&quot; class=&quot;followit--follow-form-container&quot;&gt;
    &lt;form action=&quot;https://api.follow.it/subscription-form/SmRpWjEzV1ovU0FOK2R2Y0oxajFBUmtIdW44K1ZiUWZRUm1FZVdpVlRKUHFLSFBKWjE4M1hTSk5yRU5oNmg0RU5zelhuTllNTGU1UmwzRkN0NzZWbnd2YkV5VWFwYnptWFdtekI3eXdwMmhtamtUNit3dlNSblVmRjJxd1JGbjZ8Q05yQWR2eitvYW5HRmNMMzJkbStpM09rNmxXZ0ZjWWhyOERya2VvVXRzZz0=/8&quot; data-v-f93f6730=&quot;&quot; method=&quot;post&quot;&gt;&lt;div class=&quot;form-preview&quot; data-v-f93f6730=&quot;&quot; style=&quot;background-color: white; border-color: rgb(204, 204, 204); border-style: solid; border-width: 1px; position: relative;&quot;&gt;&lt;div class=&quot;preview-heading&quot; data-v-f93f6730=&quot;&quot;&gt;&lt;h5 data-v-f93f6730=&quot;&quot; style=&quot;color: black; font-family: Arial; font-size: 16px; font-weight: bold; text-align: center; text-transform: none;&quot;&gt;Get new posts by email:&lt;/h5&gt;&lt;/div&gt;&lt;div class=&quot;preview-input-field&quot; data-v-f93f6730=&quot;&quot;&gt;&lt;input data-v-f93f6730=&quot;&quot; name=&quot;email&quot; required=&quot;&quot; spellcheck=&quot;false&quot; style=&quot;background-color: white; color: black; font-family: Arial; font-size: 14px; font-weight: normal; text-align: center; text-transform: none;&quot; type=&quot;email&quot; /&gt;&lt;/div&gt;&lt;div class=&quot;preview-submit-button&quot; data-v-f93f6730=&quot;&quot;&gt;&lt;button data-v-f93f6730=&quot;&quot; style=&quot;background-color: black; color: black; font-family: Arial; font-size: 16px; font-weight: bold; text-align: center; text-transform: none;&quot; type=&quot;submit&quot;&gt;Subscribe&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/form&gt;&lt;a class=&quot;powered-by-line&quot; href=&quot;https://follow.it&quot;&gt;Powered by &lt;img alt=&quot;follow.it&quot; height=&quot;17px&quot; src=&quot;https://follow.it/images/colored-logo.svg&quot; /&gt;&lt;/a&gt;
  &lt;/div&gt;&lt;p style=&quot;font-size: 14px; margin: 12px 0px 0px;&quot;&gt;Follow along: &lt;a href=&quot;https://www.youtube.com/@VisualStudioLearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/youtube/FF0000&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;YouTube&quot; /&gt; YouTube&lt;/a&gt; · &lt;a href=&quot;https://www.facebook.com/VisualStudio.Learn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/facebook/1877F2&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;Facebook&quot; /&gt; Facebook&lt;/a&gt; · &lt;a href=&quot;https://www.instagram.com/visualstudiolearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/instagram/E4405F&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;Instagram&quot; /&gt; Instagram&lt;/a&gt; · &lt;a href=&quot;https://www.tiktok.com/@visualstudiolearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/tiktok/000000&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;TikTok&quot; /&gt; TikTok&lt;/a&gt; · &lt;a href=&quot;https://www.visualstudiolearn.com/feeds/posts/default?alt=rss&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/rss/FFA500&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;RSS&quot; /&gt; RSS&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.visualstudiolearn.com/feeds/8981327061337745507/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.visualstudiolearn.com/2026/10/mcp-servers-explained-give-your-ai.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/8981327061337745507'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/8981327061337745507'/><link rel='alternate' type='text/html' href='http://www.visualstudiolearn.com/2026/10/mcp-servers-explained-give-your-ai.html' title='MCP Servers Explained: Give Your AI Agent Real Tools'/><author><name>The Loonie Lesson</name><uri>http://www.blogger.com/profile/02243798795439866634</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRY4V9cFLhqMxHWBOWCGBiGUqjan14Vda_dy5rz-C41kApceM-u6jAxS2_6AIRZtGkQeyiQolq1gCeSBhpo573lYq37MLmMGgSKDQLasowic65VoitivYCzqrmUDUqxJuqCTyzah8JuALYtW0Iz7rXIvIcj-ovhVpEuEhQJ6yG1FUQ-A/s1600/loonie-lesson-logo-profile.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://img.youtube.com/vi/SA-J4OARAzk/default.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8861905861585113921.post-442795928269777322</id><published>2026-10-20T09:07:00.000-04:00</published><updated>2026-11-22T02:50:46.822-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="AI Agents"/><category scheme="http://www.blogger.com/atom/ns#" term="Gemini"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><category scheme="http://www.blogger.com/atom/ns#" term="Tool Calling"/><category scheme="http://www.blogger.com/atom/ns#" term="Tutorials"/><title type='text'>Build Your First AI Agent in Python (Tool-Calling, Step by Step)</title><content type='html'>&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&amp;nbsp;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;div class=&quot;BLOG_html_snippet&quot;&gt;&lt;p&gt;&lt;strong&gt;Versions used in this post — read this first:&lt;/strong&gt; Python 3.12 · &lt;code&gt;google-genai&lt;/code&gt; 1.35.0 · model &lt;code&gt;gemini-2.5-flash&lt;/code&gt; · No framework, no LangChain. Everything below runs from one file. If a newer SDK changes a method name, the loop in Step 4 stays the same — only the call syntax moves.&lt;/p&gt;
&lt;iframe allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/r0nY3zq9YfE&quot; width=&quot;560&quot;&gt;&lt;/iframe&gt;

&lt;h2&gt;The direct answer&lt;/h2&gt;
&lt;p&gt;An AI agent is not a special kind of model. It is a normal program that does three things in a loop: it sends your question and a list of tools to a model, it runs whichever tool the model asks for, and it sends the tool result back so the model can answer — or ask for another tool. Execution stays with your program from start to finish — the model only ever asks.&lt;/p&gt;
&lt;p&gt;This post builds that loop by hand, on purpose. Frameworks hide the loop, and the loop is the part you need when something breaks in production.&lt;/p&gt;
&lt;h2&gt;What you will build&lt;/h2&gt;
&lt;p&gt;A small command-line agent with two tools:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Weather lookup&lt;/strong&gt; — gets the current temperature and wind for a city from the free Open-Meteo service, which needs no API key.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Calculator&lt;/strong&gt; — evaluates arithmetic safely, without using Python&#39;s &lt;code&gt;eval()&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;You can ask it a question that needs both, such as: &lt;em&gt;&quot;Is it warmer in Ottawa or Toronto right now, and what is the difference times two?&quot;&lt;/em&gt; The agent should call the weather tool twice, call the calculator once, and then answer in plain language.&lt;/p&gt;
&lt;h2&gt;Get the code&lt;/h2&gt; &lt;p&gt;All the code in this post lives in the companion GitHub repository, in the &lt;a href=&quot;https://github.com/sajjad037/visual-studio-learn-code/tree/main/topic-01-first-ai-agent&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;topic-01-first-ai-agent&lt;/a&gt; folder — with a README that covers prerequisites, environment setup, install, and how to run and use it. If you would rather follow along here, every step is also written out below.&lt;/p&gt; &lt;h2&gt;Step 1 — Install and set your key&lt;/h2&gt;
&lt;p&gt;Create a folder, then install the single dependency:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;pip install google-genai==1.35.0
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Get a Gemini API key from Google AI Studio and set it as an environment variable. Never paste a key into source code that you might commit.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;How to get your free Gemini API key (about 2 minutes, no card required):&lt;/strong&gt;&lt;/p&gt; &lt;ol&gt; &lt;li&gt;Open &lt;a href=&quot;https://aistudio.google.com&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Google AI Studio&lt;/a&gt; and sign in with your Google account. Accept the terms if it is your first visit.&lt;/li&gt; &lt;li&gt;Click &lt;strong&gt;Get API key&lt;/strong&gt; (in the left sidebar, or on the dashboard).&lt;/li&gt; &lt;li&gt;Click &lt;strong&gt;Create API key&lt;/strong&gt;, then choose &lt;strong&gt;Create API key in new project&lt;/strong&gt; — the simplest option for a first key.&lt;/li&gt; &lt;li&gt;Google generates the key and shows it to you. Click &lt;strong&gt;Copy&lt;/strong&gt;. A Gemini key starts with &quot;AIza&quot;.&lt;/li&gt; &lt;li&gt;Treat the key like a password: anyone who has it can use your quota. Never post it publicly, never commit it to GitHub, and if it ever leaks, delete it on the same page and create a fresh one.&lt;/li&gt; &lt;li&gt;Now set it as the &lt;code&gt;GEMINI_API_KEY&lt;/code&gt; environment variable with the commands below — the code reads it from there, so the key never lives in your source.&lt;/li&gt; &lt;/ol&gt; &lt;p&gt;The free tier is enough for everything in this post. You only need billing if you outgrow the free rate limits — and the 429 error in the errors section below is how you will know.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;# macOS / Linux
export GEMINI_API_KEY=&quot;your-key-here&quot;

# Windows PowerShell
$env:GEMINI_API_KEY=&quot;your-key-here&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Step 2 — Write the two tools&lt;/h2&gt;
&lt;p&gt;A tool is just a Python function with a clear name, typed arguments, and a short docstring. The docstring matters more than usual here: the model reads it to decide when the tool applies, so write it for the model, not only for humans.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;import ast
import json
import operator
import urllib.parse
import urllib.request


def get_weather(city: str) -&amp;gt; dict:
    &quot;&quot;&quot;Get the current weather for a city.

    Args:
        city: City name, for example &quot;Ottawa&quot; or &quot;Toronto&quot;.
    Returns:
        Temperature in Celsius, wind speed, and a condition code.
    &quot;&quot;&quot;
    query = urllib.parse.quote(city)
    geo_url = f&quot;https://geocoding-api.open-meteo.com/v1/search?name={query}&amp;amp;count=1&quot;
    with urllib.request.urlopen(geo_url, timeout=10) as response:
        places = json.load(response).get(&quot;results&quot;)
    if not places:
        return {&quot;error&quot;: f&quot;No location found for {city}&quot;}

    place = places[0]
    weather_url = (
        &quot;https://api.open-meteo.com/v1/forecast&quot;
        f&quot;?latitude={place[&#39;latitude&#39;]}&amp;amp;longitude={place[&#39;longitude&#39;]}&quot;
        &quot;&amp;amp;current=temperature_2m,wind_speed_10m,weather_code&quot;
)
    with urllib.request.urlopen(weather_url, timeout=10) as response:
        current = json.load(response)[&quot;current&quot;]
    return {
        &quot;city&quot;: place[&quot;name&quot;],
        &quot;temperature_c&quot;: current[&quot;temperature_2m&quot;],
        &quot;wind_kmh&quot;: current[&quot;wind_speed_10m&quot;],
        &quot;weather_code&quot;: current[&quot;weather_code&quot;],
}


_OPERATORS = {
    ast.Add: operator.add,
    ast.Sub: operator.sub,
    ast.Mult: operator.mul,
    ast.Div: operator.truediv,
    ast.USub: operator.neg,
}


def _eval_node(node):
    if isinstance(node, ast.Constant) and isinstance(node.value, (int, float)):
        return node.value
    if isinstance(node, ast.BinOp) and type(node.op) in _OPERATORS:
        return _OPERATORS[type(node.op)](_eval_node(node.left), _eval_node(node.right))
    if isinstance(node, ast.UnaryOp) and type(node.op) in _OPERATORS:
        return _OPERATORS[type(node.op)](_eval_node(node.operand))
    raise ValueError(&quot;Expression contains something the calculator does not allow&quot;)


def calculate(expression: str) -&amp;gt; dict:
    &quot;&quot;&quot;Calculate a basic arithmetic expression.

    Args:
        expression: Arithmetic only, for example &quot;18.5 - 16.0&quot; or &quot;(4 + 6) * 2&quot;.
    Returns:
        The numeric result, or an error message if the expression is not allowed.
    &quot;&quot;&quot;
    try:
        tree = ast.parse(expression, mode=&quot;eval&quot;)
        return {&quot;expression&quot;: expression, &quot;result&quot;: _eval_node(tree.body)}
    except Exception as exc:
        return {&quot;error&quot;: f&quot;Could not calculate &#39;{expression}&#39;: {exc}&quot;}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notice the calculator&#39;s design: it parses the expression into a tree and allows only numbers and five operators. Passing a model&#39;s output into &lt;code&gt;eval()&lt;/code&gt; would let a bad prompt become arbitrary code execution. Treat every tool argument as untrusted input, because it is.&lt;/p&gt;
&lt;h2&gt;Step 3 — Write the agent loop&lt;/h2&gt;
&lt;p&gt;This is the whole agent. Automatic function calling is switched off deliberately, so you can watch each round trip instead of having the SDK hide it.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-python&quot;&gt;import os

from google import genai
from google.genai import types

TOOLS = {&quot;get_weather&quot;: get_weather, &quot;calculate&quot;: calculate}
MAX_STEPS = 6


def run_agent(question: str) -&amp;gt; str:
    client = genai.Client(api_key=os.environ[&quot;GEMINI_API_KEY&quot;])
    config = types.GenerateContentConfig(
        tools=list(TOOLS.values()),
        automatic_function_calling=types.AutomaticFunctionCallingConfig(disable=True),
        system_instruction=(
            &quot;Use the tools when you need live weather or arithmetic. &quot;
            &quot;Do not guess temperatures or do maths in your head.&quot;
),
)
    contents = [question]

    for step in range(1, MAX_STEPS + 1):
        response = client.models.generate_content(
            model=&quot;gemini-2.5-flash&quot;,
            contents=contents,
            config=config,
)
        if not response.function_calls:
            return response.text

        contents.append(response.candidates[0].content)
        for call in response.function_calls:
            tool = TOOLS.get(call.name)
            if tool is None:
                result = {&quot;error&quot;: f&quot;Unknown tool: {call.name}&quot;}
            else:
                result = tool(**call.args)
            print(f&quot; step {step}: {call.name}({call.args}) -&amp;gt; {result}&quot;)
            contents.append(
                types.Part.from_function_response(name=call.name, response=result)
)

    return &quot;Stopped: the agent used all of its steps without finishing.&quot;


if __name__ == &quot;__main__&quot;:
    answer = run_agent(
        &quot;Is it warmer in Ottawa or Toronto right now, &quot;
        &quot;and what is the temperature difference times two?&quot;
)
    print(&quot;\nAnswer:&quot;, answer)
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Step 4 — How the loop works, round by round&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;You send the question plus the tool list.&lt;/strong&gt; The model sees the function names, arguments, and docstrings — not the function bodies.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The model replies with a request, not an answer.&lt;/strong&gt; For our question, the first reply asks for &lt;code&gt;get_weather&lt;/code&gt; for Ottawa and Toronto. Nothing has run yet.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Your code validates and runs the tools.&lt;/strong&gt; This is your security boundary. Check the tool name, check the arguments, and only then execute.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;You send the results back.&lt;/strong&gt; The results become new entries in &lt;code&gt;contents&lt;/code&gt;, so the model can reason over real numbers instead of inventing them.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The loop repeats or ends.&lt;/strong&gt; The model may ask for the calculator next. When a reply contains no function call, that reply is the final answer and the loop stops.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;A step cap protects you.&lt;/strong&gt; &lt;code&gt;MAX_STEPS = 6&lt;/code&gt; guarantees the program ends. An agent without a cap can loop — and bill you — for a very long time.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;How to run it and what you should see&lt;/h2&gt;
&lt;p&gt;Save both parts in one file named &lt;code&gt;first_agent.py&lt;/code&gt;, then run:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-bash&quot;&gt;python first_agent.py
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The exact temperatures will differ every run, but the shape should look like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-text&quot;&gt; step 1: get_weather({&#39;city&#39;: &#39;Ottawa&#39;}) -&amp;gt; {&#39;city&#39;: &#39;Ottawa&#39;, &#39;temperature_c&#39;: 9.4,...}
  step 1: get_weather({&#39;city&#39;: &#39;Toronto&#39;}) -&amp;gt; {&#39;city&#39;: &#39;Toronto&#39;, &#39;temperature_c&#39;: 12.1,...}
  step 2: calculate({&#39;expression&#39;: &#39;(12.1 - 9.4) * 2&#39;}) -&amp;gt; {&#39;result&#39;: 5.4}

Answer: Toronto is warmer right now, at 12.1°C compared with 9.4°C in Ottawa — a difference of 2.7 degrees, which doubled is 5.4.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If the printed tool calls are missing, the model answered from memory instead of using the tools. Strengthen the system instruction rather than adding more code — wording is a real control surface here.&lt;/p&gt;
&lt;h2&gt;Errors you may hit&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;KeyError: &#39;GEMINI_API_KEY&#39;&lt;/code&gt; or a 400 API key error.&lt;/strong&gt; The variable is not set in the same terminal session that runs the script, or the key was copied with a trailing space. Check only whether the variable exists — never print the key — and restart the terminal after setting it system-wide.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The model asks for a tool that does not exist.&lt;/strong&gt; Models occasionally invent a plausible function name. That is why the loop looks the name up in &lt;code&gt;TOOLS&lt;/code&gt; and returns an error result instead of crashing. The model usually corrects itself on the next step.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Malformed or missing arguments.&lt;/strong&gt; A call may arrive with a city missing or an expression that is not arithmetic. Both tools above return an &lt;code&gt;{&quot;error&quot;:...}&lt;/code&gt; dictionary rather than raising, so the agent gets a chance to recover and the conversation does not die on one bad call.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;429 rate-limit or quota errors.&lt;/strong&gt; Free-tier keys have per-minute and per-day limits, and a loop multiplies requests: one question here costs three or four model calls. Add a short pause and retry on 429, lower &lt;code&gt;MAX_STEPS&lt;/code&gt;, and expect this error during busy testing rather than treating it as a code bug.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;503 or a temporary model error.&lt;/strong&gt; Transient failures happen on hosted models. Retry a few times with increasing waits before changing your code — and log which step failed, so you know whether the failure was the model call or your own tool.&lt;/p&gt;
&lt;h2&gt;What does one run actually cost?&lt;/h2&gt;
&lt;p&gt;Honest answer: very little — but not zero, and the loop is why. A two-city question typically uses roughly 1,000–1,500 input tokens and 150–250 output tokens across all its model calls. At the published &lt;code&gt;gemini-2.5-flash&lt;/code&gt; paid rates of US$0.30 per million input tokens and US$2.50 per million output tokens, that is well under a tenth of a cent per question. On the free tier, the same run counts against your daily quota instead.&lt;/p&gt;
&lt;p&gt;Do not trust my estimate for your own app. Every response carries usage data, so build the habit on day one of logging &lt;code&gt;response.usage_metadata&lt;/code&gt; for each step. Cost surprises almost never come from one expensive call. They come from a loop that quietly ran forty cheap ones.&lt;/p&gt;
&lt;h2&gt;Where to go next&lt;/h2&gt;
&lt;p&gt;Add one tool that touches your own world — reading a folder of notes, querying a small database, or checking a calendar — and keep the same discipline: typed arguments, errors returned as data, a hard step cap, and a log line for every call. That pattern, not any framework, carries from this script to larger agent systems, including the Gemini integration behind my own AI Portfolio Manager app.&lt;/p&gt;
&lt;p&gt;Next in this series: MCP servers — the standard way to package tools like these so more than one agent can use them.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;

&lt;div style=&quot;background: rgb(251, 252, 253); border-radius: 8px; border: 1px solid rgb(227, 230, 234); margin: 28px 0px; padding: 18px 14px; text-align: center;&quot;&gt;&lt;h3 style=&quot;margin: 0px 0px 6px;&quot;&gt;Enjoying these tutorials?&lt;/h3&gt;&lt;p style=&quot;margin: 0px 0px 6px;&quot;&gt;Get new Visual Studio Learn posts by email — free, no spam:&lt;/p&gt;&lt;style&gt;
    .followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview {
  display: flex !important;
  flex-direction: column !important;
  justify-content: center !important;
  margin-top: 30px !important;
  padding: clamp(17px, 5%, 40px) clamp(17px, 7%, 50px) !important;
  max-width: none !important;
  border-radius: 6px !important;
  box-shadow: 0 5px 25px rgba(34, 60, 47, 0.25) !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview,
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview *{
  box-sizing: border-box !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-heading {
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-heading h5{
  margin-top: 0 !important;
  margin-bottom: 0 !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field {
  margin-top: 20px !important;
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input {
  width: 100% !important;
  height: 40px !important;
  border-radius: 6px !important;
  border: 2px solid #e9e8e8 !important;
  background-color: #fff;
  outline: none !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input {
  color: #000000 !important;
  font-family: &quot;Montserrat&quot; !important;
  font-size: 14px;
  font-weight: 400;
  line-height: 20px;
  text-align: center;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input::placeholder {
  color: #000000 !important;
  opacity: 1 !important;
}

.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input:-ms-input-placeholder {
  color: #000000 !important;
}

.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-input-field input::-ms-input-placeholder {
  color: #000000 !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button {
  margin-top: 10px !important;
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button button {
  width: 100% !important;
  height: 40px !important;
  border: 0 !important;
  border-radius: 6px !important;
  line-height: 0px !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button button:hover {
  cursor: pointer !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .preview-submit-button {
  margin-top: 10px !important;
  width: 100% !important;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .powered-by-line {
  color: #231f20 !important;
  font-family: &quot;Montserrat&quot; !important;
  font-size: 13px;
  font-weight: 400;
  line-height: 25px;
  text-align: center;
  text-decoration: none;
  display: flex;
  width: 100%;
  justify-content: center;
  align-items: center;
  margin-top: 10px;
}
.followit--follow-form-container[attr-a][attr-b][attr-c][attr-d][attr-e][attr-f] .form-preview .powered-by-line img {
  margin-left: 10px !important;
  height: 1.13em !important;
  max-height: 1.13em !important;
}

  &lt;/style&gt;
  &lt;div attr-a=&quot;&quot; attr-b=&quot;&quot; attr-c=&quot;&quot; attr-d=&quot;&quot; attr-e=&quot;&quot; attr-f=&quot;&quot; class=&quot;followit--follow-form-container&quot;&gt;
    &lt;form action=&quot;https://api.follow.it/subscription-form/SmRpWjEzV1ovU0FOK2R2Y0oxajFBUmtIdW44K1ZiUWZRUm1FZVdpVlRKUHFLSFBKWjE4M1hTSk5yRU5oNmg0RU5zelhuTllNTGU1UmwzRkN0NzZWbnd2YkV5VWFwYnptWFdtekI3eXdwMmhtamtUNit3dlNSblVmRjJxd1JGbjZ8Q05yQWR2eitvYW5HRmNMMzJkbStpM09rNmxXZ0ZjWWhyOERya2VvVXRzZz0=/8&quot; data-v-f93f6730=&quot;&quot; method=&quot;post&quot;&gt;&lt;div class=&quot;form-preview&quot; data-v-f93f6730=&quot;&quot; style=&quot;background-color: white; border-color: rgb(204, 204, 204); border-style: solid; border-width: 1px; position: relative;&quot;&gt;&lt;div class=&quot;preview-heading&quot; data-v-f93f6730=&quot;&quot;&gt;&lt;h5 data-v-f93f6730=&quot;&quot; style=&quot;color: black; font-family: Arial; font-size: 16px; font-weight: bold; text-align: center; text-transform: none;&quot;&gt;Get new posts by email:&lt;/h5&gt;&lt;/div&gt;&lt;div class=&quot;preview-input-field&quot; data-v-f93f6730=&quot;&quot;&gt;&lt;input data-v-f93f6730=&quot;&quot; name=&quot;email&quot; required=&quot;&quot; spellcheck=&quot;false&quot; style=&quot;background-color: white; color: black; font-family: Arial; font-size: 14px; font-weight: normal; text-align: center; text-transform: none;&quot; type=&quot;email&quot; /&gt;&lt;/div&gt;&lt;div class=&quot;preview-submit-button&quot; data-v-f93f6730=&quot;&quot;&gt;&lt;button data-v-f93f6730=&quot;&quot; style=&quot;background-color: black; color: black; font-family: Arial; font-size: 16px; font-weight: bold; text-align: center; text-transform: none;&quot; type=&quot;submit&quot;&gt;Subscribe&lt;/button&gt;&lt;/div&gt;&lt;/div&gt;&lt;/form&gt;&lt;a class=&quot;powered-by-line&quot; href=&quot;https://follow.it&quot;&gt;Powered by &lt;img alt=&quot;follow.it&quot; height=&quot;17px&quot; src=&quot;https://follow.it/images/colored-logo.svg&quot; /&gt;&lt;/a&gt;
  &lt;/div&gt;&lt;p style=&quot;font-size: 14px; margin: 12px 0px 0px;&quot;&gt;Follow along: &lt;a href=&quot;https://www.youtube.com/@VisualStudioLearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/youtube/FF0000&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;YouTube&quot; /&gt; YouTube&lt;/a&gt; · &lt;a href=&quot;https://www.facebook.com/VisualStudio.Learn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/facebook/1877F2&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;Facebook&quot; /&gt; Facebook&lt;/a&gt; · &lt;a href=&quot;https://www.instagram.com/visualstudiolearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/instagram/E4405F&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;Instagram&quot; /&gt; Instagram&lt;/a&gt; · &lt;a href=&quot;https://www.tiktok.com/@visualstudiolearn&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/tiktok/000000&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;TikTok&quot; /&gt; TikTok&lt;/a&gt; · &lt;a href=&quot;https://www.visualstudiolearn.com/feeds/posts/default?alt=rss&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;&lt;img src=&quot;https://cdn.simpleicons.org/rss/FFA500&quot; width=&quot;22&quot; height=&quot;22&quot; alt=&quot;RSS&quot; /&gt; RSS&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.visualstudiolearn.com/feeds/442795928269777322/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.visualstudiolearn.com/2026/10/build-your-first-ai-agent-in-python.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/442795928269777322'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/442795928269777322'/><link rel='alternate' type='text/html' href='http://www.visualstudiolearn.com/2026/10/build-your-first-ai-agent-in-python.html' title='Build Your First AI Agent in Python (Tool-Calling, Step by Step)'/><author><name>The Loonie Lesson</name><uri>http://www.blogger.com/profile/02243798795439866634</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRY4V9cFLhqMxHWBOWCGBiGUqjan14Vda_dy5rz-C41kApceM-u6jAxS2_6AIRZtGkQeyiQolq1gCeSBhpo573lYq37MLmMGgSKDQLasowic65VoitivYCzqrmUDUqxJuqCTyzah8JuALYtW0Iz7rXIvIcj-ovhVpEuEhQJ6yG1FUQ-A/s1600/loonie-lesson-logo-profile.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://img.youtube.com/vi/r0nY3zq9YfE/default.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8861905861585113921.post-2104785960283910823</id><published>2016-07-20T23:30:00.000-04:00</published><updated>2026-11-22T02:50:45.524-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Videos"/><category scheme="http://www.blogger.com/atom/ns#" term="visual Studio"/><category scheme="http://www.blogger.com/atom/ns#" term="Windows Forms"/><title type='text'>Create Installer Projects in Visual Studio 2013</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj5U4TLPB7hUyatRIBROspiyBN2JyYFDtk_IsKJU_1X0r4MEloa8ot_F-n2ewgZbr4R8wVllG_40Q0p5naWvHPSztF1a2OSjpLZUj7dMCnUk52NYPgZTHUajW6HHRzjGvXKXV_3zsz8jWk/s1600/msi-exe-i1%255B1%255D.png&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;186&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj5U4TLPB7hUyatRIBROspiyBN2JyYFDtk_IsKJU_1X0r4MEloa8ot_F-n2ewgZbr4R8wVllG_40Q0p5naWvHPSztF1a2OSjpLZUj7dMCnUk52NYPgZTHUajW6HHRzjGvXKXV_3zsz8jWk/s320/msi-exe-i1%255B1%255D.png&quot; title=&quot;Create Project Installer in visual studio 2013&quot; width=&quot;320&quot;&gt;&lt;/a&gt;&lt;/div&gt;
After completing the development of your desktop Application. The next phase is to deployment or installation of that application.&lt;br&gt;
&lt;br&gt;
For this you need to create a setup of your project, then you just need to give the setup file to the users or your client so they can install and use your application.&lt;br&gt;
&lt;br&gt;
In Visual Studio 10 or prior edition, has built in functionality to create setup file using Installer Projects. But in Visual Studio 13 and next edition you need to install an extension first and then you can create. &lt;br&gt;
&lt;/div&gt;&lt;a href=&quot;http://www.visualstudiolearn.com/2016/07/create-installer-projects-in-visual.html#more&quot;&gt;READ MORE&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.visualstudiolearn.com/feeds/2104785960283910823/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.visualstudiolearn.com/2016/07/create-installer-projects-in-visual.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/2104785960283910823'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/2104785960283910823'/><link rel='alternate' type='text/html' href='http://www.visualstudiolearn.com/2016/07/create-installer-projects-in-visual.html' title='Create Installer Projects in Visual Studio 2013'/><author><name>The Loonie Lesson</name><uri>http://www.blogger.com/profile/02243798795439866634</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRY4V9cFLhqMxHWBOWCGBiGUqjan14Vda_dy5rz-C41kApceM-u6jAxS2_6AIRZtGkQeyiQolq1gCeSBhpo573lYq37MLmMGgSKDQLasowic65VoitivYCzqrmUDUqxJuqCTyzah8JuALYtW0Iz7rXIvIcj-ovhVpEuEhQJ6yG1FUQ-A/s1600/loonie-lesson-logo-profile.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj5U4TLPB7hUyatRIBROspiyBN2JyYFDtk_IsKJU_1X0r4MEloa8ot_F-n2ewgZbr4R8wVllG_40Q0p5naWvHPSztF1a2OSjpLZUj7dMCnUk52NYPgZTHUajW6HHRzjGvXKXV_3zsz8jWk/s72-c/msi-exe-i1%255B1%255D.png" height="72" width="72"/><thr:total>0</thr:total><georss:featurename>Montreal, QC, Canada</georss:featurename><georss:point>45.5016889 -73.567255999999986</georss:point><georss:box>45.1459249 -74.212702999999991 45.8574529 -72.921808999999982</georss:box></entry><entry><id>tag:blogger.com,1999:blog-8861905861585113921.post-8563921201470357008</id><published>2016-07-18T23:30:00.000-04:00</published><updated>2026-11-22T02:50:44.313-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android"/><category scheme="http://www.blogger.com/atom/ns#" term="Videos"/><title type='text'>Android Development Step by step</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNRO9wFLS-2WT5hMvbVMXF4CQQbRBd59aUbWo6K2eU5-HM-gbkLAOEGnQFve3CDzrNECATNESOAGZio7LMMOvzkpO9Bl2jadLMnFIwn2n9ktip1s3Bcp-2z7sWaDz9jkqFxLmCzfPFhC0/s1600/learn-android-development-840x420%255B1%255D.jpg&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;160&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNRO9wFLS-2WT5hMvbVMXF4CQQbRBd59aUbWo6K2eU5-HM-gbkLAOEGnQFve3CDzrNECATNESOAGZio7LMMOvzkpO9Bl2jadLMnFIwn2n9ktip1s3Bcp-2z7sWaDz9jkqFxLmCzfPFhC0/s320/learn-android-development-840x420%255B1%255D.jpg&quot; width=&quot;320&quot;&gt;&lt;/a&gt;&lt;/div&gt;
&lt;a href=&quot;https://en.wikipedia.org/wiki/Android_(operating_system)&quot;&gt;Android &lt;/a&gt;is a mobile operating system developed by Google. It is primarily designed for Touchscreen mobile devices such as smartphone and tablets.&lt;br&gt;
As we know, a large number  of  audience is using Android smartphone. So you have a huge market to publish your android apps on play store.&lt;br&gt;
&lt;br&gt;
&lt;/div&gt;&lt;a href=&quot;http://www.visualstudiolearn.com/2016/07/android-development-step-by-step.html#more&quot;&gt;READ MORE&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.visualstudiolearn.com/feeds/8563921201470357008/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.visualstudiolearn.com/2016/07/android-development-step-by-step.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/8563921201470357008'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/8563921201470357008'/><link rel='alternate' type='text/html' href='http://www.visualstudiolearn.com/2016/07/android-development-step-by-step.html' title='Android Development Step by step'/><author><name>The Loonie Lesson</name><uri>http://www.blogger.com/profile/02243798795439866634</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRY4V9cFLhqMxHWBOWCGBiGUqjan14Vda_dy5rz-C41kApceM-u6jAxS2_6AIRZtGkQeyiQolq1gCeSBhpo573lYq37MLmMGgSKDQLasowic65VoitivYCzqrmUDUqxJuqCTyzah8JuALYtW0Iz7rXIvIcj-ovhVpEuEhQJ6yG1FUQ-A/s1600/loonie-lesson-logo-profile.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNRO9wFLS-2WT5hMvbVMXF4CQQbRBd59aUbWo6K2eU5-HM-gbkLAOEGnQFve3CDzrNECATNESOAGZio7LMMOvzkpO9Bl2jadLMnFIwn2n9ktip1s3Bcp-2z7sWaDz9jkqFxLmCzfPFhC0/s72-c/learn-android-development-840x420%255B1%255D.jpg" height="72" width="72"/><thr:total>1</thr:total><georss:featurename>Montreal, QC, Canada</georss:featurename><georss:point>45.5016889 -73.567255999999986</georss:point><georss:box>45.1459249 -74.212702999999991 45.8574529 -72.921808999999982</georss:box></entry><entry><id>tag:blogger.com,1999:blog-8861905861585113921.post-7064269883475300816</id><published>2016-07-16T23:30:00.000-04:00</published><updated>2026-11-22T02:50:43.027-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="JavaScript"/><category scheme="http://www.blogger.com/atom/ns#" term="Tools"/><title type='text'>Online Base64 Encode and Decode</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhlhGR-BxBAzoO62a8_kklKtL3uVPB9pCZfRu-5c39MZcBz_A_hpv5aEtnSWaXHcqa-qtVLANGoynw-S5y-WmD5mqoviHOKXNC6r2FWbK0E23RFUNj0P8hPulewXFARB6X5RxA2DwM89ZA/s1600/Online+base64+Encoder+and+Decoder.png&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;255&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhlhGR-BxBAzoO62a8_kklKtL3uVPB9pCZfRu-5c39MZcBz_A_hpv5aEtnSWaXHcqa-qtVLANGoynw-S5y-WmD5mqoviHOKXNC6r2FWbK0E23RFUNj0P8hPulewXFARB6X5RxA2DwM89ZA/s320/Online+base64+Encoder+and+Decoder.png&quot; width=&quot;320&quot;&gt;&lt;/a&gt;&lt;/div&gt;
In my previous post, I have shared the basic introduction of &lt;b&gt;base64 encode and decode&lt;/b&gt; and it uses.&lt;br&gt;
&lt;br&gt;
I have also shared the code &lt;a href=&quot;http://visualstudiolearn.blogspot.ca/2016/07/base64-encode-and-decode.html&quot;&gt;&lt;b&gt;How you can base64 encode and decode a string in C#, Java, and Object-c&lt;/b&gt;&lt;/a&gt;.&lt;br&gt;
&lt;br&gt;
I have built a simple java script based tool,  which online converts the string to Base64 encode and base64 decode. At the end of this post will get the source code of this tool and the code snippet  to share this tool on your own site.&lt;br&gt;
&lt;br&gt;
&lt;/div&gt;&lt;a href=&quot;http://www.visualstudiolearn.com/2016/07/oline-base64-encode-and-decode.html#more&quot;&gt;READ MORE&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.visualstudiolearn.com/feeds/7064269883475300816/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.visualstudiolearn.com/2016/07/oline-base64-encode-and-decode.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/7064269883475300816'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/7064269883475300816'/><link rel='alternate' type='text/html' href='http://www.visualstudiolearn.com/2016/07/oline-base64-encode-and-decode.html' title='Online Base64 Encode and Decode'/><author><name>The Loonie Lesson</name><uri>http://www.blogger.com/profile/02243798795439866634</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRY4V9cFLhqMxHWBOWCGBiGUqjan14Vda_dy5rz-C41kApceM-u6jAxS2_6AIRZtGkQeyiQolq1gCeSBhpo573lYq37MLmMGgSKDQLasowic65VoitivYCzqrmUDUqxJuqCTyzah8JuALYtW0Iz7rXIvIcj-ovhVpEuEhQJ6yG1FUQ-A/s1600/loonie-lesson-logo-profile.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhlhGR-BxBAzoO62a8_kklKtL3uVPB9pCZfRu-5c39MZcBz_A_hpv5aEtnSWaXHcqa-qtVLANGoynw-S5y-WmD5mqoviHOKXNC6r2FWbK0E23RFUNj0P8hPulewXFARB6X5RxA2DwM89ZA/s72-c/Online+base64+Encoder+and+Decoder.png" height="72" width="72"/><thr:total>0</thr:total><georss:featurename>Montreal, QC, Canada</georss:featurename><georss:point>45.5016889 -73.567255999999986</georss:point><georss:box>45.1459249 -74.212702999999991 45.8574529 -72.921808999999982</georss:box></entry><entry><id>tag:blogger.com,1999:blog-8861905861585113921.post-209060201097519748</id><published>2016-07-14T23:30:00.000-04:00</published><updated>2026-11-22T02:50:41.818-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="JAVA"/><category scheme="http://www.blogger.com/atom/ns#" term="Objective-C"/><title type='text'>Base64 Encode and Decode</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEggBqdKY1vmQ1NC5zaMsKr7GmDojhi8Km-gPL2zGTYvxUXfhED-jNT_FZ3D5QhmPS1SnwY4Pn61Mr4E-Do45i8zhJ7l717ru0jdGpcnRb89KbMAqjsKR7jDm6QaPOWvA3DSnZZ-Z5EdV1I/s1600/base64%255B1%255D.png&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;180&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEggBqdKY1vmQ1NC5zaMsKr7GmDojhi8Km-gPL2zGTYvxUXfhED-jNT_FZ3D5QhmPS1SnwY4Pn61Mr4E-Do45i8zhJ7l717ru0jdGpcnRb89KbMAqjsKR7jDm6QaPOWvA3DSnZZ-Z5EdV1I/s320/base64%255B1%255D.png&quot; width=&quot;300&quot;&gt;&lt;/a&gt;&lt;/div&gt;
Base64 is used for converting arbitrary binary data to ASCII text so, that it&amp;#39;s become easier to transmitted the data over the network like e-mail and HTML form data.&lt;br&gt;
&lt;br&gt;
In simple words, it turns binary data into plain ASCII text, so the data can travel safely through systems that only handle text, such as email or HTML forms. (Source: &lt;a href=&quot;https://en.wikipedia.org/wiki/Base64&quot;&gt;Wikipedia&lt;/a&gt;).&lt;br&gt;
&lt;br&gt;
&lt;/div&gt;&lt;/div&gt;&lt;a href=&quot;http://www.visualstudiolearn.com/2016/07/base64-encode-and-decode.html#more&quot;&gt;READ MORE&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.visualstudiolearn.com/feeds/209060201097519748/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.visualstudiolearn.com/2016/07/base64-encode-and-decode.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/209060201097519748'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/209060201097519748'/><link rel='alternate' type='text/html' href='http://www.visualstudiolearn.com/2016/07/base64-encode-and-decode.html' title='Base64 Encode and Decode'/><author><name>The Loonie Lesson</name><uri>http://www.blogger.com/profile/02243798795439866634</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRY4V9cFLhqMxHWBOWCGBiGUqjan14Vda_dy5rz-C41kApceM-u6jAxS2_6AIRZtGkQeyiQolq1gCeSBhpo573lYq37MLmMGgSKDQLasowic65VoitivYCzqrmUDUqxJuqCTyzah8JuALYtW0Iz7rXIvIcj-ovhVpEuEhQJ6yG1FUQ-A/s1600/loonie-lesson-logo-profile.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEggBqdKY1vmQ1NC5zaMsKr7GmDojhi8Km-gPL2zGTYvxUXfhED-jNT_FZ3D5QhmPS1SnwY4Pn61Mr4E-Do45i8zhJ7l717ru0jdGpcnRb89KbMAqjsKR7jDm6QaPOWvA3DSnZZ-Z5EdV1I/s72-c/base64%255B1%255D.png" height="72" width="72"/><thr:total>0</thr:total><georss:featurename>Montreal, QC, Canada</georss:featurename><georss:point>45.5016889 -73.567255999999986</georss:point><georss:box>45.1459249 -74.212702999999991 45.8574529 -72.921808999999982</georss:box></entry><entry><id>tag:blogger.com,1999:blog-8861905861585113921.post-7266969290493692784</id><published>2016-07-13T00:00:00.000-04:00</published><updated>2026-11-22T02:50:40.606-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term=".net"/><category scheme="http://www.blogger.com/atom/ns#" term="C#"/><category scheme="http://www.blogger.com/atom/ns#" term="visual Studio"/><category scheme="http://www.blogger.com/atom/ns#" term="Web service"/><title type='text'>Auto Generate Classes from JSON or XML In Visual studio .Net</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjzYFFwAiNDeWqJRZPHEwTMqLK3cx3yHQWHLNVMitjcGhuFQb7T7QglYgFK4v7olOP21LxFujVLBnOTJSY1EnPQLl7HSCuet4-Wp-p9YO2FAMECA-4t2Mfw3L6srPdwVqwLnkvOEGvMZLg/s1600/generate_json_to_C%2523_Classes_300_260.png&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjzYFFwAiNDeWqJRZPHEwTMqLK3cx3yHQWHLNVMitjcGhuFQb7T7QglYgFK4v7olOP21LxFujVLBnOTJSY1EnPQLl7HSCuet4-Wp-p9YO2FAMECA-4t2Mfw3L6srPdwVqwLnkvOEGvMZLg/s1600/generate_json_to_C%2523_Classes_300_260.png&quot;&gt;&lt;/a&gt;&lt;/div&gt;
It is very hard to convert the &lt;b&gt;JSON or XML&lt;/b&gt; output of &lt;a href=&quot;http://visualstudiolearn.blogspot.ca/search/label/Web%20service&quot;&gt;Web service&lt;/a&gt; or &lt;b&gt;Web API&lt;/b&gt; to Model or Object Classes manually.&lt;br&gt;
&lt;br&gt;
The best way to handle service response data is, to &lt;b&gt;Deserialize &lt;/b&gt;the &lt;a href=&quot;http://visualstudiolearn.blogspot.ca/2013/05/access-aspnet-j-son-web-service-on.html&quot;&gt;JSON&lt;/a&gt; or XML response into class Object.&lt;br&gt;
&lt;br&gt;
For this, you need to &lt;b&gt;create mapping&lt;/b&gt; classes for that JSON response. Today I will show you how you can easily and quickly create these classes which are auto-generated by a &lt;a href=&quot;https://www.visualstudiolearn.com/search/label/visual%20Studio&quot;&gt;Visual studio&lt;/a&gt;.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;/div&gt;&lt;a href=&quot;http://www.visualstudiolearn.com/2016/07/auto-generate-classes-from-json-or-xml.html#more&quot;&gt;READ MORE&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.visualstudiolearn.com/feeds/7266969290493692784/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.visualstudiolearn.com/2016/07/auto-generate-classes-from-json-or-xml.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/7266969290493692784'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/7266969290493692784'/><link rel='alternate' type='text/html' href='http://www.visualstudiolearn.com/2016/07/auto-generate-classes-from-json-or-xml.html' title='Auto Generate Classes from JSON or XML In Visual studio .Net'/><author><name>The Loonie Lesson</name><uri>http://www.blogger.com/profile/02243798795439866634</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRY4V9cFLhqMxHWBOWCGBiGUqjan14Vda_dy5rz-C41kApceM-u6jAxS2_6AIRZtGkQeyiQolq1gCeSBhpo573lYq37MLmMGgSKDQLasowic65VoitivYCzqrmUDUqxJuqCTyzah8JuALYtW0Iz7rXIvIcj-ovhVpEuEhQJ6yG1FUQ-A/s1600/loonie-lesson-logo-profile.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjzYFFwAiNDeWqJRZPHEwTMqLK3cx3yHQWHLNVMitjcGhuFQb7T7QglYgFK4v7olOP21LxFujVLBnOTJSY1EnPQLl7HSCuet4-Wp-p9YO2FAMECA-4t2Mfw3L6srPdwVqwLnkvOEGvMZLg/s72-c/generate_json_to_C%2523_Classes_300_260.png" height="72" width="72"/><thr:total>0</thr:total><georss:featurename>Montreal, QC, Canada</georss:featurename><georss:point>45.5016889 -73.567255999999986</georss:point><georss:box>45.1459249 -74.212702999999991 45.8574529 -72.921808999999982</georss:box></entry><entry><id>tag:blogger.com,1999:blog-8861905861585113921.post-2002445483530498632</id><published>2014-06-30T15:00:00.000-04:00</published><updated>2026-11-22T02:50:39.303-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="JavaScript"/><category scheme="http://www.blogger.com/atom/ns#" term="jQuery"/><title type='text'>jQuery Form Validation Sample Example</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhm65FdfvuFlXSzolGgS58PI3OGTOJLhzmGxl8vFvGAe2tiGofchWgO_CE1JMMh5AV5OgCRtQMBdyVTdP00caO1w-HozGvoEgCGCOML954svCBxqL-kib4sYFDUnZkSrEQAFtbbPgTPzQ8/s1600/JQuery+Validation.png&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhm65FdfvuFlXSzolGgS58PI3OGTOJLhzmGxl8vFvGAe2tiGofchWgO_CE1JMMh5AV5OgCRtQMBdyVTdP00caO1w-HozGvoEgCGCOML954svCBxqL-kib4sYFDUnZkSrEQAFtbbPgTPzQ8/s1600/JQuery+Validation.png&quot; height=&quot;200&quot; width=&quot;183&quot;&gt;&lt;/a&gt;&lt;/div&gt;
&lt;b&gt;&lt;a href=&quot;http://www.visualstudiolearn.com/search/label/jQuery&quot;&gt;jQuery&lt;/a&gt;&lt;/b&gt; is&lt;b&gt; &lt;a href=&quot;http://www.visualstudiolearn.com/search/label/JavaScript&quot;&gt;JavaScript&lt;/a&gt;&lt;/b&gt; library containing a lot of important and usable function which facilitate our Application. Java script is client side language and which run on client browser.&lt;br&gt;
&lt;br&gt;
In most of web application we have to validate user input before submitting to Server. You can validate user input on Client side using Java script or &lt;b&gt;jQuery&lt;/b&gt; and on Server Side  using server side programming language like &lt;b&gt;PHP, &lt;a href=&quot;http://www.visualstudiolearn.com/search/label/Asp%20.net&quot;&gt;Asp.net&lt;/a&gt;, &lt;a href=&quot;http://www.visualstudiolearn.com/search/label/JAVA&quot;&gt;Java&lt;/a&gt;&lt;/b&gt; and etc.But when you validate input to server side then first request send to server with all user Inputs, then it check whether  these input are correct or not the return to client to display error.&lt;br&gt;
&lt;br&gt;
&lt;/div&gt;&lt;a href=&quot;http://www.visualstudiolearn.com/2014/07/jquery-form-validation-sample-example.html#more&quot;&gt;READ MORE&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.visualstudiolearn.com/feeds/2002445483530498632/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.visualstudiolearn.com/2014/07/jquery-form-validation-sample-example.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/2002445483530498632'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/2002445483530498632'/><link rel='alternate' type='text/html' href='http://www.visualstudiolearn.com/2014/07/jquery-form-validation-sample-example.html' title='jQuery Form Validation Sample Example'/><author><name>The Loonie Lesson</name><uri>http://www.blogger.com/profile/02243798795439866634</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRY4V9cFLhqMxHWBOWCGBiGUqjan14Vda_dy5rz-C41kApceM-u6jAxS2_6AIRZtGkQeyiQolq1gCeSBhpo573lYq37MLmMGgSKDQLasowic65VoitivYCzqrmUDUqxJuqCTyzah8JuALYtW0Iz7rXIvIcj-ovhVpEuEhQJ6yG1FUQ-A/s1600/loonie-lesson-logo-profile.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhm65FdfvuFlXSzolGgS58PI3OGTOJLhzmGxl8vFvGAe2tiGofchWgO_CE1JMMh5AV5OgCRtQMBdyVTdP00caO1w-HozGvoEgCGCOML954svCBxqL-kib4sYFDUnZkSrEQAFtbbPgTPzQ8/s72-c/JQuery+Validation.png" height="72" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8861905861585113921.post-2240800271246777468</id><published>2014-06-28T14:30:00.000-04:00</published><updated>2026-11-22T02:50:38.088-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Html5"/><title type='text'>How to add hyperlink to table row &amp;lt;tr&amp;gt; ?</title><content type='html'>
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQIQRfAxM8gJsuuydD2Ljosy7ttCeMipr15GH7csNXTeL8ksAnuLseZMP8jt90P6ewSUj0spgya1eTpzimw37i4puseeKRglUVpyh4gBaLiBIIiGLihQ0JJBMJhgc9QtCsefuSgn1cLmQ/s1600/How+to+add+hyperlink+to+table+row+t.png&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQIQRfAxM8gJsuuydD2Ljosy7ttCeMipr15GH7csNXTeL8ksAnuLseZMP8jt90P6ewSUj0spgya1eTpzimw37i4puseeKRglUVpyh4gBaLiBIIiGLihQ0JJBMJhgc9QtCsefuSgn1cLmQ/s1600/How+to+add+hyperlink+to+table+row+t.png&quot; height=&quot;200&quot; width=&quot;200&quot;&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;post-text&quot; itemprop=&quot;description&quot;&gt;
&lt;br&gt;
If you having a table with its table row &lt;code&gt;&amp;lt;tr&amp;gt;&lt;/code&gt; generating in a loop to form multiple rows.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
If you  want to give separate &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; link to each &lt;code&gt;&amp;lt;tr&amp;gt;&lt;/code&gt;. Since in table we can add only add data to &lt;code&gt;&amp;lt;td&amp;gt;&lt;/code&gt; only, you are not able to achieve that.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
There is a way to achieve it which is following. &lt;br&gt;
&lt;br&gt;
&lt;/div&gt;&lt;/div&gt;&lt;a href=&quot;http://www.visualstudiolearn.com/2014/06/how-to-add-hyperlink-to-table-row.html#more&quot;&gt;READ MORE&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.visualstudiolearn.com/feeds/2240800271246777468/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.visualstudiolearn.com/2014/06/how-to-add-hyperlink-to-table-row.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/2240800271246777468'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/2240800271246777468'/><link rel='alternate' type='text/html' href='http://www.visualstudiolearn.com/2014/06/how-to-add-hyperlink-to-table-row.html' title='How to add hyperlink to table row &amp;lt;tr&amp;gt; ?'/><author><name>The Loonie Lesson</name><uri>http://www.blogger.com/profile/02243798795439866634</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRY4V9cFLhqMxHWBOWCGBiGUqjan14Vda_dy5rz-C41kApceM-u6jAxS2_6AIRZtGkQeyiQolq1gCeSBhpo573lYq37MLmMGgSKDQLasowic65VoitivYCzqrmUDUqxJuqCTyzah8JuALYtW0Iz7rXIvIcj-ovhVpEuEhQJ6yG1FUQ-A/s1600/loonie-lesson-logo-profile.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQIQRfAxM8gJsuuydD2Ljosy7ttCeMipr15GH7csNXTeL8ksAnuLseZMP8jt90P6ewSUj0spgya1eTpzimw37i4puseeKRglUVpyh4gBaLiBIIiGLihQ0JJBMJhgc9QtCsefuSgn1cLmQ/s72-c/How+to+add+hyperlink+to+table+row+t.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8861905861585113921.post-3441931306928365509</id><published>2014-06-26T15:00:00.000-04:00</published><updated>2026-11-22T02:50:36.897-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Asp .net"/><category scheme="http://www.blogger.com/atom/ns#" term="Deploy Web Sites"/><title type='text'>Free Host or Deploy Asp.net Web Application on somee</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvch-awGmi62-FAeybvsujtHAX1wkUlYGzPsJT4mUUlcSZuVFbzdxme71YmbZYTpif8nlJoeBy9xKQwKGte1aVTTRcew02-SeFgjXw6HczY4lU-TYlZHHGwyXP7Izl-FH3oSXj1NrUjUE/s1600/Free+Host+Asp.net+Web+Application+on+Soome.png&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvch-awGmi62-FAeybvsujtHAX1wkUlYGzPsJT4mUUlcSZuVFbzdxme71YmbZYTpif8nlJoeBy9xKQwKGte1aVTTRcew02-SeFgjXw6HczY4lU-TYlZHHGwyXP7Izl-FH3oSXj1NrUjUE/s1600/Free+Host+Asp.net+Web+Application+on+Soome.png&quot;&gt;&lt;/a&gt;&lt;/div&gt;
During graduation we had created a lot of academics &lt;a href=&quot;http://www.visualstudiolearn.com/search/label/.net&quot;&gt;.net&lt;/a&gt; web application project which are small but interesting. We could not deploying these project to &lt;a href=&quot;http://www.visualstudiolearn.com/search/label/Asp%20.net&quot;&gt;Asp.net&lt;/a&gt; hosting  Server because it was very expensive and we just deploy it locally and presented it to our teacher.&lt;br&gt;
&lt;br&gt;
 But now there are a lot of site which allow us to freely deploy asp.net web application. &lt;a href=&quot;http://somee.com/&quot;&gt;somee.com&lt;/a&gt; is one of them.&lt;br&gt;
&lt;br&gt;
&lt;/div&gt;&lt;a href=&quot;http://www.visualstudiolearn.com/2014/06/frree-host-or-deploy-aspnet-web.html#more&quot;&gt;READ MORE&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.visualstudiolearn.com/feeds/3441931306928365509/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.visualstudiolearn.com/2014/06/frree-host-or-deploy-aspnet-web.html#comment-form' title='11 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/3441931306928365509'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/3441931306928365509'/><link rel='alternate' type='text/html' href='http://www.visualstudiolearn.com/2014/06/frree-host-or-deploy-aspnet-web.html' title='Free Host or Deploy Asp.net Web Application on somee'/><author><name>The Loonie Lesson</name><uri>http://www.blogger.com/profile/02243798795439866634</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRY4V9cFLhqMxHWBOWCGBiGUqjan14Vda_dy5rz-C41kApceM-u6jAxS2_6AIRZtGkQeyiQolq1gCeSBhpo573lYq37MLmMGgSKDQLasowic65VoitivYCzqrmUDUqxJuqCTyzah8JuALYtW0Iz7rXIvIcj-ovhVpEuEhQJ6yG1FUQ-A/s1600/loonie-lesson-logo-profile.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvch-awGmi62-FAeybvsujtHAX1wkUlYGzPsJT4mUUlcSZuVFbzdxme71YmbZYTpif8nlJoeBy9xKQwKGte1aVTTRcew02-SeFgjXw6HczY4lU-TYlZHHGwyXP7Izl-FH3oSXj1NrUjUE/s72-c/Free+Host+Asp.net+Web+Application+on+Soome.png" height="72" width="72"/><thr:total>11</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8861905861585113921.post-7190899725782892553</id><published>2014-06-25T15:00:00.000-04:00</published><updated>2026-11-22T02:50:35.604-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Blogger Tip"/><category scheme="http://www.blogger.com/atom/ns#" term="css"/><category scheme="http://www.blogger.com/atom/ns#" term="Google Code"/><category scheme="http://www.blogger.com/atom/ns#" term="JavaScript"/><title type='text'>Host JavaScript and CSS files for Blogger and How to use Google Code?</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhqViTM6oeUkpBu4sqdiD7oxTaLW0e4wb-yPyiNx5LZ4RzRp3jQHb56ju3RM92lUQClV-ekr1rDq0X4YYf-ALpfHMeSMikxomncFL-NZaBIBpMCsI3nqH8Zm64AT2eKa40aX7T8xLu9uR8/s1600/Where+to+store+JavaScript+and+CSS+files+for+Blogger,+How+to+use+Google+Code.png&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhqViTM6oeUkpBu4sqdiD7oxTaLW0e4wb-yPyiNx5LZ4RzRp3jQHb56ju3RM92lUQClV-ekr1rDq0X4YYf-ALpfHMeSMikxomncFL-NZaBIBpMCsI3nqH8Zm64AT2eKa40aX7T8xLu9uR8/s1600/Where+to+store+JavaScript+and+CSS+files+for+Blogger,+How+to+use+Google+Code.png&quot; height=&quot;240&quot; width=&quot;320&quot;&gt;&lt;/a&gt;&lt;/div&gt;
&lt;a href=&quot;http://www.visualstudiolearn.com/search/label/Blogger%20Tip&quot;&gt;Blogger&lt;/a&gt; and web designer know how much we should care about coding assets, &lt;a href=&quot;http://www.visualstudiolearn.com/search/label/JavaScript&quot;&gt;JavaScript&lt;/a&gt; files  and css etc where to host it ? and manage it.&lt;br&gt;
&lt;br&gt;
Now in present user experience is more important if user feel bad experience form UI then it will effect site visit and ranking.&lt;br&gt;
&lt;br&gt;
There are many  widget JavaScript and  CSS script that makes you site better.&lt;br&gt;
&lt;br&gt;
But most off time we have to Upload or host some of these script file to a hosting server.&lt;br&gt;
&lt;br&gt;
&lt;/div&gt;&lt;a href=&quot;http://www.visualstudiolearn.com/2014/06/host-javascript-and-css-files-for.html#more&quot;&gt;READ MORE&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.visualstudiolearn.com/feeds/7190899725782892553/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.visualstudiolearn.com/2014/06/host-javascript-and-css-files-for.html#comment-form' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/7190899725782892553'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/7190899725782892553'/><link rel='alternate' type='text/html' href='http://www.visualstudiolearn.com/2014/06/host-javascript-and-css-files-for.html' title='Host JavaScript and CSS files for Blogger and How to use Google Code?'/><author><name>The Loonie Lesson</name><uri>http://www.blogger.com/profile/02243798795439866634</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRY4V9cFLhqMxHWBOWCGBiGUqjan14Vda_dy5rz-C41kApceM-u6jAxS2_6AIRZtGkQeyiQolq1gCeSBhpo573lYq37MLmMGgSKDQLasowic65VoitivYCzqrmUDUqxJuqCTyzah8JuALYtW0Iz7rXIvIcj-ovhVpEuEhQJ6yG1FUQ-A/s1600/loonie-lesson-logo-profile.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhqViTM6oeUkpBu4sqdiD7oxTaLW0e4wb-yPyiNx5LZ4RzRp3jQHb56ju3RM92lUQClV-ekr1rDq0X4YYf-ALpfHMeSMikxomncFL-NZaBIBpMCsI3nqH8Zm64AT2eKa40aX7T8xLu9uR8/s72-c/Where+to+store+JavaScript+and+CSS+files+for+Blogger,+How+to+use+Google+Code.png" height="72" width="72"/><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8861905861585113921.post-1777448742593740404</id><published>2014-06-24T15:00:00.000-04:00</published><updated>2026-11-22T02:52:47.702-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Google Search"/><category scheme="http://www.blogger.com/atom/ns#" term="seo"/><title type='text'>Google Search Tip and access site through FTP anonymously.</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjrXX93zWoOrgF4KiW-Rz5pUPl3ppGofWEzB-LQYlMdsBv62q_1qGy71N6tFWrLoq0-hBtljc3jzYEOocMbppwkFD9424QvKcyWkKamLrFsYNIe_me2JGkg-OOg1dHLqxgnEX0YE4GtAdU/s1600/Google+Search+Tip+VisualStudioLearn.jpg&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjrXX93zWoOrgF4KiW-Rz5pUPl3ppGofWEzB-LQYlMdsBv62q_1qGy71N6tFWrLoq0-hBtljc3jzYEOocMbppwkFD9424QvKcyWkKamLrFsYNIe_me2JGkg-OOg1dHLqxgnEX0YE4GtAdU/s1600/Google+Search+Tip+VisualStudioLearn.jpg&quot; height=&quot;155&quot; width=&quot;200&quot;&gt;&lt;/a&gt;&lt;/div&gt;
Google is giant of Technologies. &lt;a href=&quot;http://www.visualstudiolearn.com/search/label/seo&quot;&gt;Google Search &lt;/a&gt;engine is now helping every one students, teachers etc to his respective search.Students use it for school, business people use it for research, and millions more use it for entertainment.&lt;br&gt;
&lt;br&gt;
In this tutorial I will show widely use &lt;b&gt;Google Search Tip and How to access site through FTP anonymously.&lt;/b&gt;&lt;br&gt;
&lt;br&gt;
Did you know that you may not be using Google search to its full potential ? &lt;br&gt;
&lt;br&gt;
There are few amazing tip by using you can access interesting and relevant  data quickly and efficiently.&lt;br&gt;
&lt;br&gt;
&lt;/div&gt;&lt;a href=&quot;http://www.visualstudiolearn.com/2014/06/google-search-tip-and-access-site.html#more&quot;&gt;READ MORE&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.visualstudiolearn.com/feeds/1777448742593740404/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.visualstudiolearn.com/2014/06/google-search-tip-and-access-site.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/1777448742593740404'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8861905861585113921/posts/default/1777448742593740404'/><link rel='alternate' type='text/html' href='http://www.visualstudiolearn.com/2014/06/google-search-tip-and-access-site.html' title='Google Search Tip and access site through FTP anonymously.'/><author><name>The Loonie Lesson</name><uri>http://www.blogger.com/profile/02243798795439866634</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRY4V9cFLhqMxHWBOWCGBiGUqjan14Vda_dy5rz-C41kApceM-u6jAxS2_6AIRZtGkQeyiQolq1gCeSBhpo573lYq37MLmMGgSKDQLasowic65VoitivYCzqrmUDUqxJuqCTyzah8JuALYtW0Iz7rXIvIcj-ovhVpEuEhQJ6yG1FUQ-A/s1600/loonie-lesson-logo-profile.png'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjrXX93zWoOrgF4KiW-Rz5pUPl3ppGofWEzB-LQYlMdsBv62q_1qGy71N6tFWrLoq0-hBtljc3jzYEOocMbppwkFD9424QvKcyWkKamLrFsYNIe_me2JGkg-OOg1dHLqxgnEX0YE4GtAdU/s72-c/Google+Search+Tip+VisualStudioLearn.jpg" height="72" width="72"/><thr:total>2</thr:total></entry></feed>