Join our community of software engineering leaders and aspirational developers. Always
stay in-the-know by getting the most important news and exclusive content delivered
fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter
in the past. Click the button below to open the re-subscribe form
in a new tab. When you're done, simply close that tab and continue
with this form to complete your subscription.
The New Stack does not sell your information or share it with
unaffiliated third parties. By continuing, you agree to our
Terms of Use and
Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!
We’re so glad you’re here. You can expect all the best TNS content to arrive
Monday through Friday to keep you on top of the news and at the top of your game.
What’s next?
Check your inbox for a confirmation email where you can adjust your preferences
and even join additional groups.
Follow TNS on your favorite social media networks.
As businesses and technology push boundaries, staying ahead often means finding more innovative, faster ways to get work done. Enter agentic workflows, a revolutionary approach to task automation that empowers systems to analyze, decide and execute tasks independently. Agentic workflows are not just another tech buzzword; they are about enabling automation that doesn’t just follow a script but adapts in real time to handle complex challenges.
Traditional automation tools follow predefined steps. They’re effective for routine, repetitive work but falter when faced with dynamic, evolving tasks. This is where agentic workflows stand out. They combine flexibility and intelligence to manage complex operations with minimal manual input. By incorporating tools like large language models (LLMs), APIs and other external resources, these workflows act as intelligent agents capable of solving problems, streamlining processes and driving efficiency at a higher level.
For most organizations, this is a game-changer. Agentic workflows can reduce repetitive workloads, reduce error rates and speed up decision-making. When deployed effectively, they drive innovation, allowing teams to focus on high-value tasks while autonomous systems handle the details. To put it simply, agentic workflows make your organization more agile, adaptive and future-ready.
Make decisions based on predefined rules or models.
Execute tasks, often by interacting with external APIs or systems.
Key Benefits:
Automation: Reduce manual effort by delegating tasks to agents.
Adaptability: Agents can dynamically adjust to changing requirements or input.
Efficiency: Optimize multistep tasks by intelligently delegating subtasks.
Common Use Cases:
Customer support bots with escalation workflows.
Autonomous research assistants that retrieve, analyze and summarize information.
Workflow orchestration for IoT devices in smart homes.
Step 1: Setting Up the Environment
Before you begin building your agentic workflow, ensure the required tools are installed. We’ll use OpenAI for natural language processing and Langchain for workflow orchestration.
Install Dependencies:
pip install openai langchain requests
Configure OpenAI API Key:
Store your API key securely using environment variables:
import os
# Set your OpenAI API key
os.environ["OPENAI_API_KEY"] = "your_openai_api_key"
Step 2: Designing the Workflow
For this tutorial, we’ll build an autonomous research agent workflow. The workflow involves:
Accepting a research topic as input.
Retrieving relevant web articles using an external API.
Summarizing the content.
Storing the results in a local file.
Step 3: Creating the Agent Framework
Agents need a core structure to perceive, decide and act. Let’s build this framework step by step.
Basic Agent Class
Define a reusable Agent class that all agents will inherit.
class Agent:
def __init__(self, name):
self.name = name
def perceive(self, input_data):
"""Receive input from the environment."""
raise NotImplementedError("Perceive method must be implemented.")
def decide(self):
"""Make decisions based on perceived input."""
raise NotImplementedError("Decide method must be implemented.")
def act(self):
"""Perform an action based on the decision."""
raise NotImplementedError("Act method must be implemented.")
Step 4: Implementing Specialized Agents
We will create three specialized agents:
Input agent: Accepts the research topic.
Retrieval agent: Fetches articles from an API.
Summarization agent: Summarizes the content.
Input Agent
This agent takes a research topic as input.
class InputAgent(Agent):
def perceive(self, input_data):
self.topic = input_data
def decide(self):
return f"Proceeding with research on: {self.topic}"
def act(self):
print(self.decide())
return self.topic
Retrieval Agent
This agent uses an external API (such as a mock news API) to fetch articles.
This agent uses OpenAI’s API to summarize the content.
import openai
class SummarizationAgent(Agent):
def perceive(self, articles):
self.articles = articles
def decide(self):
summaries = []
for article in self.articles:
prompt = f"Summarize the following article:\n\n{article['content']}"
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
max_tokens=100
)
summaries.append(response.choices[0].text.strip())
return summaries
def act(self):
summaries = self.decide()
for idx, summary in enumerate(summaries):
print(f"Summary {idx + 1}: {summary}")
return summaries
Step 5: Orchestrating the Workflow
Now, let’s integrate the agents into an orchestrated workflow.
class Workflow:
def __init__(self, agents):
self.agents = agents
def run(self, input_data):
current_data = input_data
for agent in self.agents:
agent.perceive(current_data)
current_data = agent.act()
print("Workflow completed.")
Instantiate Agents and Run the Workflow
# Define the API URL for article retrieval
api_url = "https://newsapi.org/v2/everything"
# Create agents
input_agent = InputAgent(name="InputAgent")
retrieval_agent = RetrievalAgent(name="RetrievalAgent", api_url=api_url)
summarization_agent = SummarizationAgent(name="SummarizationAgent")
# Orchestrate workflow
agents = [input_agent, retrieval_agent, summarization_agent]
research_workflow = Workflow(agents)
# Run the workflow
topic = "AI in Healthcare"
research_workflow.run(topic)
Step 6: Enhancing the Workflow
You can extend the functionality of this workflow by:
Adding file storage: Save the summarized content to a text file.
class FileStorageAgent(Agent):
def perceive(self, summaries):
self.summaries = summaries
def decide(self):
return "Summaries saved to research_summaries.txt."
def act(self):
with open("research_summaries.txt", "w") as file:
for summary in self.summaries:
file.write(summary + "\n\n")
print(self.decide())
Error handling: Implement exception handling for API errors and empty responses.
Parallel processing: Use Python’s asyncio to process multiple articles concurrently.
Step 7: Testing and Debugging
Test the workflow with various topics to ensure robustness:
Handle topics with no articles available.
Test with diverse inputs to verify agent adaptability.
Log errors for easier debugging.
Conclusion
Agentic workflows offer a practical approach to creating smart, task-oriented systems. By breaking tasks into specialized components, you can build scalable, flexible solutions for handling complex processes.
By following this step-by-step guide, you’ve mastered the basics of designing and implementing agentic workflows with Python. From setting up individual agents to coordinating them into a unified system, you now have the tools to develop autonomous workflows that fit your specific needs.
Take the next step by experimenting with more advanced agents, integrating additional tools and APIs, and refining decision-making processes to maximize the potential of these workflows.
Revolutionize the way you work with cutting-edge AI innovation. Explore Andela’s definitive guide “3 AI tools transforming productivity.”
Andela provides the world’s largest private marketplace for global remote tech talent driven by an AI-powered platform to manage the complete contract hiring lifecycle. Andela helps companies scale teams & deliver projects faster via specialized areas: App Engineering, AI, Cloud, Data & Analytics.