Integrating Agentic AI with Existing Machine Learning Pipelines

Learn how to combine a scikit-learn churn prediction pipeline with an LLM-powered agentic AI system into a single autonomous Python workflow.

Integrating Agentic AI with Existing Machine Learning Pipelines

In this article, you will learn how to combine a classical machine learning pipeline with an agentic AI system to build a hybrid, autonomous customer retention workflow.

Topics we will cover include:

  • How to generate a synthetic dataset and train a random forest classifier for customer churn prediction using scikit-learn.
  • How to design an agentic AI system — complete with tools and an LLM-powered reasoning core — that interprets machine learning predictions and acts on them autonomously.
  • How to wire the machine learning pipeline and the agent together into a single, end-to-end runnable Python application.

Integrating Agentic AI with Existing Machine Learning Pipelines

Introduction

Agentic AI and machine learning pipelines are far from incompatible when it comes to building production-ready AI applications. In fact, embracing them as two sides of the same coin has become more than a mere trend: it constitutes a modern foundational architecture pattern that drives the shift from passive predictive analytics to autonomous decision-making and action.

Traditional machine learning pipelines excel at pattern recognition tasks of varying complexity, but they are purely reactive in their base form. Meanwhile, agentic AI systems are all about proactivity: combined with predictive machine learning models, they can build on the insights yielded by such models to plan, use tools, and address real-world use cases with little or no human guidance.

In this hands-on article, we will show you how to bridge the gap between reactive machine learning models and proactive AI agents. We will construct a lightweight, free, runnable Python pipeline that:

  1. Predicts customer churn based on a classical machine learning model built with scikit-learn.
  2. Hands the obtained predictions over to an agent endowed with a state-of-the-art LLM to autonomously reason and execute different customer retention strategies.

Prerequisites

The entire coding tutorial can be run for free in Google Colab or a local Jupyter notebook, provided you have the necessary libraries installed and imported.

If you are using Colab, at the time of writing, the only library you might need to manually install is Groq:

!pip install groq

Make sure you also import the following:

import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from groq import Groq

Since Groq — one of today’s most capable open-source LLM providers — requires an API key, be sure to register on their website and create your own API key here. You will need to incorporate it in your notebook or Google Colab account. The code below is designed to read the API key from the “Secrets” section found on the left-hand sidebar in Google Colab: create a new secret variable there called GROQ_API_KEY, and paste your actual Groq API key into the “value” field.

These instructions will help you inject the newly added API key into your program:

import os
from google.colab import userdata
# Injecting the Colab secret into standard environment variables
os.environ["GROQ_API_KEY"] = userdata.get('GROQ_API_KEY')

Step-by-Step Guide

Once the prerequisites are set up, we will start building the classical machine learning pipeline — for customer churn prediction — that will later be extended by incorporating agentic AI principles and tools.

First, we need a customers dataset to feed to our machine learning model. For this example, we will synthetically generate our own dataset containing 500 customers, each described by two predictor features plus a target variable indicating whether the customer is prone to churn. The two input features are the monthly customer spend and the number of support tickets issued by the customer: both are real-world predictors of a customer’s willingness to stay with or abandon a brand. Notice that the code uses numpy functions to introduce random noise, making the artificially generated data look realistic:

# ==========================================
# 0. SYNTHETIC DATASET GENERATION
# ==========================================
# Generating a realistic dataset of 500 customers described by two input features
np.random.seed(42)
n_samples = 500

# Feature 1: Monthly customer's spend (uniformly distributed between $10 and $150)
spend = np.random.uniform(10, 150, n_samples)

# Feature 2: Support tickets issued by customer (Poisson distribution, averaging 1.5 tickets)
tickets = np.random.poisson(lam=1.5, size=n_samples)

# Generate target variable / Binary class (Churn):
# Churn risk increases with more tickets and decreases with higher spend
base_churn_risk = (tickets * 0.15) + np.where(spend < 30, 0.3, 0) - np.where(spend > 100, 0.2, 0)

# Add some random noise to make the dataset realistic
base_churn_risk += np.random.normal(0, 0.1, n_samples)
base_churn_risk = np.clip(base_churn_risk, 0, 1)

# 0 = Retain, 1 = Churn (Threshold at 0.5)
y = (base_churn_risk > 0.5).astype(int)
X = np.column_stack((spend, tickets))

Next, we build a simple, classical machine learning pipeline by splitting the dataset into training and test sets and training a random forest ensemble classifier. We verify the model’s performance on the test set before continuing:

# ==========================================
# 1. CLASSIC ML PIPELINE (Predictive -> Classification)
# ==========================================
# Train/Test Split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train the predictive classifier on the larger dataset
print(f"Training ML Model on {len(X_train)} records...")
ml_model = RandomForestClassifier(n_estimators=50, max_depth=5, random_state=42)

With the synthetic dataset generated and the random forest classifier trained, the pipeline is ready to produce churn predictions that can be passed downstream to the agentic AI layer for autonomous reasoning and action.