SyncAI.news, a Varaisys broadcasting
How to Turn a Python Script Into an AI Agent
AA

Abid Ali Awan

· 1 min read

EngineeringKDnuggets

How to Turn a Python Script Into an AI Agent

You do not need to rewrite your Python applications to start using AI agents.

If your script already contains useful functions, you can expose those functions as tools and let an LLM decide when to call them, what arguments to provide, and how to use their outputs.

In this tutorial, we will take a simple website-monitoring script and turn it into an AI agent using the OpenAI Agents SDK.

Starting With a Normal Python Script

Before building an AI agent, let's start with a normal Python program.

Suppose we want to check whether a website is responding and measure how long the request takes:

from time import perf_counter

import requests


def check_website(url: str) -> str:
    start = perf_counter()

    try:
        response = requests.get(url, timeout=10)
        latency = perf_counter() - start

        return (
            f"{url}\n"
            f"Status: {response.status_code}\n"
            f"Response time: {latency:.2f}s"
        )

    except requests.RequestException as error:
        return f"{url}\nError: {error}"


print(check_website("https://www.python.org"))

Output:

https://www.python.org
Status: 200
Response time: 0.99s

The script does exactly what we programmed it to do: send an HTTP request, collect the status code, measure the response time, and return the result.

This is useful, but the workflow is completely fixed:

If we want to check five websites, compare their response times, or determine which one appears unhealthy, we need to write that logic ourselves.

This is where an AI agent changes the workflow.

Instead of encoding every decision in Python, we can expose check_website() as a tool and give an AI model a goal. The model can then decide when to call the tool, which URL to check, how many times to use it, and what to do with the results.

Step 1: Installing the Agents SDK

First, set up a Python project and install the packages we need to build and run the agent.

Create a new project:

mkdir website-agent
cd website-agent

uv init
uv add openai-agents requests

Original source

This story was published by KDnuggets and written by Abid Ali Awan. SyncAI.news shows a preview; the complete article is on the publisher's site.

Read the full story on kdnuggets.com

Similar News