<?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: BUSINESS24.AI</title>
    <description>The latest articles on DEV Community by BUSINESS24.AI (@business24ai).</description>
    <link>https://dev.to/business24ai</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%2Forganization%2Fprofile_image%2F10110%2F16ff6ab9-62c3-4ef6-a25f-18bb3414dc87.png</url>
      <title>DEV Community: BUSINESS24.AI</title>
      <link>https://dev.to/business24ai</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/business24ai"/>
    <language>en</language>
    <item>
      <title>Create an AI Agent with PydanticAI in Minutes</title>
      <dc:creator>Kiu</dc:creator>
      <pubDate>Sat, 18 Jan 2025 21:41:54 +0000</pubDate>
      <link>https://dev.to/business24ai/create-an-ai-agent-with-pydanticai-in-minutes-3k07</link>
      <guid>https://dev.to/business24ai/create-an-ai-agent-with-pydanticai-in-minutes-3k07</guid>
      <description>&lt;p&gt;Creating an AI agent might sound like a daunting task, but with modern tools like Pydantic and OpenAI, it's surprisingly straightforward. In this guide, we’ll walk through building a simple AI agent step by step.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/ptsWI7YsKN0"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Set Up Your Environment&lt;/strong&gt;&lt;br&gt;
To begin, we’ll create a virtual environment and install the required library: pydantic-ai. A virtual environment ensures your project dependencies are isolated and manageable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python -m venv venv  
source venv/bin/activate  # Use `venv\Scripts\activate` on Windows
pip install pydantic-ai
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2: Configure Your OpenAI API Key&lt;/strong&gt;&lt;br&gt;
Before interacting with OpenAI’s models, you’ll need an API key. Set this as an environment variable to keep it secure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;OPENAI_API_KEY="your_openai_api_key"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3: Write the Script&lt;/strong&gt;&lt;br&gt;
Now, let’s create the main script for our AI agent. The agent will use a system prompt and answer user questions by leveraging OpenAI’s API.&lt;/p&gt;

&lt;p&gt;Import Libraries&lt;br&gt;
We’ll begin by importing the necessary modules:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIModel
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Define the Model and Agent&lt;br&gt;
Next, define the model and create an agent instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# define the model
model = OpenAIModel("gpt-4o")

# define the agent
agent = Agent(
    model=model,
    system_prompt="Be concise, reply with one sentence.",
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, we use the run_sync method to send a query to the agent and print the response:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# run the agent
result = agent.run_sync("What does AGI mean?")

# print the result
print("\n=== Data ===")
print(result.data)

# print the Usage
print("\n=== usage ===")
print(result.usage())

# print the Messages
print("\n=== messages ===")
print(result.all_messages())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 4: Execute the Script&lt;br&gt;
Run the script to see the agent in action:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python your_script_name.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When executed, the agent will communicate with OpenAI’s API, process the query, and return a response. For example, if you ask, “What does AGI mean?” the agent might reply:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;=== Data ===
AGI stands for Artificial General Intelligence, which refers to a machine's ability to understand, learn, and apply intelligence across any task like a human.

=== usage ===
Usage(requests=1, request_tokens=25, response_tokens=31, total_tokens=56, details={'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0, 'cached_tokens': 0})

=== messages ===
[ModelRequest(parts=[SystemPromptPart(content='Be concise, reply with one sentence.', dynamic_ref=None, part_kind='system-prompt'), UserPromptPart(content='What does AGI mean?', timestamp=datetime.datetime(2025, 1, 18, 19, 17, 51, 503464, tzinfo=datetime.timezone.utc), part_kind='user-prompt')], kind='request'), ModelResponse(parts=[TextPart(content="AGI stands for Artificial General Intelligence, which refers to a machine's ability to understand, learn, and apply intelligence across any task like a human.", part_kind='text')], timestamp=datetime.datetime(2025, 1, 18, 19, 17, 52, tzinfo=datetime.timezone.utc), kind='response')]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
In just a few lines of code, we built a functional AI agent using Pydantic and OpenAI. This setup provides a robust foundation for building more complex AI-driven applications. Whether you’re answering questions, summarizing text, or performing advanced data analysis, this simple architecture can adapt to a variety of use cases.&lt;/p&gt;

&lt;p&gt;Let us know what you’ll create with your new AI agent!&lt;/p&gt;

&lt;p&gt;Links:&lt;br&gt;
&lt;a href="https://www.skool.com/business24ai" rel="noopener noreferrer"&gt;AI Agents Community on Skool&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.youtube.com/shorts/ptsWI7YsKN0" rel="noopener noreferrer"&gt;YouTube&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/business24ai/pydantic-ai-setup-short" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/p&gt;

</description>
      <category>pydanticai</category>
      <category>python</category>
      <category>openai</category>
      <category>ai</category>
    </item>
  </channel>
</rss>
