<?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[Dream AI - Medium]]></title>
        <description><![CDATA[Technical, high-quality, and most importantly, relevant blogs on AI. - Medium]]></description>
        <link>https://medium.com/dream-ai?source=rss----bb4a3200de4c---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Dream AI - Medium</title>
            <link>https://medium.com/dream-ai?source=rss----bb4a3200de4c---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 08:58:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/dream-ai" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Create Your Own AI Agent Without an AI Framework]]></title>
            <link>https://medium.com/dream-ai/create-your-own-ai-agent-without-an-ai-framework-55b9a83c3b05?source=rss----bb4a3200de4c---4</link>
            <guid isPermaLink="false">https://medium.com/p/55b9a83c3b05</guid>
            <category><![CDATA[ai-engineering]]></category>
            <category><![CDATA[ai-agent]]></category>
            <category><![CDATA[pydantic-ai]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[context-engineering]]></category>
            <dc:creator><![CDATA[Hamza Farhan]]></dc:creator>
            <pubDate>Tue, 13 Jan 2026 03:23:33 GMT</pubDate>
            <atom:updated>2026-01-13T03:32:46.364Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OG-Ywdj5SVfJMCh7l9ZEvg.png" /></figure><p>The widely agreed-upon definition for an AI Agent: <a href="https://simonwillison.net/2025/Sep/18/agents/#:~:text=to%20use%20this%3A-,An%20LLM%20agent%20runs%20tools%20in%20a%20loop%20to%20achieve%20a%20goal.,-I%E2%80%99ve%20been%20very"><strong>An LLM agent runs tools in a loop to achieve a goal.</strong></a></p><p>How tho?</p><ol><li>Define the <em>goal</em>. For example: Financial Analysis.</li><li>Define the <em>tools</em>: For example: Tools to load and edit csv files.</li><li>Provide these tools to an LLM and run it in a loop until the <strong><em>GOAL </em></strong>is achieved.</li></ol><p>Now, do we <em>really</em> need an AI framework for this? Let’s find out.</p><h3>Tools</h3><h4>Defining</h4><p>If you’re using Gemini, this is what a <a href="https://ai.google.dev/gemini-api/docs/function-calling?example=weather#step-1"><em>tool</em></a> looks like:</p><pre>weather_tool = {<br>    &quot;name&quot;: &quot;get_current_temperature&quot;,<br>    &quot;description&quot;: &quot;Gets the current temperature for a given location.&quot;,<br>    &quot;parameters&quot;: {<br>        &quot;type&quot;: &quot;object&quot;,<br>        &quot;properties&quot;: {<br>            &quot;location&quot;: {<br>                &quot;type&quot;: &quot;string&quot;,<br>                &quot;description&quot;: &quot;The city name, e.g. San Francisco&quot;,<br>            },<br>        },<br>        &quot;required&quot;: [&quot;location&quot;],<br>    },<br>}</pre><p>get_current_temperature is the name, and we can pass a bunch of tools like this as a list. The LLM decides which tool to use based on their descriptions.</p><h4>Using</h4><p>What does using a tool actually mean? We just sent a list of JSONs to the LLM. How can it then <em>use</em> a tool to set the light values? Well, it can’t.</p><pre>User Prompt: What’s the temperature in London?<br>LLM response: {args={&#39;location&#39;: &#39;London&#39;} name=&#39;get_current_temperature&#39;}</pre><p>So the LLM:</p><ol><li>Read the user prompt.</li><li>Read the descriptions of all of the available tools.</li><li>Decided that the tool with the description “Gets the current temperature for a given location.” is perfect for this task.</li><li>Noticed that this tool has 1 required parameter: “location”.</li><li>Returned the ideal values for these parameters based on the user prompt.</li></ol><p>Now, it’s <em>OUR</em> job to take this JSON and call our get_current_temperature function with these parameters.</p><p>“What function?” I hear you ask. Right, so the JSON above represents a function that we have actually written:</p><pre>def get_current_temperature(location: str) -&gt; dict:<br>    &quot;&quot;&quot;Gets the current weather temperature for a given location.&quot;&quot;&quot;<br>    print(f&quot;Tool Call: get_current_temperature(location={location})&quot;)<br>    # TODO: Make API call<br>    print(&quot;Tool Response: {&#39;temperature&#39;: 25, &#39;unit&#39;: &#39;celsius&#39;}&quot;)<br>    return {&quot;temperature&quot;: 25, &quot;unit&quot;: &quot;celsius&quot;}  # Dummy response</pre><p>So we will select the function based on the <em>name</em> returned and pass in the <em>args.</em></p><p>This function will return the weather like a normal Python function.</p><h3>Framework?</h3><p>If you stopped here and decided to make your own AI agent because of how easy this sounds, these are the first things you’ll notice:</p><h3>Provider Specific Tool Schemas</h3><p>As discussed, a tool is a JSON that represents a Python function. The problem is, Gemini, Claude, and GPT all use slightly different schemas for their tools. So it was already annoying having to define a JSON schema for a basic Python function, you now need to define at least 3 and switch between them using if statements or a dict based on the LLM you choose. You might think that using an OpenAI-compatible API is enough, but more often than not, you miss out on provider-specific parameters.</p><p>Based on the model/provider:</p><ol><li>Change the actual API key and the API request</li><li>Change the tool schemas</li></ol><h4>Tool Returns</h4><p>The LLM selected the tool and sent over the args, we called the function and got the return value. The LLM does not have this value. Yet. That’s where the <em>loop</em> from the AI Agent definition comes in. We need to feed the return value back to the LLM if we want it to make use of it. We may want the LLM to come up with a nice final response, or just wait for the next user prompt. Either way, we <em>usually </em>want it to know what the tool returned.</p><h4>Solution</h4><p>This is where frameworks come in. A framework will let you define a Python function once and just pass that function to the Agent. A framework will let you change a model by just changing a string. A framework will call the functions and accumulate the user prompts, LLM responses, and the function return values in a conversation history that gets fed back to the LLM on every turn. Pretty convenient if you ask me.</p><h4>My Loop</h4><p>So we’ve decided to use a framework, and life is good.</p><p>What if we have a specific function whose return value we don’t want to be fed back in? What if we want to start off with 5 tools and then add/remove 2 tools after a specific tool has been used? What if we want to compress the conversation history after a certain number of tokens/turns to save the context window?</p><p>Every framework gives its own version of <em>hooks </em>to tackle these scenarios, and by the time you’ve implemented 10 custom hooks, you’ll think: “Wouldn’t it be easier if this were <strong><em>my loop</em></strong>?”</p><p>Tempting idea, but what about the 2 issues we’ve highlighted in the <strong>Provider Specific Tool Schemas </strong>section? I like the way you think.</p><h3>Pydantic AI Gateway and Direct Module</h3><p>Before you say anything, this is <strong><em>not</em></strong> about the full-fledged Pydantic AI framework. Just hear me out.</p><p>The <a href="https://pydantic.dev/ai-gateway">Pydantic AI Gateway</a> is a unified interface for accessing multiple AI providers with a single key. So this solves the first half of the first issue defined above.</p><p>The <a href="https://ai.pydantic.dev/direct/">direct module</a> provides low-level methods for making imperative requests to LLMs where the only abstraction is input and output schema translation, enabling you to use all models with the same API.</p><p>So now both of our issues have been solved:</p><ol><li>The same API key with the same API</li><li>The same tool schema because of the translation in the direct module</li></ol><h3>Project-Specific Framework</h3><p>Frameworks like Pydantic AI are very convenient, but I would prefer having full control over the <strong>agentic</strong> <strong>loop. </strong>Especially if the use case is specialized with a particular set of pre-defined tools.</p><p>So let’s make our <strong>framework!</strong></p><p>The entire code is at the end, don’t worry.</p><h4>Use Case</h4><p>An AI Agent that gathers specifications and recommends a phone.</p><p>We’re basically making our own framework for this use case. So no need to think about “the next project”.</p><h4>Instructions (System Prompt)</h4><p>Let’s keep it simple.</p><pre>messages: list[ModelMessage] = [<br>    ModelRequest(<br>        parts=[<br>            SystemPromptPart(&quot;Keep asking questions until you get all the necessary specs to suggest a phone.&quot;)<br>        ],<br>    )<br>]</pre><p>Notice that messages is a list of ModelMessage<strong>,</strong> and the instructions are a SystemPrompt part. Pydantic AI gives us some helpful types like this, including a UserPromptPart and a ToolReturnPart. We don’t <em>have</em> to use these, but they are good for type safety and helpful LSP suggestions.</p><h4>Tools</h4><p>One to gather the specs, one to check the price of the phone. The run method is what we will call to <em>use</em> the tool.</p><pre>class Tool(BaseModel, ABC):<br>    @abstractmethod<br>    def run(self) -&gt; Any:<br>        raise NotImplementedError<br><br><br>class CheckPhonePrice(Tool):<br>    phone_name: str<br><br>    def run(self) -&gt; float | str:<br>        prices = {<br>            &quot;Apple iPhone 17&quot;: 799,<br>            &quot;Apple iPhone 17 Pro Max&quot;: 1099,<br>            &quot;Samsung Galaxy S25 Ultra&quot;: 892.5,<br>            &quot;Samsung Galaxy S25 FE&quot;: 650,<br>            &quot;Google Pixel 10 Pro&quot;: 799,<br>            &quot;Google Pixel 10 Pro Fold&quot;: 1499,<br>            &quot;Motorola Razr Ultra 2025&quot;: 999.99,<br>            &quot;OnePlus 15&quot;: 799,<br>            &quot;Xiaomi 14&quot;: 749,<br>            &quot;Nothing Phone 3a&quot;: 399,<br>        }<br>        return prices.get(self.phone_name, f&quot;Price not found. We know the prices for: {&#39;, &#39;.join(prices.keys())}&quot;)<br><br><br>class PhoneSpecs(Tool):<br>    &quot;&quot;&quot;Phone specifications extracted from the conversation&quot;&quot;&quot;<br><br>    ram: int<br>    storage: int<br>    camera_megapixels: int<br><br>    def run(self) -&gt; str:<br>        return &quot;Nice, we got the specs, now think of a suggestion, check the price, and give a nice reccomendation message. If you can&#39;t find it, wrap it up and provide all the necessary details and some useful search queries for the user to search the web on their own. You could also suggest some alternative based on the ones you know the price about.&quot;</pre><p>This is what our agent will get for the PhoneSpecs<strong> </strong>tool:</p><pre>{<br>  &quot;description&quot;: &quot;Phone specifications extracted from the conversation&quot;,<br>  &quot;properties&quot;: {<br>    &quot;ram&quot;: { &quot;title&quot;: &quot;Ram&quot;, &quot;type&quot;: &quot;integer&quot; },<br>    &quot;storage&quot;: { &quot;title&quot;: &quot;Storage&quot;, &quot;type&quot;: &quot;integer&quot; },<br>    &quot;camera_megapixels&quot;: { &quot;title&quot;: &quot;Camera Megapixels&quot;, &quot;type&quot;: &quot;integer&quot; }<br>  },<br>  &quot;required&quot;: [&quot;ram&quot;, &quot;storage&quot;, &quot;camera_megapixels&quot;],<br>  &quot;title&quot;: &quot;PhoneSpecs&quot;,<br>  &quot;type&quot;: &quot;object&quot;<br>}</pre><p>Notice how the PhoneSpecs<strong> </strong>tool returns a string that is basically a follow-up instruction, and the CheckPhonePrice tool returns the price or a helpful message. We can return <strong>whatever we want</strong> and send <strong>whatever we want</strong> back to the LLM. It’s our own loop with our own tools.</p><h4>The Loop</h4><p>Please read the comments.</p><pre># notice how we only have one tool for now<br># we want the CheckPhonePrice tool to be available only AFTER the phone specs are finalized<br><br>function_tools: dict[str, type[Tool]] = {PhoneSpecs.__name__.lower(): PhoneSpecs}<br>text_response = &quot;&quot;<br>while True:<br>    if text_response:<br>        user_prompt = input(f&quot;\n{text_response}\n&gt; &quot;)<br>        messages.append(ModelRequest.user_text_prompt(user_prompt=user_prompt))<br>        if user_prompt.lower() in [&quot;exit&quot;, &quot;quit&quot;, &quot;stop&quot;, &quot;q&quot;]:<br>            break<br>    model_response = model_request_sync(<br><br>        # &quot;gateway&quot; means we are using Pydantic AI gateway<br>        # we can use any model after the slash<br><br>        model=&quot;gateway/google-vertex:gemini-2.5-flash&quot;,<br>        messages=messages,<br><br>        # we can add google-specific settings by just adding &quot;google_&quot; at the start<br>        # this will not break if we use a gpt/claude model<br><br>        model_settings={&quot;google_thinking_config&quot;: {&quot;thinking_budget&quot;: 0}},  # ty: ignore[invalid-key, invalid-argument-type]<br>        model_request_parameters=ModelRequestParameters(<br>            function_tools=[tool_to_tool_def(model) for model in function_tools.values()],<br>            output_mode=&quot;tool&quot;,<br><br>            # we are forcing the model to select a tool<br>            # but we are also allowing it to return plain text so that it can chat with the user<br><br>            allow_text_output=True,<br>        ),<br>    )<br>    messages.append(model_response)<br>    res_tool = model_response.tool_calls[0] if model_response.tool_calls else None<br><br>    # if res_tool is None, the model returned a plain text response<br>    # add it to messages<br><br>    if not res_tool:<br>        text_response = model_response.text<br>        continue<br>    text_response = &quot;&quot;<br><br>    # now this is fun<br>    # if it returned a PhoneSpec tool use, it thinks that all the required specs are finalized<br><br>    if res_tool.tool_name == PhoneSpecs.__name__.lower():<br><br>        # make the CheckPhonePrice tool available<br><br>        function_tools[CheckPhonePrice.__name__.lower()] = CheckPhonePrice<br><br>    # either way, call the tool and add the result to messages<br><br>    messages.append(<br>        ModelRequest(<br>            parts=[tool_return_part(res_tool, function_tools[res_tool.tool_name](**res_tool.args).run())]<br>        )<br>    )</pre><h4>Run</h4><p>And there you have it. Now we might as well run our agent.</p><p>Our messages after the <strong>&gt;</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n9Vhrd_0Sy_chbI_koQLvw.png" /></figure><p>Notice how it asked me for the remaining specs after I only mentioned the camera megapixels.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tN1axAH1F2dRSEuxl3P1Pg.png" /></figure><p>I’m using Gemini 2.5 Flash, and it doesn’t even know about the latest phones, but it did suggest the ones that it got from CheckPhonePrice<strong>.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yWAkHgGKBfLV_b6LTBtFoA.png" /></figure><p>Nice!</p><h3>Observability?</h3><p>Glad you asked. Since we are using Pydantic AI’s features, we get <a href="https://pydantic.dev/logfire">Logfire</a> built in!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*02PO7jrQ4IrR-981YGgnwA.png" /></figure><h3>Exception Handling? Retries?</h3><p>Now let me ask you this: What would you do in a <em>“normal”</em> application? You would catch the exceptions, and you would handle them. Maybe you’d try again, maybe you’d try something else.</p><p>Exception handling is part of <strong>Engineering</strong>, not the new flashy <strong>AI Engineering</strong>. You own the loop; you can do whatever you want. Make it as brittle or as robust as you like. Go for it!</p><h3>Code</h3><p>You deserve it: <a href="https://gist.github.com/HamzaFarhan/fa232f7a4a57ecd296d7a425b7e68fdb">https://gist.github.com/HamzaFarhan/fa232f7a4a57ecd296d7a425b7e68fdb</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=55b9a83c3b05" width="1" height="1" alt=""><hr><p><a href="https://medium.com/dream-ai/create-your-own-ai-agent-without-an-ai-framework-55b9a83c3b05">Create Your Own AI Agent Without an AI Framework</a> was originally published in <a href="https://medium.com/dream-ai">Dream AI</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Smart Context Engineering with Pydantic AI: History Processing for Long-Running Tasks]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/dream-ai/smart-context-engineering-with-pydantic-ai-part-1-history-processing-for-long-running-tasks-f06926241503?source=rss----bb4a3200de4c---4"><img src="https://cdn-images-1.medium.com/max/2600/1*TBjTgVlBqExRT07U6I61GA.png" width="3044"></a></p><p class="medium-feed-snippet">As we know by now, an agent is only as good as the context it is given.</p><p class="medium-feed-link"><a href="https://medium.com/dream-ai/smart-context-engineering-with-pydantic-ai-part-1-history-processing-for-long-running-tasks-f06926241503?source=rss----bb4a3200de4c---4">Continue reading on Dream AI »</a></p></div>]]></description>
            <link>https://medium.com/dream-ai/smart-context-engineering-with-pydantic-ai-part-1-history-processing-for-long-running-tasks-f06926241503?source=rss----bb4a3200de4c---4</link>
            <guid isPermaLink="false">https://medium.com/p/f06926241503</guid>
            <category><![CDATA[llm-function-calling]]></category>
            <category><![CDATA[pydantic-ai]]></category>
            <category><![CDATA[ai-agent]]></category>
            <category><![CDATA[context-engineering]]></category>
            <category><![CDATA[large-language-models]]></category>
            <dc:creator><![CDATA[Hamza Farhan]]></dc:creator>
            <pubDate>Wed, 17 Sep 2025 15:27:21 GMT</pubDate>
            <atom:updated>2026-01-13T19:31:40.759Z</atom:updated>
        </item>
    </channel>
</rss>