<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Pankaj Telang</title>
    <description>The latest articles on DEV Community by Pankaj Telang (@ptelang).</description>
    <link>https://dev.to/ptelang</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%2F1403060%2Fbc6dcfb5-96fe-4ba4-b1da-bab8e9fb02bd.jpeg</url>
      <title>DEV Community: Pankaj Telang</title>
      <link>https://dev.to/ptelang</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ptelang"/>
    <language>en</language>
    <item>
      <title>A Mind Meld for the Modern Enterprise: Breaking Down Knowledge Silos with MCP</title>
      <dc:creator>Pankaj Telang</dc:creator>
      <pubDate>Mon, 04 Aug 2025 02:36:54 +0000</pubDate>
      <link>https://dev.to/stacklok/a-mind-meld-for-the-modern-enterprise-breaking-down-knowledge-silos-with-mcp-3enf</link>
      <guid>https://dev.to/stacklok/a-mind-meld-for-the-modern-enterprise-breaking-down-knowledge-silos-with-mcp-3enf</guid>
      <description>&lt;p&gt;Authors: &lt;a href="https://www.linkedin.com/in/aponcedeleonch/" rel="noopener noreferrer"&gt;Alejandro Ponce de León Chávez&lt;/a&gt;, &lt;a href="https://www.linkedin.com/in/nigel-brown-ba3362/" rel="noopener noreferrer"&gt;Nigel Brown&lt;/a&gt;, and &lt;a href="https://www.linkedin.com/in/pankaj-telang-0597483/" rel="noopener noreferrer"&gt;Pankaj Telang&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;AI: the final frontier. These are the voyages of the Enterprise. Our continuing mission: to explore strange new worlds; to seek out new applications and new technologies; to boldly go where not everyone has gone before!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bottom line up front:&lt;/strong&gt; We built a production-ready solution that makes enterprise Google Drive, GitHub, and Discord knowledge instantly available to AI agents using Model Context Protocol (MCP) servers and deployed using ToolHive's Kubernetes operator. Instead of hunting through documents for hours, your AI agents can now find and synthesize information from your organization's scattered knowledge in seconds.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Enterprise Knowledge Nebula
&lt;/h2&gt;

&lt;p&gt;Most enterprises struggle to chart a course through a galaxy of scattered knowledge. Essential data is often marooned across far-flung systems - Google Docs, internal wikis, Slack channels, Discord servers, and GitHub repositories - forming a nebula of information silos.&lt;/p&gt;

&lt;p&gt;Picture this familiar scenario: You're working on a critical project deadline and need to find the latest marketing assets, product roadmap, or expense policy buried somewhere in your company's Google Drive. You know it exists, but where? Sound familiar?&lt;/p&gt;

&lt;p&gt;This fragmentation leads to adverse business impact:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Inefficiency: Employees waste valuable time searching for, collating, and synthesizing information from multiple sources&lt;/li&gt;
&lt;li&gt;Information decay: Important knowledge remains inaccessible to employees who need it, leading to duplicated efforts and missed opportunities&lt;/li&gt;
&lt;li&gt;Reduced productivity: The difficulty in accessing relevant information hinders collaboration and decision-making
All the data you need is there, if only you had time to read it... or if someone could read it for you... or something...&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;We could use AI for that!&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why MCP Servers Are the Missing Piece
&lt;/h2&gt;

&lt;p&gt;Large language models excel at understanding and reasoning about information, but they're blind to your proprietary enterprise knowledge. That's where &lt;a href="https://modelcontextprotocol.io/introduction" rel="noopener noreferrer"&gt;Model Context Protocol (MCP)&lt;/a&gt; servers come in - they're the bridge that connects AI agents to your internal systems.&lt;/p&gt;

&lt;p&gt;For example, when you ask Claude about your company's expense policy, an MCP server can fetch the relevant document from Google Drive and provide that context to the AI model.&lt;/p&gt;

&lt;p&gt;The challenge? Deploying and managing MCP servers in enterprise environments requires solving for security, scalability, and reliability - exactly what &lt;strong&gt;ToolHive and Kubernetes&lt;/strong&gt; excel at.&lt;/p&gt;

&lt;h2&gt;
  
  
  Our Journey: From Concept to Production
&lt;/h2&gt;

&lt;p&gt;We started thinking about building a tool to connect our dots, but then discovered that one already exists: Onyx - an end-to-end open source solution for enterprise knowledge management.&lt;/p&gt;

&lt;p&gt;We decided to explore. We connected Onyx to multiple sources including Google Drive, GitHub repositories, and Discord channels, let it read and semantically index the text, and then unleashed AI agents on it. Here's how we built our enterprise-ready knowledge retrieval system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Architecture&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn89wgmbjowos02gll2q1.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn89wgmbjowos02gll2q1.jpg" alt=" " width="800" height="201"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our solution combines four key technologies:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Onyx:&lt;/strong&gt; Extracts and indexes content from multiple sources, including Google Drive, GitHub repositories, Discord channels, and others, using vector embeddings&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ToolHive Kubernetes Operator:&lt;/strong&gt; Our ready-to-use operator that deploys and manages MCP servers securely at scale&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Knowledge MCP server:&lt;/strong&gt; Acts as a secure bridge between AI clients and the Onyx knowledge base&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LibreChat:&lt;/strong&gt; A flexible, open-source UI for AI interactions that integrates seamlessly with MCP servers&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Implementation Journey
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Deploy Onyx&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We started with Onyx's Kubernetes deployment. Key points from our experience:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We copied images to our local cloud repository for better control&lt;/li&gt;
&lt;li&gt;The default configuration gives you a full-size cluster — you can scale this back for smaller deployments&lt;/li&gt;
&lt;li&gt;GPUs would help performance but aren't strictly necessary for getting started&lt;/li&gt;
&lt;li&gt;Pay careful attention to authentication setup (more on this below)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Onyx comes with a built-in chat interface that might be all you need. However, as an enterprise that needs to integrate with other agents, apps, and domains while ensuring proper access control, we wanted a different approach.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Create the MCP Server&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;MCP proved to be the ideal marshalling point. We created a custom MCP server for Onyx.&lt;/p&gt;

&lt;p&gt;The server is fairly simple - essentially a passthrough for calls to Onyx with some tailored prompts and authentication handling.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Deploy with ToolHive&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is where ToolHive's Kubernetes operator shines. Instead of manually configuring containers and networking, you define your MCP server as a Kubernetes custom resource:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: toolhive.stacklok.dev/v1alpha1
kind: MCPServer
metadata:
  name: knowledge-mcp-server
  namespace: toolhive-system
spec:
  image: xxxx.com/knowledge-mcp-server:latest
  transport: streamable-http
  port: 8000
  targetPort: 8000
  env:
    - name: ONYX_URL
      value: "http://onyx-api-service.onyx:8080"
  permissionProfile:
    type: builtin
    name: network
  resources:
    limits:
      cpu: "100m"
      memory: "128Mi"
    requests:
      cpu: "50m"
      memory: "64Mi"
  oidcConfig:
    type: inline
    inline:
      issuer: &amp;lt;IDP URL&amp;gt;
      audience: &amp;lt;AUDIENCE FOR TOKEN&amp;gt;
      jwksUrl: &amp;lt;URL TO FETCH JWKS&amp;gt;
      jwksAllowPrivateIP: false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ToolHive gives us a layer of control and authentication over the MCP server. Connections are protected by OAuth, so we know exactly who is calling in.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Set Up LibreChat&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To make this available to our team on a permanent basis, we deployed LibreChat - a fantastic, flexible open-source AI chat interface. This gives us a production-ready UI that integrates seamlessly with our MCP server.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Note this &lt;a href="https://github.com/danny-avila/LibreChat/discussions/8574" rel="noopener noreferrer"&gt;slight issue&lt;/a&gt; if you try this at home&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  ToolHive: Making MCP Servers Enterprise-Ready
&lt;/h2&gt;

&lt;p&gt;The game-changer in our architecture is ToolHive's Kubernetes operator. Here's why it matters:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One-command deployment:&lt;/strong&gt; Apply the YAML configuration with a simple kubectl command and ToolHive handles pod creation, service discovery, security policies, and monitoring automatically:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="s"&gt;kubectl apply -f toolhive-deployment.yaml&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Security by default:&lt;/strong&gt; Every MCP server runs in an isolated container with minimal permissions. The operator automatically creates:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dedicated ServiceAccount per MCP with least-privilege access&lt;/li&gt;
&lt;li&gt;Network policies that restrict communication&lt;/li&gt;
&lt;li&gt;Secure secret management for OAuth credentials&lt;/li&gt;
&lt;li&gt;RBAC configurations for multi-tenant deployments&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Enterprise scale:&lt;/strong&gt; The operator supports multi-namespace deployments, allowing different teams to manage their own MCP servers while maintaining security boundaries.&lt;/p&gt;

&lt;h2&gt;
  
  
  Real-World Results: From Hours to Seconds
&lt;/h2&gt;

&lt;p&gt;The transformation is immediate. Instead of employees spending hours hunting through Google Drive folders, GitHub issues, or Discord messages, they can ask natural language questions and get answers with source citations in seconds.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example interactions:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Although not essential it helps to create a custom agent in LibreChat. This saves things like the model, prompt and tools to use for later.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb7grwqwrath9g60m59ua.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb7grwqwrath9g60m59ua.gif" alt=" " width="512" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then we can go through and ask about some of the enterprise data we’ve given it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm44vbyfvgwt2c5b6703v.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm44vbyfvgwt2c5b6703v.gif" alt=" " width="512" height="364"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It does a good job, joining the dots for us.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lessons Learned: The Good, The Bad, and The AI
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Is it good?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Well, yes. And no. (It is AI after all!)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The impressive parts:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Simple retrieval across massive document collections&lt;/li&gt;
&lt;li&gt;Excellent at synthesizing information from multiple sources&lt;/li&gt;
&lt;li&gt;Natural language queries that actually work&lt;/li&gt;
&lt;li&gt;Source citations that let you verify information&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The challenges:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sometimes it struggles with dates and can make things up&lt;/li&gt;
&lt;li&gt;You need to be very careful with permissions&lt;/li&gt;
&lt;li&gt;Authorization policies between different tools need careful consideration&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Security Considerations: Boldly Safely Going&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Take care with permissions&lt;/strong&gt; - If you follow default instructions, you might expose all your documents to everyone. This probably isn't what you want.&lt;/p&gt;

&lt;p&gt;The default approach suggested by Onyx requires domain-wide delegation access for the full Google Drive Workspace. It is a big, scary ask. Onyx will impersonate each user in the domain and fetch their documents. Had we granted that, it would have allowed access to all documents in our domain, including sensitive documents with PII. Furthermore, indexed document fragments will end up in the vector database which may or may not be secured to high enough standards. The authorization boundary is also fragmented in this process.&lt;/p&gt;

&lt;p&gt;While it would be possible to take a less restrictive approach with broader service account permissions, we prioritized security and explicit document access setup. We used a normal account with OAuth access and standard permission. For now, the account has the same access as a typical employee to our internally public documents, which represent the knowledge that teams want to be discoverable and accessible and have explicitly shared for broad internal access. This gives us confidence that every piece of information in our vector database belongs there intentionally, and that access to the database doesn’t create an escalation of privilege for users of the database.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Still on our roadmap:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pass the OIDC token from LibreChat through the MCP to Onyx for proper authorization&lt;/li&gt;
&lt;li&gt;Make email address verification spoof-proof&lt;/li&gt;
&lt;li&gt;Implement fine-grained access controls&lt;/li&gt;
&lt;li&gt;Open-source the Knowledge MCP server&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Getting Started: Deploy Your Own Knowledge MCP Server&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Want to try this yourself? Here's the path we recommend:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Set up the ToolHive Operator&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Install the operator CRDs&lt;/span&gt;
helm upgrade &lt;span class="nt"&gt;-i&lt;/span&gt; toolhive-operator-crds &lt;span class="se"&gt;\&lt;/span&gt;
  oci://ghcr.io/stacklok/toolhive/toolhive-operator-crds

&lt;span class="c"&gt;# Deploy the operator  &lt;/span&gt;
helm upgrade &lt;span class="nt"&gt;-i&lt;/span&gt; toolhive-operator &lt;span class="se"&gt;\&lt;/span&gt;
  oci://ghcr.io/stacklok/toolhive/toolhive-operator &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;-n&lt;/span&gt; toolhive-system &lt;span class="nt"&gt;--create-namespace&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Deploy Onyx:&lt;/strong&gt; Follow the &lt;a href="https://docs.onyx.app/" rel="noopener noreferrer"&gt;Kubernetes deployment guide&lt;/a&gt; and configure your Google Drive connectors.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Create your MCP server:&lt;/strong&gt; Use your favorite programming language. We went with the &lt;a href="https://github.com/modelcontextprotocol/python-sdk" rel="noopener noreferrer"&gt;Python SDK&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Note:&lt;/strong&gt; We implemented a search API in Onyx which is called by the MCP server.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Deploy with ToolHive:&lt;/strong&gt; Apply your MCPServer resource and watch ToolHive handle the rest.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Connect LibreChat:&lt;/strong&gt; &lt;a href="https://www.librechat.ai/docs" rel="noopener noreferrer"&gt;Deploy LibreChat&lt;/a&gt; and configure it to use your new MCP server.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Try ToolHive Yourself
&lt;/h2&gt;

&lt;p&gt;Ready to break down your knowledge silos?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Explore ToolHive:&lt;/strong&gt; Check out the &lt;a href="https://docs.stacklok.com/toolhive/tutorials/quickstart-k8s" rel="noopener noreferrer"&gt;ToolHive documentation&lt;/a&gt; and try the Kubernetes operator quickstart&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Join the community:&lt;/strong&gt; Connect with other MCP developers in our &lt;a href="https://discord.gg/stacklok" rel="noopener noreferrer"&gt;Discord&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The tools exist today to make enterprise knowledge universally accessible to AI agents. The question isn't whether to build this capability - it's how quickly you can deploy it.&lt;/p&gt;

&lt;p&gt;You might just Klingon to it! (Ach!, this metaphor - she cannae take any more, captain!)&lt;/p&gt;

&lt;p&gt;&lt;em&gt;What knowledge silos is your organization struggling with? Let us know in the comments how you're thinking about connecting AI agents to your internal systems.&lt;/em&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Exploring SmolAgents</title>
      <dc:creator>Pankaj Telang</dc:creator>
      <pubDate>Fri, 02 May 2025 19:30:24 +0000</pubDate>
      <link>https://dev.to/stacklok/exploring-smolagents-50ka</link>
      <guid>https://dev.to/stacklok/exploring-smolagents-50ka</guid>
      <description>&lt;h2&gt;
  
  
  Part 2 of &lt;em&gt;Demystifying AI Agents&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;In &lt;a href="https://dev.to/stacklok/understanding-ai-agents-thought-action-and-observation-in-practice-509i"&gt;Part 1&lt;/a&gt; of this series, I covered the conceptual foundations of AI agents. In this part, I introduce &lt;strong&gt;SmolAgents&lt;/strong&gt;, a lightweight agent framework, and demonstrate its use by implementing the stock trading scenario from Part 1.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;SmolAgents Framework&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;SmolAgents&lt;/strong&gt; is a lightweight AI agent framework developed by Hugging Face. It is designed to help developers build and deploy agents with minimal code.&lt;/p&gt;

&lt;p&gt;To &lt;strong&gt;observe&lt;/strong&gt; or &lt;strong&gt;act&lt;/strong&gt; on the &lt;strong&gt;environment&lt;/strong&gt;, the LLM (large language model) powering an agent can request tool calls in two ways:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;By generating JSON, which the agent then parses, or
&lt;/li&gt;
&lt;li&gt;By generating &lt;strong&gt;executable Python code&lt;/strong&gt;, which the agent executes using a python interpreter.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://arxiv.org/abs/2402.01030" rel="noopener noreferrer"&gt;Research&lt;/a&gt; shows that the second method is more flexible and modular. SmolAgents supports both approaches; in this blog, I’ll focus on an implementation that uses executable Python code.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Stock Trading Agent&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Let’s briefly recap the stock trading agent introduced in Part 1. This agent performs user-requested tasks using:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An &lt;strong&gt;LLM&lt;/strong&gt; as the planning and reasoning engine
&lt;/li&gt;
&lt;li&gt;A set of &lt;strong&gt;tools&lt;/strong&gt; for:

&lt;ul&gt;
&lt;li&gt;Looking up stock tickers
&lt;/li&gt;
&lt;li&gt;Fetching current holdings
&lt;/li&gt;
&lt;li&gt;Getting stock prices
&lt;/li&gt;
&lt;li&gt;Selling stocks&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;For example, a user might issue this instruction:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Check the stock price of Nvidia. If it is above $150, sell 80% of the stock that I hold.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://dev.to/stacklok/understanding-ai-agents-thought-action-and-observation-in-practice-509i"&gt;Part 1&lt;/a&gt; walked through an agent execution for the above user instruction in terms of the think-act-observe loop. Now, let's see how to implement it using SmolAgents.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;SmolAgent Implementation of the Stock Trading Agent&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;We begin by importing the required components from the &lt;code&gt;smolagents&lt;/code&gt; library:&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;smolagents&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;CodeAgent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;OpenAIServerModel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tool&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we implement the four tools needed by our stock trading agent. These tools use hardcoded values (for demonstration purposes); in a real-world scenario, they would interface with external APIs.&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="nd"&gt;@tool&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;lookup_ticker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&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;
    Looks up the stock ticker symbol for a given company name.
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;nvidia&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;NVDA&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;else&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;Unknown&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_my_stock_holdings&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;dict&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
    Returns the user&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;s current stock holdings.
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;NVDA&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;MSFT&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;TSLA&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;20&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_stock_price&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ticker&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;float&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
    Returns the current price of the specified stock ticker.
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;ticker&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;NVDA&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="mf"&gt;293.46&lt;/span&gt;
    &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;ticker&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;MSFT&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="mf"&gt;225.23&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&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;Unknown stock symbol: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;ticker&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="nd"&gt;@tool&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sell_stock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ticker&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;quantity&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;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;
    Sells a specified quantity of a given stock.
    &lt;/span&gt;&lt;span class="sh"&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="s"&gt;Sold &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;quantity&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; shares of &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;ticker&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="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Success&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we create the stock trading agent using the CodeAgent class:&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;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;OpenAIServerModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;model_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;gpt-4o&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;api_key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;&amp;lt;OPENAI_API_KEY&amp;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;stock_agent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CodeAgent&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;lookup_ticker&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;get_my_stock_holdings&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;get_stock_price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sell_stock&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="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;planning_interval&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that we're using CodeAgent, which relies on executable Python code for tool invocation.&lt;/p&gt;

&lt;p&gt;To run the agent:&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;agent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&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 the stock price of Nvidia. If it is above $150, sell 80 percent of the stock that I hold.&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;
  
  
  &lt;strong&gt;Results&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Here’s what happens when the agent executes. Observe how these align with each of the think-act-observe iterations I covered in &lt;a href="https://dev.to/stacklok/understanding-ai-agents-thought-action-and-observation-in-practice-509i"&gt;Part 1&lt;/a&gt;. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The agent analyzes the instruction and creates a plan:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fse63ab7xo7z9mve8nuww.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fse63ab7xo7z9mve8nuww.png" alt=" " width="800" height="547"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It calls the lookup_ticker tool to find Nvidia’s stock symbol:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd7ul43ieva0wtvv37dyj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd7ul43ieva0wtvv37dyj.png" alt=" " width="800" height="102"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It calls get_stock_price to check Nvidia’s current price:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1gjj6b7cleirqtx8k0f7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1gjj6b7cleirqtx8k0f7.png" alt=" " width="800" height="103"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It retrieves the user’s stock holdings via get_my_stock_holdings:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F47kdw5ixd9vaa4951rjx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F47kdw5ixd9vaa4951rjx.png" alt=" " width="800" height="101"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Since Nvidia's stock price is above $150, it sells 80% of the holdings:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc08ds2a7asrao6x4u5ri.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc08ds2a7asrao6x4u5ri.png" alt=" " width="800" height="239"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Finally, it confirms the stock sale to the user:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7dq35et1ynx36f2ee5jd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7dq35et1ynx36f2ee5jd.png" alt=" " width="800" height="55"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A few things to note in the above executions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;All tool calls are implemented using &lt;strong&gt;Python&lt;/strong&gt;, providing flexibility and reliability.&lt;/li&gt;
&lt;li&gt;Since the calculations are handled in &lt;strong&gt;Python&lt;/strong&gt;, their &lt;strong&gt;accuracy&lt;/strong&gt; is ensured. Unlike LLMs, which can sometimes make errors in mathematical calculations, Python executes exact calculations using its built-in logic and libraries.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In this second part of the Demystifying AI Agents series, we demonstrated how to implement a simple stock trading agent using the SmolAgents framework. This hands-on example showcases how lightweight frameworks like SmolAgents can help developers quickly build and experiment with autonomous agents using LLMs. In the next part, we’ll explore how CodeGate can be used in conjunction with SmolAgents for security and privacy. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;References:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Artificial Intelligence: A Modern Approach, Russell &amp;amp; Norvig, Chapter 3 &lt;a href="https://people.eecs.berkeley.edu/%7Erussell/aima1e/chapter02.pdf" rel="noopener noreferrer"&gt;https://people.eecs.berkeley.edu/~russell/aima1e/chapter02.pdf&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Smolagents: &lt;a href="https://huggingface.co/blog/smolagents" rel="noopener noreferrer"&gt;https://huggingface.co/blog/smolagents&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Codegate: &lt;a href="https://codegate.ai/" rel="noopener noreferrer"&gt;https://codegate.ai/&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

</description>
    </item>
    <item>
      <title>Understanding AI Agents</title>
      <dc:creator>Pankaj Telang</dc:creator>
      <pubDate>Tue, 04 Mar 2025 19:15:57 +0000</pubDate>
      <link>https://dev.to/stacklok/understanding-ai-agents-thought-action-and-observation-in-practice-509i</link>
      <guid>https://dev.to/stacklok/understanding-ai-agents-thought-action-and-observation-in-practice-509i</guid>
      <description>&lt;h2&gt;
  
  
  Part 1 of Demystifying AI agents
&lt;/h2&gt;

&lt;p&gt;If you're a developer or engineer navigating the rapidly evolving landscape of AI, you're in the right place. I’ve spent a while exploring how AI can enhance software development, security, and automation. My goal with this blog series is to demystify AI agents—what they are, how they work, and how you can build secure and reliable ones.&lt;/p&gt;

&lt;p&gt;This series will break down key concepts around AI agents, walk through real-world use cases, and provide practical guidance on implementing them effectively. Whether you're experimenting with autonomous systems or looking to integrate agents into your workflow, I hope this blog will help you understand not just &lt;strong&gt;how&lt;/strong&gt; agents function but also &lt;strong&gt;why&lt;/strong&gt; they behave the way they do.&lt;/p&gt;

&lt;h2&gt;
  
  
  What You’ll Learn in This Series
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;What is an AI agent? A foundational look at agents, the thought-act-observe loop, and how they interact with tools.&lt;/li&gt;
&lt;li&gt;Exploring SmolAgents – a lightweight framework for building AI agents.&lt;/li&gt;
&lt;li&gt;Securing AI agents – how CodeGate can protect AI-driven workflows.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;What is an AI agent?&lt;/strong&gt;&lt;br&gt;
An &lt;strong&gt;agent&lt;/strong&gt; is an entity that can &lt;strong&gt;observe&lt;/strong&gt; and &lt;strong&gt;act&lt;/strong&gt; on the &lt;strong&gt;environment&lt;/strong&gt; in which it is situated. Based on the user provided goal (i.e. instructions), the agent first constructs a plan (i.e. a sequence of steps), to achieve the goal. Then, the agent executes the plan, optionally calling a pre-defined set of tools. &lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;tool&lt;/strong&gt; is an arbitrary function in a programming language with a well defined interface. For example, the tool might be a function that performs some calculation, query a database, or call an API. &lt;/p&gt;

&lt;p&gt;Conceptually, the agent executes several iterations of a loop consisting of: thinking, acting and observing. This is also referred to as the &lt;strong&gt;think-act-observe&lt;/strong&gt; loop.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Think&lt;/strong&gt;: Given a user instruction, the agent thinks and constructs a plan to satisfy the user’s request. As the agent executes the plan, the agent reasons with the current state to decide which action to perform next.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Act&lt;/strong&gt;: The agent executes the action it decided from thinking by calling the available tools. A tool might read or update some information or it might carry out a physical action in the real world. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Observe&lt;/strong&gt;: The agent observes the results from its action. These results are the output generated by the tool used for the action. The agent uses these results to further think about its next action.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Fig 1&lt;/strong&gt; illustrates a general schema of an AI agent.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhnxxej23fid7qb9bdqfl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhnxxej23fid7qb9bdqfl.png" alt="Agent Schema" width="800" height="510"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As a naive example, a stock trading agent can observe the price of a specific stock in a stock market and perform the action of either purchasing or selling that stock based on the user instructions. &lt;/p&gt;

&lt;p&gt;In the context of &lt;strong&gt;LLMs&lt;/strong&gt;, an agent is a program which employs a &lt;strong&gt;LLM as a planning and reasoning engine&lt;/strong&gt; to achieve a user specified goal. For observing and acting, the agent employs the available tools. However, the decision to use a tool is made by the &lt;strong&gt;LLM&lt;/strong&gt;. I will clarify this notion of an agent using the example of stock trading.&lt;/p&gt;

&lt;p&gt;Suppose the stock trading agent from the above example has access to these tools:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Lookup the stock ticker of a company
lookup_ticker(company_name: str)

# Lookup my stock holdings
get_my_stock_holdings()

# Get the price of a given stock ticker
get_stock_price(stock_ticker: str)

# Sell the stock of given ticker and given quantity
sell_stock(stock_ticker: str, quantity: int)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, assume a user provides these instructions to the agent: &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Check the stock price of Nvidia. If it is above $150, sell 80% of the stock that I hold.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;When the agent starts execution, it employs the LLM to construct a plan, that is, a sequence of steps. The LLMs reasoning process might look like below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;I need to create a plan for this problem. First, I need to check the price of Nvidia stock. I have access to the get_stock_price tool. But the tool needs a stock ticker. I need to first look up the ticker symbol of Nvidia. I will use the lookup_ticker tool. 
Step 1: Call lookup_ticker to get ticker for Nvidia.

Now I can look up Nvidia’s stock price using the get_stock_price tool. 
Step 2: Call get_stock_price to get the price using ticker. 

If the price is above $150, I need to sell 80% of the stock. I need to first get the total number of shares. I will use the get_my_stock_holdings tool. 
Step 3: Call get_mystock_holdings() to get the number of Nvidia stock.

I need to calculate 80% of the Nvidia shares. I will use the calculator tool. 
Step 4: Call calculate to get 80% of the shares.

I need to sell 80% of the shares. I can use the sell_stock tool.
Step 5: Call sell_stock to sell 80% of the shares.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At the end of the reasoning process, the LLM has generated a plan consisting of Steps 1–5.&lt;/p&gt;

&lt;p&gt;Now, the agent can execute this plan by repeatedly running the think-act-observe loop. Here is how the agent execution might look like. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&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;Think: I now have a plan, and I need to execute Step 1.
Act: Call lookup_ticker to get ticker for Nvidia.
Observe: The tool returns the stock ticker as: NVDA.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2&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;Think: Now I have the ticker symbol NVDA of Nvidia. I can execute Step 2.
Act: Call get_stock_price to get the price using ticker. 
Observe: The tool returns the stock price as: $160.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2&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;Think: Since $160 is above $150, I need to sell 80% of the shares. I need to execute Step 3. 
Act:  Call get_mystock_holdings().
Observe: The tool returns the stock holding: {“nvidia”: 100, “msft”: 50, “tsla”: 10}.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 4&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;Think: I know that the user has 100 units of Nvidia. I need to execute Step 4.
Act: Call calculate to get 80% of the shares.
Observe: The tool returns: 20.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 5&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;Think: I know I need to sell 20 shares. I need to execute Step 5.
Act: Call sell_stock to sell 80% of the shares.
Observe: The tool returns success. 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Done&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;Think: User’s request is successfully completed. I am done.
Act: Indicate completion of user’s request.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;: This blog introduces the notion of an AI agent. It presents the think-act-observe loop, which is central to how an agent executes. Through a simple stock scenario, I outline the planning and execution steps of an agent. I hope you found this blog useful. In the next blog, I will review smolagents, a recently introduced framework from Huggingface for developing such agents. I will also describe how smolagents can be secured using Codegate, a recently introduced framework for security AI agents and LLMs. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;References:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Artificial Intelligence: A Modern Approach, Russell &amp;amp; Norvig, Chapter 3 &lt;a href="https://people.eecs.berkeley.edu/%7Erussell/aima1e/chapter02.pdf" rel="noopener noreferrer"&gt;https://people.eecs.berkeley.edu/~russell/aima1e/chapter02.pdf&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Smolagents: &lt;a href="https://huggingface.co/blog/smolagents" rel="noopener noreferrer"&gt;https://huggingface.co/blog/smolagents&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Codegate: &lt;a href="https://codegate.ai/" rel="noopener noreferrer"&gt;https://codegate.ai/&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

</description>
    </item>
  </channel>
</rss>
