Agentic AI Python Code Example: Build Smarter Autonomous Systems

Agentic AI, defined by their autonomy and goal-directed behavior, is a fascinating subclass of artificial intelligence that's transforming industries worldwide. Python, with its simplicity and robust libraries, is often the go-to language for developing and working with these intelligent agents. Let's delve into the world of agentic AI using Python, exploring key concepts, relevant libraries, and practical code examples.

Generative AI with Python: Master Pretrained LLMs, Vector DBs & Retrieval-Augmented Generation
Generative AI with Python: Master Pretrained LLMs, Vector DBs & Retrieval-Augmented Generation

Agentic AI, as opposed to reactive or learning AI, possesses the ability to adapt and make decisions based on its environment and goals. In Python, we can exemplify this through simple yet illustrative agent models. Let's start with a basic agentic AI example using the Agent Super-Simple (ASS) library.

code to go through all the files using python automation
code to go through all the files using python automation

Agent Super-Simple (ASS) Library

The ASS library provides a simple and easy-to-use framework for creating agentic AI in Python. It allows developers to define agents with states, perceptions, and actions, enabling the creation of agent-based simulations and games.

Python Cheat Sheet for Beginners
Python Cheat Sheet for Beginners

Here's a simple example of an agentic AI using the ASS library, where our agent learns to navigate a grid world:

Creating a Simple Learning Agent

an image of a computer screen with the text'generation ascii art in python '
an image of a computer screen with the text'generation ascii art in python '

First, install the ASS library using `pip install ass`. Next, import the necessary modules and define our agent.

from ass.agents import QLearningAgent from ass.environments import GridWorld agent = QLearningAgent(actions=['up', 'down', 'left', 'right']) environ = GridWorld([[-1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1]])

The agent learns the best action ( bronch move towards the goal) to take in each state through Q-learning, a model-free reinforcement learning algorithm.

Training and Evaluating the Agent

an image of a computer screen with many lines and numbers on the screen, all in different colors
an image of a computer screen with many lines and numbers on the screen, all in different colors

Now, let's train and evaluate our agent. We'll run a number of episodes for training and then test its performance.

for _ in range(1000): # Training state = environ.reset() while not environ.is_terminal(state): action = agentembler(state) state = environ.execute_action(action) episodes = 100 # Testing total_reward = 0 for _ in range(episodes): state = environ.reset() total_reward += environ.run_episode(lambda s: agent.get_best_action(s)) print("Average reward per episode:", total_reward / episodes)

This simple example demonstrates how to create an agentic AI using Python and the ASS library. The agent learns to navigate the environment, showcasing agentic AI's goal-directed behavior.

Utilizing Gym library for Building Agents

Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables
Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables

The Gym library provides a standardized platform for developing and testing reinforcement learning algorithms. It facilitates the creation of agentic AI by providing a unified interface for environments and tools to monitor and control simulations.

Let's explore how to create anagentic AI to play CartPole using the Gym library and Stable Baselines3, a Python package implementing various reinforcement learning algorithms.

Brand Marketing Strategy, Artificial Neural Network, Digital Data, Python Programming, Big Data, Brand Marketing, Data Science, Python, Programming
Brand Marketing Strategy, Artificial Neural Network, Digital Data, Python Programming, Big Data, Brand Marketing, Data Science, Python, Programming
the diagram shows how to use python commands for web pages and other contents, including data
the diagram shows how to use python commands for web pages and other contents, including data
Python Libraries for AI and Machine Learning (Cheat Sheet)
Python Libraries for AI and Machine Learning (Cheat Sheet)
Easy IP-adress scanner with Python! ☕
Easy IP-adress scanner with Python! ☕
python aesthetic
python aesthetic
agentic ai python code example
agentic ai python code example
a poster showing the different types of people
a poster showing the different types of people
Emojis in python
Emojis in python
All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)
🐍 Python Operators Cheat Sheet ✨ Learn the symbols that make Python powerful! 💻
🐍 Python Operators Cheat Sheet ✨ Learn the symbols that make Python powerful! 💻
Python If-Else Cheat Sheet
Python If-Else Cheat Sheet
Building truly modular Agentic AI systems? | Brij kishore Pandey posted on the topic | LinkedIn
Building truly modular Agentic AI systems? | Brij kishore Pandey posted on the topic | LinkedIn
Building AI Agents with Python
Building AI Agents with Python
Python 404
Python 404
Python Cheat Sheet 🚀 | Beginner to Advanced | Save This for Coding & Interviews
Python Cheat Sheet 🚀 | Beginner to Advanced | Save This for Coding & Interviews
Save AI Face Locally in Python Python, Coding
Save AI Face Locally in Python Python, Coding
an image of a computer screen with cats on it and the words, python list method
an image of a computer screen with cats on it and the words, python list method
Artificial Intelligence With Python
Artificial Intelligence With Python

Installing Required Libraries

To work with Gym and Stable Baselines3, first install them using `pip install gym[atari] stable-baselines3[extra]`.

Creating an RL Agent to Play CartPole

Now we'll create an agentic AI to play CartPole using PPO, a policy-based method developed by Schulman et al. that uses a clipping trick to prevent large policy updates.

from stable_baselines3 import PPO from stable_baselines3.common.policies import MlpPolicy from stable_baselines3.common.vec_env import DummyVecEnv env = DummyVecEnv([lambda: env]) # The algorithms require a vectorized environment to run model = PPO(MlpPolicy, env, n_steps=2048) model.learn(total_timesteps=25000)

After training, you can evaluate and test the agent's performance.

Embracing agentic AI in your projects can lead to innovative solutions,automated processes, and enhanced user experiences. Python, with its extensive library support, offers a perfect playground for experimenting and building these smart agents.

Related Articles

How To Write Code In Python Example Sample Python Certification Questions Sum Of Numbers Python While Loop Python Add Two Numbers Leetcode How To Add Two Numbers Together In Python Random Sample Gamma Distribution Python Python Simple Json Example Simple Python Game Code Snake Cbse Ai Code 417 Sample Paper Simple Python Game Code Github