<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: IO_Node</title>
    <description>The latest articles on Forem by IO_Node (@ramoyald).</description>
    <link>https://forem.com/ramoyald</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3469582%2Ff430f1ec-40ae-47be-9c1e-3e86cfe656c0.jpg</url>
      <title>Forem: IO_Node</title>
      <link>https://forem.com/ramoyald</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ramoyald"/>
    <language>en</language>
    <item>
      <title>Hello again, here's a LangChain Ollama helper sheet :)</title>
      <dc:creator>IO_Node</dc:creator>
      <pubDate>Mon, 09 Feb 2026 15:19:25 +0000</pubDate>
      <link>https://forem.com/ramoyald/hello-again-heres-a-langchain-ollama-helper-sheet--1h45</link>
      <guid>https://forem.com/ramoyald/hello-again-heres-a-langchain-ollama-helper-sheet--1h45</guid>
      <description>&lt;h1&gt;
  
  
  LangChain + Ollama: A Practical Guide to Building AI Agents with Python
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;This guide teaches you how to build real, working AI agents using Ollama and LangChain.&lt;/strong&gt; &lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  What You'll Learn
&lt;/h2&gt;

&lt;p&gt;In this guide, you'll discover:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ How to set up Ollama + LangChain (10 minutes)&lt;/li&gt;
&lt;li&gt;✅ When to use &lt;code&gt;ollama.chat()&lt;/code&gt; vs &lt;code&gt;ChatOllama()&lt;/code&gt; (quick decision tree)&lt;/li&gt;
&lt;li&gt;✅ How to build agents that remember things (persistent storage)&lt;/li&gt;
&lt;li&gt;✅ Real, working examples (copy &amp;amp; paste ready)&lt;/li&gt;
&lt;li&gt;✅ Performance tuning for your machine&lt;/li&gt;
&lt;li&gt;✅ How to deploy to production&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Quick Decision: Which Tool to Use?
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌─────────────────────────────────────────┐
│  Want to use AI in your Python code?    │
└────────────┬────────────────────────────┘
             │
             ▼
┌──────────────────────────────────────────┐
│  Building a multi-step AI agent that     │
│  makes decisions and uses tools?         │
└────────────┬─────────────────────────────┘
             │
        YES  │  NO
             │   └──────────────────────┐
             │                          │
             ▼                          ▼
      Use ChatOllama()          Use ollama.chat()
      ✅ For agents             ✅ For simple queries
      ✅ For tools              ✅ For streaming
      ✅ For state mgmt         ✅ For speed
      ✅ For production         ✅ For prototyping
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Performance at a Glance
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operation&lt;/th&gt;
&lt;th&gt;Time&lt;/th&gt;
&lt;th&gt;Notes&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;ollama.chat()&lt;/strong&gt; response&lt;/td&gt;
&lt;td&gt;15-25ms&lt;/td&gt;
&lt;td&gt;Fastest&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;ChatOllama()&lt;/strong&gt; response&lt;/td&gt;
&lt;td&gt;35-55ms&lt;/td&gt;
&lt;td&gt;More features&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Streaming first token&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;5-20ms&lt;/td&gt;
&lt;td&gt;Real-time feedback&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Tool execution&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;2-12ms&lt;/td&gt;
&lt;td&gt;Overhead varies&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Real-world:&lt;/strong&gt; On a laptop with 8GB RAM, you'll get responses in &lt;strong&gt;under 100ms&lt;/strong&gt; most of the time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For local AI, this is blazingly fast.&lt;/strong&gt; (Cloud APIs add 500ms+ of network latency)&lt;/p&gt;




&lt;h2&gt;
  
  
  Part 1: Simple Queries with &lt;code&gt;ollama.chat()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;When to use:&lt;/strong&gt; You just need to ask the AI something and get an answer.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setup (2 minutes)
&lt;/h3&gt;

&lt;p&gt;First, make sure Ollama is running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Terminal 1: Start Ollama&lt;/span&gt;
ollama serve
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you're ready to code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Your First Query
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ollama&lt;/span&gt;

&lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ollama&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;chat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;qwen2.5-coder:latest&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;role&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;user&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;content&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;What is 2 + 2?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;message&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;content&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="c1"&gt;# Output: "2 + 2 equals 4"
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Streaming (See Responses as They Generate)
&lt;/h3&gt;

&lt;p&gt;Want to see the AI think in real-time?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ollama&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;AI: &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;flush&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;ollama&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;chat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;qwen2.5-coder:latest&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;role&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;user&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;content&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Write a haiku about code&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;
&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;message&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;content&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;flush&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  &lt;span class="c1"&gt;# Newline at end
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AI: Lines of logic dance,
Bugs and fixes both take turns—
Code shapes the future.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Multi-Turn Conversation (Remember Context)
&lt;/h3&gt;

&lt;p&gt;Ask follow-up questions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ollama&lt;/span&gt;

&lt;span class="n"&gt;messages&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;user_input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;You: &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Add your message
&lt;/span&gt;    &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;role&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;user&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;content&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;user_input&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;

    &lt;span class="c1"&gt;# Get response
&lt;/span&gt;    &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ollama&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;chat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;qwen2.5-coder:latest&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;messages&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;ai_response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;message&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;content&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;AI: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;ai_response&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Add AI's response so it remembers context
&lt;/span&gt;    &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;role&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;assistant&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;content&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ai_response&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Try this conversation:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;You: What is a lambda function in Python?
AI: A lambda function is a small anonymous function...

You: How is it different from a regular function?
AI: Great question! The key differences are...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how the AI knows you're talking about Python, because it remembers the context.&lt;br&gt;
Once context limit is reached, expect errors to appear.&lt;/p&gt;


&lt;h2&gt;
  
  
  Part 2: Building AI Agents with &lt;code&gt;ChatOllama()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;When to use:&lt;/strong&gt; You're building something more sophisticated—agents that make decisions, use tools, and manage state.&lt;/p&gt;
&lt;h3&gt;
  
  
  Setup (5 minutes)
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;langchain-ollama langchain langgraph
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Your First Agent
&lt;/h3&gt;

&lt;p&gt;An agent is an AI that can:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;✅ Make decisions&lt;/li&gt;
&lt;li&gt;✅ Use tools to accomplish tasks&lt;/li&gt;
&lt;li&gt;✅ Keep track of conversation state&lt;/li&gt;
&lt;li&gt;✅ Handle multiple steps&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's build one that can tell time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain_ollama&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ChatOllama&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain.tools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;tool&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain.agents&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;create_agent&lt;/span&gt;

&lt;span class="c1"&gt;# Step 1: Create a tool
&lt;/span&gt;&lt;span class="nd"&gt;@tool&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_current_time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Get the current time.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;strftime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;%H:%M:%S&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Step 2: Create the AI
&lt;/span&gt;&lt;span class="n"&gt;llm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ChatOllama&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;qwen2.5-coder:latest&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;temperature&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.0&lt;/span&gt;  &lt;span class="c1"&gt;# Be deterministic
&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Step 3: Create the agent
&lt;/span&gt;&lt;span class="n"&gt;agent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;create_agent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;llm&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;tools&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;get_current_time&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="n"&gt;system_prompt&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;You are a helpful time assistant.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Step 4: Use it
&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;agent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;messages&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;role&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;user&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;content&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;What time is it right now?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}]&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;output&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="c1"&gt;# Output: "It is currently 14:23:45"
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What just happened?&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You asked the agent what time it is&lt;/li&gt;
&lt;li&gt;The agent decided it needed to use the &lt;code&gt;get_current_time&lt;/code&gt; tool&lt;/li&gt;
&lt;li&gt;It called the tool and got the time&lt;/li&gt;
&lt;li&gt;It gave you a friendly response&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The agent made the decision. You just provided the tools.&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding Multiple Tools
&lt;/h3&gt;

&lt;p&gt;Tools let your agent accomplish real things:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain.tools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;tool&lt;/span&gt;

&lt;span class="nd"&gt;@tool&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add_numbers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Add two numbers together.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;

&lt;span class="nd"&gt;@tool&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;multiply_numbers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Multiply two numbers together.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;

&lt;span class="c1"&gt;# Create agent with multiple tools
&lt;/span&gt;&lt;span class="n"&gt;agent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;create_agent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;llm&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;tools&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;add_numbers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;multiply_numbers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;get_current_time&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="n"&gt;system_prompt&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;You are a helpful math assistant.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# The agent will decide which tool to use
&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;agent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;messages&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;role&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;user&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;content&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;What&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;s 25 * 4?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}]&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;output&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="c1"&gt;# Output: "25 * 4 equals 100"
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The agent automatically chose the &lt;code&gt;multiply_numbers&lt;/code&gt; tool!&lt;br&gt;
If you need, you can add verbose logging in each of the functions to keep track of which tools were used by the agent. This is also how you protect these tools by creating an input request to confirm the usage of the tool to avoid the agent doing the wrong actions.&lt;/p&gt;
&lt;h3&gt;
  
  
  Agents that Remember Things
&lt;/h3&gt;

&lt;p&gt;What if you want the agent to remember user preferences or conversation history?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;agent_workspace.hybrid_store&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;HybridStore&lt;/span&gt;

&lt;span class="c1"&gt;# Create persistent storage
&lt;/span&gt;&lt;span class="n"&gt;store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;HybridStore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;storage_dir&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;agent_workspace/storage&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Tool that saves preferences
&lt;/span&gt;&lt;span class="nd"&gt;@tool&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;save_preference&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;runtime&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Save a user preference that persists.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;runtime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;store&lt;/span&gt;
    &lt;span class="n"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;preferences&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,),&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;value&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Saved: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; = &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="nd"&gt;@tool&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_preference&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;runtime&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Retrieve a saved preference.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;runtime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;store&lt;/span&gt;
    &lt;span class="n"&gt;pref&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;preferences&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,),&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;pref&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Your &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; is: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;pref&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;value&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;No preference found&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="c1"&gt;# Create agent WITH persistent storage
&lt;/span&gt;&lt;span class="n"&gt;agent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;create_agent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;llm&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;tools&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;save_preference&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;get_preference&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="n"&gt;store&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;store&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;# Connect the storage
&lt;/span&gt;    &lt;span class="n"&gt;system_prompt&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;You help manage user preferences.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Session 1: Save preference
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;=== Session 1 ===&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;result1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;agent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;messages&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;role&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;user&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;content&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Remember that my favorite color is blue&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}]&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;output&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="c1"&gt;# Session 2: Retrieve preference (even after restart!)
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;=== Session 2 (After Restart) ===&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;result2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;agent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;messages&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;role&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;user&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;content&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;What&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;s my favorite color?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}]&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result2&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;output&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="c1"&gt;# Output: "Your favorite color is: blue"
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The magic:&lt;/strong&gt; Data saved in Session 1 is still there in Session 2, even if you restart your computer! The HybridStore will be available in MagicPythong Library, it is a custom made class to save/restore the runtime store from LangChain to file.&lt;/p&gt;




&lt;h2&gt;
  
  
  Part 3: Real-World Examples
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Example 1: A Personal Code Assistant
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain.tools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;tool&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain.agents&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;create_agent&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain_ollama&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ChatOllama&lt;/span&gt;

&lt;span class="nd"&gt;@tool&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;check_python_syntax&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Check if Python code is valid.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;compile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;&amp;lt;string&amp;gt;&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;exec&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;✅ Syntax is valid!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;SyntaxError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;❌ Syntax error: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="nd"&gt;@tool&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;explain_code&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Provide a simple explanation of what code does.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="c1"&gt;# In a real app, you'd call the LLM here
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;This code does X, Y, and Z&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="n"&gt;llm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ChatOllama&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;qwen2.5-coder:latest&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;temperature&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;agent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;create_agent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;llm&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;tools&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;check_python_syntax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;explain_code&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="n"&gt;system_prompt&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;You are a Python code assistant. Help the user write and understand code.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Usage
&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
def greet(name):
    print(f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello, {name}!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;)
&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;

&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;agent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;messages&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;role&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;user&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;content&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Is this Python code valid?&lt;/span&gt;&lt;span class="se"&gt;\n\n&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}]&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;output&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="c1"&gt;# Output: "Yes, this Python code is valid..."
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example 2: A Data Analysis Agent
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain.tools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;tool&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain.agents&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;create_agent&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langchain_ollama&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ChatOllama&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;agent_workspace.hybrid_store&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;HybridStore&lt;/span&gt;

&lt;span class="c1"&gt;# Sample data
&lt;/span&gt;&lt;span class="n"&gt;SALES_DATA&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;product&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Laptop&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;sales&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;product&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Phone&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;sales&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;product&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Tablet&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;sales&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;28&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;product&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Headphones&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;sales&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;35&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="nd"&gt;@tool&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_sales_data&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Get the latest sales data.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SALES_DATA&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nd"&gt;@tool&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;save_report&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;summary&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;runtime&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Save analysis report.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;runtime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;store&lt;/span&gt;
    &lt;span class="n"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;reports&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,),&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;latest&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;summary&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;summary&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Report saved!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="nd"&gt;@tool&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_saved_report&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;runtime&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Retrieve the latest saved report.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;runtime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;store&lt;/span&gt;
    &lt;span class="n"&gt;report&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;reports&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,),&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;latest&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;report&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Latest report: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;report&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;summary&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;No report found&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="n"&gt;llm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ChatOllama&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;qwen2.5-coder:latest&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;temperature&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;HybridStore&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;agent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;create_agent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;llm&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;tools&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;get_sales_data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;save_report&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;get_saved_report&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="n"&gt;store&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;store&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;system_prompt&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;You are a data analyst. Help users understand their sales data.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Usage
&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;agent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;messages&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;role&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;user&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;content&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Analyze our sales data and give me a summary&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}]&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;output&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Part 4: Choosing the Right Model
&lt;/h2&gt;

&lt;p&gt;Ollama has different size models. Pick based on your computer:&lt;/p&gt;

&lt;h3&gt;
  
  
  If you have 4GB or less RAM
&lt;/h3&gt;

&lt;p&gt;Use &lt;strong&gt;Qwen2.5-Coder 1.5B&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;llm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ChatOllama&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;qwen2.5-coder:1.5b&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Fast&lt;br&gt;&lt;br&gt;
⚠️ Less capable&lt;/p&gt;
&lt;h3&gt;
  
  
  If you have 8GB RAM
&lt;/h3&gt;

&lt;p&gt;Use &lt;strong&gt;Qwen2.5-Coder 7B&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;llm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ChatOllama&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;qwen2.5-coder:7b&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Good balance&lt;br&gt;&lt;br&gt;
✅ Handles most tasks&lt;/p&gt;
&lt;h3&gt;
  
  
  If you have 16GB+ RAM
&lt;/h3&gt;

&lt;p&gt;Use &lt;strong&gt;Qwen3-Coder 30B&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;llm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ChatOllama&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;qwen3-coder:30b&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Most capable&lt;br&gt;&lt;br&gt;
⚠️ Slower&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pull a model:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ollama pull qwen2.5-coder:7b
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Part 5: Tuning Performance
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Make responses faster
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;llm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ChatOllama&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;qwen2.5-coder:7b&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;temperature&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;      &lt;span class="c1"&gt;# ← Deterministic (faster)
&lt;/span&gt;    &lt;span class="n"&gt;num_predict&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;128&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;      &lt;span class="c1"&gt;# ← Shorter responses
&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Make responses more creative
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;llm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ChatOllama&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;qwen2.5-coder:7b&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;temperature&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;      &lt;span class="c1"&gt;# ← More creative
&lt;/span&gt;    &lt;span class="n"&gt;num_predict&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;512&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;      &lt;span class="c1"&gt;# ← Longer responses
&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Use GPU (if you have NVIDIA)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;llm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ChatOllama&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;qwen2.5-coder:7b&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;num_gpu&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;35&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;           &lt;span class="c1"&gt;# ← Use GPU layers
&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Part 6: Common Issues &amp;amp; Fixes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Issue 1: "Connection refused"
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Getting an error when trying to use the AI&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Terminal 1: Start Ollama&lt;/span&gt;
ollama serve
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then run your Python code in a different terminal.&lt;/p&gt;

&lt;h3&gt;
  
  
  Issue 2: "Model not found"
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Error says the model doesn't exist&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Download the model&lt;/span&gt;
ollama pull qwen2.5-coder:latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Issue 3: "Out of memory"
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; "CUDA out of memory" or system slows down&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; Use a smaller model&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Instead of 32B
&lt;/span&gt;&lt;span class="n"&gt;llm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ChatOllama&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;qwen2.5-coder:7b&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Issue 4: Slow responses
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Takes too long to get a response&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;llm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ChatOllama&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;qwen2.5-coder:1.5b&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;# Smaller model
&lt;/span&gt;    &lt;span class="n"&gt;temperature&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;              &lt;span class="c1"&gt;# Deterministic
&lt;/span&gt;    &lt;span class="n"&gt;num_predict&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;128&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;              &lt;span class="c1"&gt;# Shorter output
&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Part 7: Next Steps
&lt;/h2&gt;

&lt;p&gt;You now have enough to build:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ Chat bots&lt;/li&gt;
&lt;li&gt;✅ Code assistants&lt;/li&gt;
&lt;li&gt;✅ Data analysis agents&lt;/li&gt;
&lt;li&gt;✅ Personal AI assistants&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Ollama&lt;/strong&gt;: &lt;a href="https://ollama.ai" rel="noopener noreferrer"&gt;https://ollama.ai&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LangChain&lt;/strong&gt;: &lt;a href="https://langchain.com" rel="noopener noreferrer"&gt;https://langchain.com&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Qwen2.5-Coder&lt;/strong&gt;: &lt;a href="https://github.com/QwenLM/Qwen" rel="noopener noreferrer"&gt;https://github.com/QwenLM/Qwen&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Happy coding! 🚀&lt;/p&gt;

</description>
      <category>python</category>
      <category>ai</category>
      <category>langchain</category>
      <category>programming</category>
    </item>
    <item>
      <title>Back with another cancerous code block to share</title>
      <dc:creator>IO_Node</dc:creator>
      <pubDate>Tue, 25 Nov 2025 02:50:20 +0000</pubDate>
      <link>https://forem.com/ramoyald/back-with-another-cancerous-code-block-to-share-4cig</link>
      <guid>https://forem.com/ramoyald/back-with-another-cancerous-code-block-to-share-4cig</guid>
      <description>&lt;p&gt;Look at this bad boy I cooked up to extract a folder location from an error handling XD&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def extract_cwd(data):
    import re
    match = re.search(r'File "(.*?\.py)", line \d+, in &amp;lt;module&amp;gt;', data)
    if match:
        print(f"Current Working Directory (CWD): {match.group(1)}")
    else:
        print("CWD not found in the traceback string.")
try:
    crash
except Exception as e:
    from traceback import format_exc
    extract_cwd(format_exc())

# Output - :&amp;gt; Current Working Directory (CWD): path/to/script
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I also made a smaller version using subprocess, just change cd to pwd if on linux&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def extract():
    from subprocess import run
    return run("cd", shell=True, check=True, capture_output=True, text=True, encoding='utf-8')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is all, thanks for tuning in.&lt;/p&gt;

</description>
      <category>cursed</category>
      <category>python</category>
      <category>beginners</category>
      <category>devjournal</category>
    </item>
    <item>
      <title>Been a bit eh</title>
      <dc:creator>IO_Node</dc:creator>
      <pubDate>Wed, 15 Oct 2025 20:01:41 +0000</pubDate>
      <link>https://forem.com/ramoyald/been-a-bit-eh-3bh0</link>
      <guid>https://forem.com/ramoyald/been-a-bit-eh-3bh0</guid>
      <description>&lt;p&gt;Well we all got the news about win10 EOL. I've been tinkering a linux build to see if I switch or if I fight to get win11 offline. I mean if I'm going to fight windows OS, might as well fight a linux kernel instead, at least I know I can win eventually. So far I would only lose access to some games, most of the current software I'm using seems to be available and working. Also started more programming projects as most of them are helper tools to assist me in coding bigger softwares. I need to start learning on how to manage git safely to not expose my account which is why I'm aiming at a linux security approach even though there's more CVEs at the moment. It seems I'm not the only one that are looking for desktop solution in the linux environment. I'm choosing Fedora since it's usually well maintained and I have the most experience using. &lt;/p&gt;

&lt;p&gt;Also the last post I wanted to make was about sourcing oneself, taking a breather and learning to view my approach from a back view standpoint. I like writing scripts that are useful but investing a lot of time into creating sofisticated softwares might render me insane overtime. Lots of security prevention, good habits and a decent system to support any endeavors I'm enjoying doing for me and others. I don't have much people reading me here but for those that take the time to read these, thank you and I wish our path might cross one day if ever you enjoy my views on programming and technology.&lt;/p&gt;

</description>
      <category>devjournal</category>
      <category>beginners</category>
      <category>linux</category>
      <category>microsoft</category>
    </item>
    <item>
      <title>Created my first bot today</title>
      <dc:creator>IO_Node</dc:creator>
      <pubDate>Wed, 03 Sep 2025 19:23:49 +0000</pubDate>
      <link>https://forem.com/ramoyald/created-my-first-bot-today-341e</link>
      <guid>https://forem.com/ramoyald/created-my-first-bot-today-341e</guid>
      <description>&lt;p&gt;Got asked to create an automated bot using Mastodon APIs so I did :)&lt;/p&gt;

&lt;p&gt;I'll say more about it another day but I while creating it I thought about something that lots of people I know get confused: bytes and bits, so I make a short code snippets to convert and showcase the differences.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def bytes_to_bits(byte_count: int) -&amp;gt; int:
    return byte_count * 8

def bits_to_bytes(bit_count: int) -&amp;gt; int:
    # floor division (no half-bytes possible)
    return bit_count // 8

def bytes_to_bits(byte_seq: bytes, sep: str = " ") -&amp;gt; str:
    """
    Convert a sequence of bytes into a string of bits.
    Each byte is 8 bits, padded with leading zeros.

    :param byte_seq: bytes object (b'abc' or b'\xff\x10')
    :param sep: separator string between bytes (default: space)
    :return: bit string (e.g. '01100001 01100010')
    """
    return sep.join(f"{byte:08b}" for byte in byte_seq)


def bits_to_bytes(bit_str: str, sep: str = " ") -&amp;gt; bytes:
    """
    Convert a bit string back into raw bytes.

    :param bit_str: string of bits (with optional separators)
    :param sep: separator used between bytes (default: space)
    :return: bytes object
    """
    # Remove separators if present
    parts = bit_str.split(sep) if sep else [bit_str]
    return bytes(int(part, 2) for part in parts if part)

print(bytes_to_bits(512))      # 4096 bits
print(bits_to_bytes(4096))     # 512 bytes

data = b"A"  # ASCII 65
bitstring = bytes_to_bits(data)
print(bitstring)  # '01000001'

data2 = b"Hi"
print(bytes_to_bits(data2, sep="|"))
# '01001000|01101001'

# Back to bytes
recovered = bits_to_bytes(bitstring)
print(recovered)  # b'A'

recovered2 = bits_to_bytes("01001000|01101001", sep="|")
print(recovered2.decode())  # 'Hi'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since API calls use network, those are usually stored in byte size chunks and each bit is important. b"string" will create a byte version of that string where each character is a byte. I added a bit separator so each bit value can be used individually if needed. I hope you have fun with binary&lt;br&gt;
~IO &lt;/p&gt;

</description>
      <category>devjournal</category>
      <category>mastodon</category>
      <category>beginners</category>
      <category>python</category>
    </item>
    <item>
      <title>I'm not even sorry for this</title>
      <dc:creator>IO_Node</dc:creator>
      <pubDate>Mon, 01 Sep 2025 23:32:08 +0000</pubDate>
      <link>https://forem.com/ramoyald/im-not-even-sorry-for-this-h1f</link>
      <guid>https://forem.com/ramoyald/im-not-even-sorry-for-this-h1f</guid>
      <description>&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;verify_params=lambda p:print("Verifying the parameters of the script...") or (lambda: [print(f"Script path: {extract_full_path('python_code_checker',p[0])}"), (lambda lp: lp==2 and (fix_path_from_param(p), (lambda f:(check_outside_cwd(f), (lambda: (f.exists() or (_ for _ in ()).throw(FileNotFoundError(f"File not found: '{p[1]}'"))), (lambda: (lambda c:(lambda: c["config"])(json.load(open(f,'r',encoding='utf-8'))))((lambda: (print('Unrecognized config filename, checking for python script instead.') or (print_analyze_menu(f) if f.parts[-1].endswith('.py') else print('Unrecognized python script file')) or json.load(open(f,'r',encoding='utf-8'))))()) ) )() )() )(extract_full_path('python_code_checker',p[1]))) or (lp==3 and print('This test only supports 1 config argument')) or (lp&amp;gt;3 and print('Final script only supports 2 arguments')) or (lp==1 and (print('Script executed without argument, easter egg triggered'),default_config)) or (lp==0 and print('Error: system is unable to find any active script')) or (lp&amp;gt;0 and default_config)])(len(p)))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;yes I figured out how to play golf in python. Enjoy this cringe-fest. 1000 lines scripts? pffft more like 1 line XD&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
    </item>
    <item>
      <title>Algorithmic Suggestion Extraction #1</title>
      <dc:creator>IO_Node</dc:creator>
      <pubDate>Mon, 01 Sep 2025 18:38:17 +0000</pubDate>
      <link>https://forem.com/ramoyald/algorithmic-suggestion-extraction-1-2ipl</link>
      <guid>https://forem.com/ramoyald/algorithmic-suggestion-extraction-1-2ipl</guid>
      <description>&lt;p&gt;  &lt;iframe src="https://www.youtube.com/embed/Rb8e1H4hMFQ"&gt;
  &lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>ai</category>
      <category>learning</category>
    </item>
    <item>
      <title>The importance of self-approval</title>
      <dc:creator>IO_Node</dc:creator>
      <pubDate>Sun, 31 Aug 2025 21:25:44 +0000</pubDate>
      <link>https://forem.com/ramoyald/the-importance-of-self-approval-3pkj</link>
      <guid>https://forem.com/ramoyald/the-importance-of-self-approval-3pkj</guid>
      <description>&lt;p&gt;TL/DR: Only how you perceive your work matters really. Don't be scared of learning to find solutions. If you see something missing, it might mean you were meant to create it. Class Dunder tutorial at the end.&lt;/p&gt;

&lt;p&gt;I bet most of you here had to go through this type of thought process to motivate you to keep pushing even when everything seems unachievable. Sometime it's because you took on something that was more complex than you previously thought or while fixing an issue, you end up with a bigger problem to figure out than a simple bug fix. Those surprise challenges that appears on the path often drives the best of us to self-torture for not being able to figure it out in a timely manner that prevents peers from pressuring for a solution. &lt;/p&gt;

&lt;p&gt;Always remember, you are okay. Not all solution can be found immediately, even if the unrealistic expectation of others are looming over your shoulder. Let them keep expecting unreal results and be disappointed. Did you try to fix it? If the answer if yes then you have more to be proud about it than anyone complaining about any new problems that appears.&lt;/p&gt;

&lt;p&gt;Personnally, I'm starting to find that my code_checker project has put me in this situation many times, forcing me to slow down and learn the intricacies of python. I recomfort myself in knowning that no matter what problem I face, there's a solution. Might not be an immediately found solution, but the possibility of a problem being solved is way higher nowadays compared to when I started learning coding in the late 1990. &lt;/p&gt;

&lt;p&gt;We are in an era where coding can be as pretty and efficient as it could ever be, we just need to focus on creating it and making it accessible. How many of you have complaints about a specific language just because it's missing parts of another language that you like? I know that I constantly have to pre-patch any language that I want to use to make it behave in a way that feels more comfortable for a HUMAN to use. &lt;/p&gt;

&lt;p&gt;I do think we are about to experience a moment in computer science where coding will have two distinct version, the low-level machine coding and the high-level human applications. Although some humans do like to code low-level language, most of the synthax is unreadable without highly advanced knowledge of that language (usually C or Rust) which still would categorize it as more adapted for machine to handle than humans. There's a reason why many of us gives this level of importance to what is arguably and subjectively called clean code. &lt;/p&gt;

&lt;p&gt;Although the specifics of clean code can be slightly different between individuals, the overall objective of using clean code should always be the same: easy to read and understand. I always thought of any attempts at recreating an algorithmic synthax as attempting to create clean code. I am currently working on a workaround meant specifically for cleaning and standardizing python class objects usage and output. Here's an example of a big flaw of class object in python; the lack of native support for them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;🟢 Summary

Operation       
Foo + Foo
Normal Class (class Foo)    
❌ TypeError (no __add__)  
With Dunders / Meta
✅ if metaclass defines __add__

Operation
Foo(…) + Foo(…) (instances)
Normal Class (class Foo)    
❌ TypeError (no __add__)  
✅ if instance defines __add__

Operation
Foo[0]
Normal Class (class Foo)    
❌ TypeError (not subscriptable)
With Dunders / Meta 
✅ if metaclass defines __getitem__

Operation
Foo_instance(…)
Normal Class (class Foo)    
❌ TypeError (not callable)
With Dunders / Meta 
✅ if instance defines __call__
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So I learned how Python works and most of my software do exactly what I want them to do, that is until I started using objects. Then it became a dunder hunting nightmare as each time I tried to use an object in a new scenario, it would always end up failing because of the lack of support for them. &lt;/p&gt;

&lt;p&gt;And thus Magic Pythong was born, an honest attempt at making class objects more usable natively through a simple package/module import. I'm still gathering any possible feedback about already existing libraries that does similar stuff but so far nothing. This specific "patch" is what I will identify officially as an UniversalObject class, a class that has all missing native operation support and it's accessible from the scope it is imported into. &lt;/p&gt;

&lt;p&gt;Want to use a boolean check with two class stored value? you now can. Want to addition two int value stored in two objects? you now can. Want to get the type of a value stored into an object? it will now give you the proper type instead of always outputing type. Want to create an actual constant value that is protected? you bet you can now. Why was those options not already available? I have no idea but I bet it has to do with Python Interpreter.&lt;/p&gt;

&lt;p&gt;This UniversalObject class is actually ready for use and collaborative work. Before I attempt any opening of my code publicly, I want to learn how to do it safely. Many article I've read about people accidently exposing their API keys because they use vibe coding in their project management. So until I feel comfortable opening it, I'll keep learning how to properly do it and create more workarounds that I want. &lt;/p&gt;

&lt;p&gt;This is when I thought about java. Yes java, ugh. So I thought about locking an object for access. It's somewhere in your code but it needs a key to be used. It reminded me of the private / public function system that java supports and felt it could be a great addition to have if ever I wanted to start creating video games with secret levels. This was the birth of my bane; LocalObject.&lt;/p&gt;

&lt;p&gt;This is thing is a pain to create, not only is it hard to create the workaround to make class usable natively, adding a lock on each of those workaround to avoid data leak is extremely frustrating. the &lt;strong&gt;getattr&lt;/strong&gt; and &lt;strong&gt;setattr&lt;/strong&gt; dunders just dont want to play nice with me at all and they got me stuck into testing different approach for a whole week.&lt;/p&gt;

&lt;p&gt;So more learning it is, decided to get a closer look at them, and again, using my favorite GPT friend to produce me some decent starting points for researching. I'm using these posts to share the data I'm extracting so that if ever you feel it's something you could use to learn, then I'm glad I helped you.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;🐍 Python Class Dunder Hacks: A Full Tutorial

Dunder (double underscore) methods are hooks Python calls automatically. They’re not “magic,” but they feel like it because you never call them directly — Python does.

1. Object Lifecycle

These dunders control how objects are created and destroyed.

__new__(cls, *args, **kwargs)
Called before __init__. Returns the actual instance.
Hack: implement singletons or immutable types.

class OnlyOne:
    _instance = None
    def __new__(cls, *args, **kwargs):
        if cls._instance is None:
            cls._instance = super().__new__(cls)
        return cls._instance


__init__(self, ...)
Normal initializer.

__del__(self)
Destructor (called when garbage-collected). Rarely reliable.

2. Attribute Control

These dunders let you intercept attribute access like a proxy.

__getattr__(self, name)
Called when attribute name doesn’t exist.

class Lazy:
    def __getattr__(self, name):
        return f"Generated {name} dynamically"


__getattribute__(self, name)
Called always for every attribute access. (Be careful: must avoid recursion by using object.__getattribute__).

class Spy:
    def __getattribute__(self, name):
        print(f"Accessed {name}")
        return object.__getattribute__(self, name)


__setattr__(self, name, value)
Intercepts obj.attr = value.

class Locked:
    def __setattr__(self, name, value):
        if name.startswith("_"):
            object.__setattr__(self, name, value)
        else:
            raise TypeError("Locked object: cannot set attributes")


__delattr__(self, name)
Called on del obj.attr.

__dir__(self)
Customizes dir(obj) output.

3. Representation &amp;amp; Conversion

These dunders make your objects look nice.

__repr__(self) → unambiguous string (for devs).

__str__(self) → pretty string (for users).

__bytes__(self) → bytes(obj).

__format__(self, spec) → f-string formatting.

class Person:
    def __init__(self, name):
        self.name = name
    def __repr__(self):
        return f"Person({self.name!r})"
    def __str__(self):
        return self.name

4. Container Emulation

Make objects act like lists, dicts, sets.

__len__(self) → len(obj)

__getitem__(self, key) → obj[key]

__setitem__(self, key, value) → obj[key] = value

__delitem__(self, key) → del obj[key]

__iter__(self) → for x in obj:

__contains__(self, item) → item in obj

class VirtualList:
    def __getitem__(self, idx):
        return idx * 10
    def __len__(self):
        return 999

5. Callable &amp;amp; Context Manager

__call__(self, *args, **kwargs) → make an object act like a function.

class Adder:
    def __call__(self, a, b):
        return a + b


__enter__ / __exit__ → context managers (with statement).

class TempFile:
    def __enter__(self): print("OPEN")
    def __exit__(self, exc_type, exc_val, exc_tb): print("CLOSE")

6. Operator Overloading

Arithmetic dunders let objects behave like numbers.

__add__(self, other) → +

__sub__ → -

__mul__ → *

__truediv__ → /

__floordiv__ → //

__mod__ → %

__pow__ → **

__radd__, __rmul__, etc. → reverse ops

__eq__, __lt__, __gt__, __ne__, etc. → comparisons

__hash__ → hash(obj)

class Vec2:
    def __init__(self, x, y): self.x, self.y = x, y
    def __add__(self, o): return Vec2(self.x+o.x, self.y+o.y)
    def __repr__(self): return f"Vec2({self.x},{self.y})"

7. Class Control

Dunders at the class level.

__class_getitem__(cls, key) → allow generics (MyClass[int]).

__instancecheck__ / __subclasscheck__ → customize isinstance/issubclass.

__init_subclass__ → hook called when subclassing.

__mro_entries__ → control multiple inheritance resolution.

__prepare__ (in metaclasses) → customize class dict before creation.

class Meta(type):
    def __new__(mcls, name, bases, dct):
        print(f"Creating {name}")
        return super().__new__(mcls, name, bases, dct)

class MyClass(metaclass=Meta): pass

8. Pickling &amp;amp; Copying

__getstate__ / __setstate__ → for pickle.

__reduce__ → advanced pickling control.

__copy__, __deepcopy__ → for copy module.

9. Async &amp;amp; Awaitable

__await__ → await obj

__aiter__, __anext__ → async iteration

__aenter__, __aexit__ → async context manager.

class AsyncCounter:
    def __aiter__(self): self.i = 0; return self
    async def __anext__(self):
        self.i += 1
        if self.i &amp;gt; 3: raise StopAsyncIteration
        return self.i

🧨 Real Hacks You Can Pull Off

Sealed objects → lock attributes with __setattr__.

Lazy loading → __getattr__ to fetch things on demand.

Immutable data classes → override __setattr__ to block changes.

Fluent DSLs → __call__ + __getitem__ to make objects behave like mini-languages.

Proxy objects → wrap APIs with logging via __getattribute__.

Sandboxing → disallow dangerous attributes dynamically.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is where I'm currently working on, learning those dunder hacks properly so I can start working together with the agent on the fix. I usually work in the testing lab instead of assisting the agent and simply do vibe coding cleanup once all the premade test passes. This is how I could identify that the locking mechanism was being skipped instead of refactored. &lt;/p&gt;

</description>
      <category>programming</category>
      <category>devjournal</category>
      <category>beginners</category>
      <category>python</category>
    </item>
    <item>
      <title>They can't always be good days</title>
      <dc:creator>IO_Node</dc:creator>
      <pubDate>Sun, 31 Aug 2025 15:28:30 +0000</pubDate>
      <link>https://forem.com/ramoyald/they-cant-always-be-good-days-4o5d</link>
      <guid>https://forem.com/ramoyald/they-cant-always-be-good-days-4o5d</guid>
      <description>&lt;p&gt;It seems I was cheering a bit too early my success haha. It seems the AI agent decided to simply bypass the locking mechanism instead of making it work with constant values. Tool is running with insufficient ressources so it takes full days for it to execute anything. I love this setup where the agent is slow as it forces me to work manually as well. I strongly suggest it to any begginers, tandem work on dual project, one is manual and the other is slow but automatic. While the agent is finishing up the class lock, I'll work to figure out how class inheritance / metaclass fed to it can be detected or backtracked for guidance purposes. Didn't even knew class had this system so I'm still exploring and might be doing this for the whole day.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;🟦 1. Basic Classes

A class in Python is just a template for creating objects.

class Animal:
    def speak(self):
        return "some sound"

a = Animal()
print(a.speak())  # "some sound"

🟦 2. Inheritance (Re-using / Extending Classes)

Inheritance means one class reuses or extends another.

class Dog(Animal):   # Dog inherits from Animal
    def speak(self):
        return "woof!"


Dog gets everything from Animal.

If you don’t override, it just uses the parent’s methods.

If you override (def speak), your new version is used.

d = Dog()
print(d.speak())  # "woof!"

🔹 Multiple Inheritance

Python allows a class to inherit from multiple classes.

class Walker:
    def move(self):
        return "walking"

class Swimmer:
    def move(self):
        return "swimming"

class Duck(Walker, Swimmer):
    pass


Now, which move() does Duck use?
Python resolves it using the Method Resolution Order (MRO) → a deterministic order of lookup.

duck = Duck()
print(duck.move())  
# "walking", because Walker is listed first
print(Duck.__mro__)  
# (&amp;lt;class '__main__.Duck'&amp;gt;, &amp;lt;class '__main__.Walker'&amp;gt;, &amp;lt;class '__main__.Swimmer'&amp;gt;, &amp;lt;class 'object'&amp;gt;)

🟦 3. Constructors (__init__)

When you do obj = ClassName(...), Python calls the class’s __init__ method.

Parent constructors can be called with super().

class Animal:
    def __init__(self, name):
        self.name = name

class Dog(Animal):
    def __init__(self, name, breed):
        super().__init__(name)  # call parent __init__
        self.breed = breed

d = Dog("Rex", "Labrador")
print(d.name, d.breed)  # Rex Labrador

🟦 4. Metaclasses (Classes of Classes)

Now the tricky part:

In Python, everything is an object, even classes themselves.

If objects are created by classes… what creates classes?
→ The answer: a metaclass.

By default, Python uses type as the metaclass.

# "Animal" is itself an object created by "type"
print(type(Animal))  # &amp;lt;class 'type'&amp;gt;


So:

Normal objects are instances of a class.

Classes are instances of a metaclass (usually type).

🔹 Custom Metaclass

You can define your own metaclass to control how classes are built.

class Meta(type):  # inherit from 'type'
    def __new__(cls, name, bases, dct):
        print(f"Creating class {name}")
        return super().__new__(cls, name, bases, dct)

class MyClass(metaclass=Meta):
    pass


Output:

Creating class MyClass


Here, Meta.__new__ runs at the moment the class is defined, not when it’s instantiated.
Metaclasses are like blueprints for classes.

🟦 5. When to Use Inheritance vs Metaclasses

Inheritance = organize and reuse code between objects.

Metaclasses = control or modify how classes themselves behave (rare, advanced use cases).

Example metaclass usage:

Enforcing coding standards (e.g., all methods must be lowercase).

Auto-registering classes into a plugin system.

Automatically adding methods/attributes to classes.

🟦 6. Summary

Inheritance:

Reuse code between classes.

Supports multiple parents.

Follows MRO for lookup.

Constructors (__init__):

Define what parameters a class instance takes.

super() allows chaining to parent constructors.

Metaclasses:

Classes are objects, created by a metaclass (type by default).

Custom metaclasses let you intercept or modify class creation.

Powerful, but niche — usually not needed unless building frameworks/libraries.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wasn't too sure about how super() and metaclass usage so I keep going:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;🟦 1. type as “the metaclass of metaclasses”

In Python:

Normally, when you call a class like Dog("Rex"), what happens is not a simple function call.

Internally, Python does something like this:

obj = Dog.__call__(*args, **kwargs)


But Dog.__call__ comes from its metaclass (by default, type).

So the whole flow is:

Dog(...) 
→ Dog.__class__ is type
→ type.__call__(Dog, *args, **kwargs)
→ Dog.__new__(Dog, *args, **kwargs)  # allocate memory
→ Dog.__init__(self, *args, **kwargs) # initialize fields
→ return instance

🟦 2. So what does super() really do?

super() doesn’t mean “the parent”. It means:
“go to the next class in the MRO after my current class.”

Example:

class A:
    def greet(self):
        print("A")

class B(A):
    def greet(self):
        print("B")
        super().greet()

class C(A):
    def greet(self):
        print("C")
        super().greet()

class D(B, C):
    def greet(self):
        print("D")
        super().greet()


MRO:

print(D.__mro__)
# (&amp;lt;class '__main__.D'&amp;gt;, &amp;lt;class '__main__.B'&amp;gt;, &amp;lt;class '__main__.C'&amp;gt;, &amp;lt;class '__main__.A'&amp;gt;, &amp;lt;class 'object'&amp;gt;)


Execution:

D().greet()
# D
# B
# C
# A


Notice how super() followed the MRO, not just “parent”.

🟦 3. Metaclasses in Action

If you give a class a metaclass=Meta, then the creation of the class object itself is intercepted.

class Meta(type):
    def __call__(cls, *args, **kwargs):
        print(f"Creating instance of {cls.__name__}")
        return super().__call__(*args, **kwargs)

class MyClass(metaclass=Meta):
    def __init__(self, x):
        self.x = x


Now:

obj = MyClass(42)


Execution order:

Meta.__call__(MyClass, 42) runs

Inside, super().__call__ delegates to type.__call__

Which calls MyClass.__new__

Then MyClass.__init__

🟦 4. Why it looks “hardcoded”

Yes — you must declare metaclass=Meta because only one metaclass controls class creation.

But note:

class Foo: pass → uses type automatically.

class Foo(metaclass=Meta): pass → uses your Meta.

And since metaclasses are themselves subclasses of type, you can extend the logic of type.__call__, type.__new__, etc.

🟦 5. Analogy

Think of it like a factory system:

Objects = Products (instances you actually use).

Classes = Factories (blueprints for products).

Metaclasses = Factory Designers (blueprints for factories).

When you do:

p = Product()


it’s like:

Call the Factory Designer (metaclass) to create a Factory (class) → happens once at definition.

Call the Factory (class) to produce a Product (object) → happens each time you instantiate.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So here's a quick look under the hood of my learning process using GPT. I also sometime use Gemini through google search but it's usually for code snippets to validate syntax more than to learn new stuff. I hope you learned something as well. This learning experience opened my eyes to the power of Method Resolution Order (MRO). &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use Class.&lt;strong&gt;mro&lt;/strong&gt; to walk inheritance.&lt;/li&gt;
&lt;li&gt;If unresolved locally, parse imports to resolve base classes.&lt;/li&gt;
&lt;li&gt;Once you have class definitions, check &lt;strong&gt;init&lt;/strong&gt; / &lt;strong&gt;new&lt;/strong&gt; signatures → collect their parameter requirements.&lt;/li&gt;
&lt;li&gt;Detect metaclass= as the “entry point” to type class detection and can only have one declaration.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>devjournal</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Surprise Breakthrough</title>
      <dc:creator>IO_Node</dc:creator>
      <pubDate>Sun, 31 Aug 2025 01:39:56 +0000</pubDate>
      <link>https://forem.com/ramoyald/surprise-breakthrough-47g7</link>
      <guid>https://forem.com/ramoyald/surprise-breakthrough-47g7</guid>
      <description>&lt;p&gt;As mentioned in my previous post, I've been working to create a locking mechanism for a class in python and it seems the test initially made for synthax ruling is now passing all the tests!! Here's the output results of using both the UniversalObject and LocalObject classes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;=== UniversalObject Examples ===
Dict creation (normal): {'name': 'John', 'age': 30}
Index keys: ['name', 'age']
Name from index: John

Dict creation (constant): {'name': 'John', 'age': 30}
Index keys: ['name', 'age']
Name from index (should be tuple): ('John',)

List creation (normal): [1, 2, 3]
Index keys: ['arg_0', 'arg_1', 'arg_2']
arg_0 from index: 1

List creation (constant): [1, 2, 3]
Index keys: ['const_0', 'const_1', 'const_2']
const_0 from index (should be tuple): (1,)

Single value (normal): hello
Index keys: ['arg_0']

Single value (constant): hello
Index keys: ['arg_0']
arg_0 from index (should be tuple): ('hello',)

=== Demonstrating Parent Display ===
With show_parent=True: {'test': 'value'}

=== LocalObject Examples ===
Dict creation (normal): &amp;lt;LocalObject:locked&amp;gt;
Index keys: ['name', 'age']

Dict creation (constant): &amp;lt;LocalObject:locked&amp;gt;
Index keys: ['name', 'age']
name from index (should be tuple): ('Jane',)

List creation (normal): &amp;lt;LocalObject:locked&amp;gt;
Index keys: ['arg_0', 'arg_1', 'arg_2']

List creation (constant): &amp;lt;LocalObject:locked&amp;gt;
Index keys: ['const_0', 'const_1', 'const_2']

Single value (normal): &amp;lt;LocalObject:locked&amp;gt;
Index keys: ['arg_0']

Single value (constant): &amp;lt;LocalObject:locked&amp;gt;
Index keys: ['arg_0']
arg_0 from index (should be tuple): ('world',)

=== Demonstrating Constant Protection ===
Constant values are stored as single-element tuples:
u4.const_0 = 1
type(u4.const_0) = &amp;lt;class 'int'&amp;gt;
l4.const_0 = 10
type(l4.const_0) = &amp;lt;class 'int'&amp;gt;

=== Demonstrating LocalObject Locking Behavior ===
Locked LocalObject created:
After unlocking, l7.locked = True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Not only does the naming is standardized, it support real constant / variables and all logical / mathematical operations as if they are regular container instead of class objects. It also has a dict index of all value fed to the class for easy access (.arg_index). &lt;/p&gt;

&lt;p&gt;Step 1 completed, will now work on the multi-lock system using conditions now that I can do normal boolean checks with the objects XD This type() check on constants that gives back the actual type of the value stored instead of always giving a tuple is chef kiss.&lt;/p&gt;

&lt;p&gt;If you are interested in my Magic Pythong journey, feel free to drop a comment about what you would like to see implemented in the future public repo.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>devjournal</category>
      <category>python</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Python Journey Log #1</title>
      <dc:creator>IO_Node</dc:creator>
      <pubDate>Sat, 30 Aug 2025 17:38:56 +0000</pubDate>
      <link>https://forem.com/ramoyald/python-journey-log-1-1f77</link>
      <guid>https://forem.com/ramoyald/python-journey-log-1-1f77</guid>
      <description>&lt;p&gt;Opening:&lt;/p&gt;

&lt;p&gt;Coming from a perl / wscript background, I'm currently focusing on learning and applying my previous application (RAII/SRP) to python. &lt;/p&gt;

&lt;p&gt;Projects Resume:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Music generator that generates visuals using shaders.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Started well, most of the musical theory is fairly accessible through libraries like music21, numpy and midiutil. I can currently generate basic midi song loops. the threejs part for shader support isnt started yet. currently on pause as i need to learn more about python before i can combine other languages.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Python tutorials, also doubles as a code snippet cheat sheet.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Ongoing progress, currently have some pretty decent starting subjects:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;* globals() manipulation and importance
* locals() manipulation and importance
* regex filtering (I hate this synthax, can't wait to do linux python and use grep)
* logical operators for variables and objects
* keywords and built-in functions
* IO and file manipulation
* assembly / source extraction
* classes and custom dunder (__init__, __str__, etc.)
* error handling and bubbling of raise
* multiprocessing and GIL
* matplotlib visuals and application
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;If you have any good subject suggestions, I'm all ears. As you saw, I'm not affraid do dig deep. The more I'm digging, the more I feel python needs my magic touch. Keep reading to see what I mean :)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Python Code Checker!&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Hell yeah I'm going to try this. I can now successfully extract arguments fed into my script from the console, understand what type of argument they are, send them properly toward the right function while preventing any violation during input. I'm pretty happy with the efforts I've put in this one.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;uses AST to create a usable structure&lt;/li&gt;
&lt;li&gt;use logic and regex to identify multiple pieces of code (functions, classes, variables, arguments/parameters of functions, loops, conditions, errors, docstrings, dead values, and still adding more!)&lt;/li&gt;
&lt;li&gt;supports both user input and dynamic console arguments.&lt;/li&gt;
&lt;li&gt;produce a report once completed (currently broken and under maintenance)&lt;/li&gt;
&lt;li&gt;currently working on dynamic import detections and variable backtracking.&lt;/li&gt;
&lt;li&gt;plan of supporting automatic code fixes, code suggestion for issues found, support for linux and configuration editor (config edit almost done)&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;My favorite project so far as I'm learning a lot of stuff doing this, exploring the most system heavy libraries and how to safely travel through them, wouldnt want to recursively delete all my project XD&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Magic Pythong Package&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;So here's why I said python needs some magic. I currently have as an objective to focus on Langchain application in Python.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;The first thing I identified is the lack of actual constant values so my first "magic" is to use tuple type even with single values to protect anything that I consider a constant in my code. &lt;/li&gt;
&lt;li&gt;The second thing I identified is class manipulation through logical operator is a cancer. It needs to be chirurgically removed, which leads to the next "magic": Multi-Locked, Universal and standardized classes. &lt;/li&gt;
&lt;li&gt;While looking into memory manipulation, I started working on what I would consider my 3rd "magic" which is a 100% dynamic module import with an automatic cleaner (I would consider the auto clean the 4th "magic"). Everyone is using globals for import to simplify their usage but that's not tolerated for me. imports should be only used in localized scopes and immediately flushed afterward. You only strategize your imports scope after you are done with the complete structure. This is to plan for really memory intense workflows.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;This is what I would call my MPythong, this is purely personal in nature and is something I would be interested in making publicly available for everyone to use if interest for it catches. It might already exist in some form in another library but I didn't found it yet. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That gives a full round tour of all my current python projects, what I'm aiming them towards and some opening for collaborations for those interested. &lt;/p&gt;

&lt;p&gt;The picture was generated using a special proprietary tool called synthopixel. I have full rights on all content I use. &lt;/p&gt;

</description>
      <category>python</category>
      <category>newbie</category>
      <category>magicpy</category>
      <category>devjournal</category>
    </item>
  </channel>
</rss>
