Home S4/HANA How to Build a Code-Based AI Agent in SAP: Step-by-Step Guide with Python & AI Core

How to Build a Code-Based AI Agent in SAP: A Step-by-Step Guide Using Python & AI Core

This wiki shows, in five steps, how to create an AI agent in SAP. This wiki is based on the SAP videos “SAP CodeBased AI Agents (Part 1)” dated June 1, 2026, and “SAP CodeBased AI Agents (Part 2)” dated June 10, 2026. 

If you really want to automate SAP processes—that is, not just generate responses, but solve tasks independently—AI agents are essential. An AI agent thinks, plans, and acts: It receives a task in natural language, selects the appropriate tool, executes it, and checks the result until the goal is achieved. 

This wiki is intended for developers and technically savvy SAP consultants who want to build a code-based AI agent using Python, LangGraph, and SAP AI Core for the first time. It explains the concept, provides the complete code, and shares best practices from real-world experience. 

According to this wiki, you: 
– the difference between a chatbot and a real AI agent, 
– a working AI agent in Python and connect it to SAP AI Core, 
– define your own tools and use them to extend the agent, 
– deploy the agent securely and transparently in an SAP context.

Table of contents

An AI agent is a software program based on a large language model (LLM). Unlike a simple chatbot, an AI agent is capable of thinking independently, developing plans, and carrying out tasks step by step without requiring human intervention. That is precisely why it is referred to as an autonomous system. 

The key difference from a traditional chatbot is illustrated in the following comparison: 

Simple Chatbot 

AI Agent 

Answer one question at a time. 

Plans and executes multi-step workflows. 

kA Memory. 

Retains context and uses memory. 

Er cannot interact with external systems (passive).

Runs tools (e.g., APIs, databases, etc.).

Based on predefined answers. 

Think creatively and use the tool's results to make adjustments.

2. Core Components of an AI Agent

Every AI agent consists of four core components: 

  1. Brain (model): An LLM (e.g., GPT-4, Claude, or Gemini) processes information and makes decisions
  2. Tools: External APIs, databases, calculators, documents, or programs that the agent can access toperform actions.
  3. Memory & Knowledge: The ability to store past interactions and reference external documents.
  4. Instructions / Guidelines: There are clear guidelines and stop rules, which define what the agent is and is not allowed to do.

3. Why is this relevant? (Business benefits):

AI Agents open up entirely new possibilities for process automation in the SAP environment.While steps that previously had to be performed manually werenecessary were apply the agent now makes intelligent decisions and acts in a context-aware manner. 

An Overview of Specific Benefits: 

  • True automation: No more rigid scripting—the agent dynamically adapts to the process flow. 
  • Greater efficiency: Repetitive SAP tasks (invoice verification, tax calculation, inventory queries) are completed more quickly. 
  • Natural Language as an Interface: Users interact in German or English; no knowledge of SAP transactions is required.
  • Error Reduction: The agent checks the tool's results, detects errors, and corrects itself (self-correction). 
  • Scalability: Once an agent has been developed, it can be extended to other SAP modules and processes. 

 

4. Step-by-Step Instructions:

The following guide shows how to build an AI agent using Python and SAP AI Core: 

Prerequisites:

Several prerequisites must be met for implementation. First, you need an SAP BTP account with an SAP AI Core Service instance (extended plan) provisioned. You also need access to the SAP AI Launchpad (Standard plan). 

In the next step, the AI Core Service Key is downloaded from the BTP Cockpit and either saved as credentials.json or exported as .env variables. 

You will also need the LLM Deployment ID from the SAP AI Launchpad (ML Operations -> Deployments).  

Finally, a local development environment with Python 3.10 or later and Visual Studio Code is required. 

Step 4.1: Virtualgenvironment 

Create and activate a virtual Python environment to cleanly isolate dependencies:

# Create a virtual environment 
python3 –m venv myagent 

# Activation (Windows) 
myagent\Scripts\acrivate 

 

# Activation (macOS/Linux) 
source myagent/bin/activate 

All nInstall any necessary librariesen:

pip install “sap-ai-sdk-gen[all]” langchain-core langchain-community langgraph 

pip install --upgrade pip 

What the packages offer: 

  • sap-ai-sdk-gen: Core SDK for connecting to SAP AI Core. It provides the LangChain-compatible ChatSapOpenAI model and handles automatic OAuth2 token refresh. 
  • langchain-core: Basic interfaces, runnables, and the @tool decorator for defining agent tools with typed signatures and docstrings. 
  • langchain-community: Advanced integrations, including memory, loaders, and utility chains. 
  • langgraph: Provides `create_react_agent`—a stateful agent framework that implements the ReAct loop and maintains the conversation history. 

Step 4.2: Configure Authentication 

Retrieve the following values from the BTP Cockpit: 

AICORE_CLIENT_ID=<Client ID> 

AICORE_CLIENT_SECRET=<Client Secret> 

AICORE_AUTH_URL=<Auth URL>/oauth/token> 

AICORE_BASE_URL=<AI API URL> 

AICORE_RESOURCE_GROUP=<Resource Group aus AI Launchpad> 

AICORE_DEPLOYMENT_ID=<Deployment ID aus AI Launchpad> 

Step 4.3: Create a tool 

Tools are Python functions that the agent can call. Create a new file named tools.py and define the desired tool in it. 

An example from the video is the calculation of corporate income tax:

# tools.py 

from langchain_core.tools import tool 

 

@tool 

def calculate_sap_tay(amount: float) -> str: 

“””Calculate 13.25% corporate income tax on an  
       invoice amount.””” 

       tax_total = amount * 0.1325 

       retutn f”Berechnete Steuer: {tax_total:.2f} CHF” 

The @tool decorator registers the function as a tool for the Langchain agent. The docstring is crucial; it tells the LLM when to call this tool. 

Step 4.4: Set Up Agents 

Create a main.py file that initializes the LLM, sets up the agent, and processes a user request: 

# main.py 
import os 
from dotenv import load_dotenv 
from langgraph.prebuilt import create_react_agent 
from gen_ai_hub.proxy.langchain import ChatOpenAI 
from tools import calculate_sap_tax 

load_dotenv() 

# 1. Initialize the LLM via SAP AI Core 

llm = ChatOpenAI( 

    deployment_id=os.environ["AICORE_DEPLOYMENT_ID"], 

    temperature=0.0 

  

# 2. Building Agents 

agent = create_react_agent(llm, [calculate_sap_tax]) 

  

# 3. Call Agents 

query = input("Enter the invoice amount for tax calculation: ") 

response = agent.invoke({„messages“: [(„user“, query)]}) 

  

print(f“Ergebnis: {response[‚messages‘][-1].content}“) 

temperature=0.0 ensures deterministic, reproducible results. 

Step 4.5: Test and Run Agents 

python main.py 

# Sample interaction: 
# > Enter invoice amount for tax calculation: 5000 
# > Result: Calculated tax: 500.00 CHF 

The agent is now ready for use. It receives the user's request, selects the appropriate tool, executes it, and returns the result. 

5. Architecture / How It Works:

At the heart of every AI agent is the ReAct Loop (Reason + Act Loop). In each iteration, the agent evaluates the current state, decides on an action, executes it, observes the result, and repeats the cycle until the goal is achieved.

Goal Received: The user poses a task or question in natural language. 

  • Reason (Thinking): The LLM analyzes the problem and selects the best action . 
  • Act: The agent calls a tool (API, code, search). 
  • Observe (Observe/Check): The agent reads the tool’s result—success, error, or intermediate result. 
  • Evaluate: Has the goal been achieved? If not, go back to Step 2 (Think) 

6. Best Practices

Start small: First, build the agent for a single workflow with a clear definition of success. No agent should be expected to do everything right from the start. 

Securing Tools: Every tool requires explicit and strict input validation. Undefined inputs lead to "hallucinated" arguments and runtime errors. 

Set guardrails: Implement strict validations and human approval checkpoints before the agent accesses business-critical tasks. Trust is built gradually. 

7. Summary

AI agents are far more than traditional chatbots. They can independently analyze and plan tasks and execute multi-step workflows with the help of tools. The four core components—Brain (LLM), Tools, Memory, and Guardrails—form the foundation of every agent. At the very heart of the system is the ReAct Loop, in which the agent continuously thinks, acts, checks results, and evaluates its approach until the desired goal is achieved. 

SAP AI Core is an enterprise platform that provides the necessary infrastructure for operating such agents. For production use, a clear definition of tasks, secure tool calls, and appropriate control mechanisms are critical success factors for ensuring reliability, security, and traceability. 

Would you like to learn more about AI Units & Co. and find out how to successfully launch your AI project?

Then book an appointment with our SAP AI expert Robert Kehrli here.

 

 

Robert Kehrli Portraitbild
Robert Kehrli
Head of Sales

Published by:

Joana Komnick

author

How did you like the article?

How helpful was this post?

Click on a star to rate!

Average rating 5 / 5.
Number of ratings: 12

No votes so far! Be the first person to rate this post!

INFORMATION

More information

SAP Sapphire 2026 Glossary: Joule, AI Agents, and the Autonomous Enterprise Explained Simply

Joule, AI agents, Autonomous Enterprise, A2A protocol—all the new SAP terms from Sapphire 2026 at a glance. Look them up now in the s-peers glossary.
Wiki Big Query SAP Warehousing

Google BigQuery vs. SAP BW: A Comparison of Two Data Warehouse Solutions

This Wiki article introduces two leading solutions for data management and analysis in today's data-driven world: Google...
AI Transformation and Behavioral Economics: Why People (Often) Don't Want to Change

AI Transformation and Behavioral Economics: Why People (Often) Don't Want to Change

In this wiki, Yvonne uses behavioral economics to explain why change is so difficult in companies. Why do people act...
AI Adoption Success Factors: Change Management

What Really Drives AI Adoption in Companies? 6 Factors That Determine Success or Failure.

This wiki explores why change rarely fails because of technology, but rather because of psychological patterns, and how...
IT Change Management

Psychology in Change Management: 6 Biases That Hinder Adoption (and 6 Countermeasures)

This wiki explores why change rarely fails because of technology, but rather because of psychological patterns, and how...
IT Change Management

IT Change Management: Definition, Process, and Best Practices

Companies realize less than one-third of the expected benefits from digital transformations, not because the technology fails, but because...

What Is FP&A? Definition, Core Processes, and the Future of Corporate Management

Companies aren't managed by reports—they're managed by better decisions. FP&A, or Financial Planning & Analysis, is the strategic...