<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Juan Felipe Arias Aguirre on Medium]]></title>
        <description><![CDATA[Stories by Juan Felipe Arias Aguirre on Medium]]></description>
        <link>https://medium.com/@juan8arias?source=rss-b3c33259b36a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Zt0G1-R75_c5fAhDqoua2g@2x.jpeg</url>
            <title>Stories by Juan Felipe Arias Aguirre on Medium</title>
            <link>https://medium.com/@juan8arias?source=rss-b3c33259b36a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 06:18:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@juan8arias/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Give an Engineer a Problem and He Will LangGraph the Solution]]></title>
            <link>https://medium.com/@juan8arias/give-an-engineer-a-problem-and-he-will-langgraph-the-solution-2f330775b841?source=rss-b3c33259b36a------2</link>
            <guid isPermaLink="false">https://medium.com/p/2f330775b841</guid>
            <category><![CDATA[application]]></category>
            <category><![CDATA[langgraph]]></category>
            <category><![CDATA[energy]]></category>
            <category><![CDATA[llm-applications]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <dc:creator><![CDATA[Juan Felipe Arias Aguirre]]></dc:creator>
            <pubDate>Tue, 02 Dec 2025 19:51:08 GMT</pubDate>
            <atom:updated>2025-12-02T19:51:08.753Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4EAZiNHss8YYLoW657k1YA.gif" /></figure><blockquote>How I used a sledgehammer to crack a nut, and learned to love the graph along the way.</blockquote><p>It started with a simple, personal problem: I wanted to track my energy consumption.</p><p>I have an energy meter. I have a phone. I wanted to take a picture of the meter, send it somewhere, and have it magically tracked. Also, in my defense, no, I don’t have a smart meter nor access to the service provider’s platform.</p><p>The “sensible” engineering solution would be a simple script: receive image -&gt; OCR -&gt; save to DB. Done.</p><p>But we live in the age of AI, so naturally, I chose the <strong>overpowered</strong> route. I decided to build a full-blown AI assistant to handle it.</p><p>However, as I built it, I realized something crucial about building with LLMs: <strong>Not everything should be an agent.</strong></p><p>If you give an LLM <em>too much</em> freedom, it might hallucinate, get distracted, or just fail to do the simple things reliably. You don’t want a “creative” agent parsing a critical meter reading. You want a robot.</p><p>This project, <strong>Energy Budy</strong>, is the result. It’s a demonstration of a <strong>Hybrid Architecture</strong> using <a href="https://github.com/langchain-ai/langgraph">LangGraph</a>. It combines:</p><ol><li><strong>Deterministic Logic</strong>: For things that <em>must</em> happen the same way every time (parsing messages, routing).</li><li><strong>Agentic Intelligence</strong>: For things that require flexibility (answering questions like “How does my usage compare to last month?”).</li></ol><p>It’s a “nice simple project” that proves you can have the best of both worlds: the reliability of code and the flexibility of agents.</p><h3>The Architecture: A Tale of Two Flows</h3><p>The application is structured as a graph, not a single monolithic agent. This allows us to define “fixed actions” that run based on logic, while keeping the “brain” for where it’s actually needed.</p><h3>1. The Main Graph (The Backbone)</h3><p>When a message arrives from WhatsApp (via Twilio), it doesn’t go straight to an LLM. It goes through a structured pipeline:</p><ol><li><strong>Parser</strong>: First, we check what we actually received. Is it an image? Is it text?</li><li><strong>Router</strong>: Based on the input, we decide where to go.</li></ol><ul><li><em>Image?</em> -&gt; Send to the <strong>Classifier</strong> &amp; <strong>Extractor</strong> (Specialized, focused tasks).</li><li><em>Text?</em> -&gt; Send to the <strong>Query Agent</strong> (The “Brain”).</li></ul><p>3.<strong> Writer</strong>: If we extracted data, we save it to BigQuery.</p><p>4.<strong> Responder</strong>: Finally, we format a nice reply.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lscVtfXoj3bSQUE4JTLZBw.png" /></figure><h3>2. Zoom In: The Agent with Tools</h3><p>This is where the “overpowered” part comes in. When you ask a question (e.g., <em>“Based on my last 10 readings, what’s my estimated monthly consumption in euros ?”</em>), the <strong>Query Agent</strong> takes over.</p><p>This isn’t just a chatbot. It’s a <strong>ReAct Agent</strong> equipped with specific tools to interact with the real world. It can query the database, check live electricity prices (still need to find a reliable api to connect this, but for now I have a fallback response 😅 ), and even generate Python plots on the fly.</p><h3>Code Deep Dive: Building the Graph</h3><p>Let’s look at how we actually implement this hybrid approach using LangGraph.</p><h3>1. The State</h3><p>First, we define the AgentState. This is the shared memory that gets passed around between all the nodes in our graph. It&#39;s a typed dictionary, which makes it easy to know exactly what data is available at any point.</p><pre>class AgentState(TypedDict):<br>    message_sid: str<br>    sender: str<br>    message_body: str<br>    media_url: Optional[str]<br>    has_image: bool<br>    is_query: bool<br>    # ... other fields</pre><h3>2. The Workflow Definition</h3><p>This is the core of the application. In src/workflow.py, we define the graph structure. Notice how we add nodes for each step of our process.</p><pre># src/workflow.py<br>def create_workflow() -&gt; StateGraph:<br>    # Initialize StateGraph with AgentState schema<br>    workflow = StateGraph(AgentState)<br>    # Add all sub-agent nodes<br>    workflow.add_node(&quot;parse_message&quot;, parse_message)<br>    workflow.add_node(&quot;classify_image&quot;, classify_image)<br>    workflow.add_node(&quot;extract_reading&quot;, extract_reading)<br>    workflow.add_node(&quot;write_to_bigquery&quot;, write_to_bigquery)<br>    workflow.add_node(&quot;query_handler&quot;, handle_query)<br>    workflow.add_node(&quot;generate_response&quot;, generate_response)<br>    # Set entry point<br>    workflow.set_entry_point(&quot;parse_message&quot;)<br>    # ...</pre><h3>3. The Routing Logic (The “Magic Glue”)</h3><p>The most important part of a hybrid graph is the <strong>Conditional Edge</strong>. This is where we decide whether to be a “Robot” or a “Brain”.</p><p>Instead of asking an LLM “What should I do next?”, we use a simple Python function. This is faster, cheaper, and 100% reliable.</p><pre># src/workflow.py<br>def should_classify_image(state: AgentState) -&gt; Literal[&quot;classify_image&quot;, &quot;query_handler&quot;, &quot;responder&quot;]:<br>    &quot;&quot;&quot;Routes based on message content type.&quot;&quot;&quot;<br>    has_image = state.get(&quot;has_image&quot;, False)<br>    is_query = state.get(&quot;is_query&quot;, False)<br>    if has_image:<br>        return &quot;classify_image&quot;<br>    elif is_query:<br>        return &quot;query_handler&quot;<br>    return &quot;responder&quot;<br># Add the conditional edge to the graph<br>workflow.add_conditional_edges(<br>    &quot;parse_message&quot;,<br>    should_classify_image,<br>    {<br>        &quot;classify_image&quot;: &quot;classify_image&quot;,<br>        &quot;query_handler&quot;: &quot;query_handler&quot;,<br>        &quot;responder&quot;: &quot;generate_response&quot;<br>    }<br>)</pre><h3>4. The “Brain” Side: The Agent with Tools</h3><p>When the router decides we have a text query, we send it to the Query Agent. This is a standard ReAct agent, but because we&#39;ve already filtered out the &quot;noise&quot; (like image processing), it can focus entirely on answering the user&#39;s question.</p><p>We give it specific tools:</p><ol><li>query_readings: To get data from BigQuery.</li><li>get_electricity_price: To fetch live prices.</li><li>generate_plot: To visualize the data.</li></ol><pre># src/nodes/agents/query_agent.py<br>def create_query_agent():<br>    llm = get_chat_model()<br>    tools = [<br>        query_readings,<br>        get_electricity_price,<br>        generate_plot<br>    ]<br>    # System prompt enforces rules like &quot;DIRECTLY execute tools&quot;<br>    agent = create_agent(<br>        model=llm,<br>        tools=tools,<br>        system_prompt=SYSTEM_PROMPT,<br>    )<br>    return agent</pre><p>The SYSTEM_PROMPT is crucial here, but the real power comes from the tools we provide.</p><h3>5. The Memory (short-term): Context is King</h3><p>One of the coolest features of LangGraph is how easily it handles conversation history. I didn’t need to build a complex database schema to store chat logs. I just used a <strong>Checkpointer</strong>.</p><pre># src/workflow.py<br>from langgraph.checkpoint.memory import InMemorySaver<br># ... inside create_workflow<br>checkpointer = InMemorySaver()<br>compiled_workflow = workflow.compile(checkpointer=checkpointer)</pre><p>When we invoke the workflow, we just pass a thread_id (in this case, the user&#39;s phone number). LangGraph automatically saves the state after every step.</p><pre># src/workflow.py<br>final_state = workflow.invoke(<br>    initial_state, <br>    config={&quot;configurable&quot;: {&quot;thread_id&quot;: user_phone}}<br>)</pre><p>This means if I ask “What was that reading again?”, the agent knows exactly what we were talking about. It’s “short-term memory” out of the box. Of course for as long as the server is running.</p><h3>Future Plans: Making it Even More OP</h3><p>This version is great, but I’m already planning the next upgrade.</p><p>While the current checkpointer handles the immediate conversation, I want <strong>Long-Term Memory</strong>. I want the agent to remember that I usually check my meter on Sundays, or that my energy bill spiked last July. Imagine an agent that doesn’t just react to your current question, but learns from your habits over months or years. That’s the dream.</p><h3>Conclusion: The Best of Both Worlds</h3><p>By using LangGraph, we didn’t just build a chatbot; we built a reliable system.</p><ul><li><strong>Reliability:</strong> We use deterministic code for the critical path (reading the meter). If I send a picture, it <em>will</em> be processed. The LLM can’t “decide” to ignore it.</li><li><strong>Flexibility:</strong> We use an agent for the open-ended path (questions). I can ask “How much did I spend last week?” and it figures it out.</li></ul><p>So, if you’re building an AI application, ask yourself: <strong>Does this need to be an agent?</strong></p><p>If the answer is “sometimes,” then a graph is probably what you’re looking for. Don’t be afraid to hardcode logic. Your agent (and your users) will thank you.</p><p><em>Check out the full code on </em><a href="https://github.com/juanfe88/energy_buddy"><em>GitHub</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2f330775b841" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>