For this CodeJam, you will build three agents. Each of these agents will take an active part in solving a burglary and executing a loss appraisal for an insurance claim.
After any good burglary you need a loss appraiser who determines the insurance claims. That will be the first agent you are going to build.
In this exercise, you will build an agent with Python, LiteLLM and CrewAI.
LiteLLM is a library that provides a unified, provider-agnostic API for calling large language models (LLMs) and handling common tasks (completion, chat, streaming, multimodal inputs). It standardizes request/response handling and includes utilities that speed up integration with agent frameworks and tooling. Essentially it is a gateway between LLM providers and AI Agent frameworks.
That means you can use your Generative AI Hub credentials to build state of the art AI Agents with any of the models available through GenAI Hub and any of the AI Agent frameworks compatible with LiteLLM. This combination is extremely powerful because that means you can use LLMs hosted, managed by SAP (Mistral, Llama, Nvidia), and models from our partners such as Azure OpenAI, Amazon Bedrock (including Anthropic) and Gemini.
CrewAI is a third-party open-source Python library. As the name suggests you can use it to build a crew of agents that have a set of tools available to accomplish certain tasks. CrewAI uses tasks to bridge the gap between high-level goals and concrete agent actions, assigning specific objectives and expected outputs. You will use CrewAI as the AI Agent framework for your agents going forward. For now your agent will only be able to respond to incoming queries.
👉 Create a new file /project/Python/starter-project/basic_agent.py (You can just click on the file link to create the file)
👉 Add the following lines of code to import the necessary packages and load the infos from your environment (.env) file:
import os
from pathlib import Path
from dotenv import load_dotenv
from crewai import Agent, Task, Crew
# Load .env from the same directory as this script
env_path = Path(__file__).parent / '.env'
load_dotenv(dotenv_path=env_path)Every agent needs to have at least a role, a goal, and a backstory.
- Role: Defines the agent's identity and expertise domain (e.g., "Loss Appraiser", "Detective"). This shapes how the agent approaches tasks.
- Goal: Specifies what the agent should accomplish. A clear goal helps the LLM stay focused on the desired outcome.
- Backstory: Provides context and personality to the agent, influencing its reasoning style and decision-making approach.
Important is also the parameter llm. Here you can specify which model provider and which LLM you want to use (syntax: provider/llm). We will specify sap as the LLM provider with over 30 models to pick from. LiteLLM is directing calls to the LLMs through the orchestration service of Generative AI Hub. That means you do not need to deploy your models on SAP AI Core. You only need the out of the box deployment of the orchestration service. This way you can easily switch between all the models available via the orchestration service.
👉 Below that add the code for your first agent
# Create a Loss Appraiser Agent
appraiser_agent = Agent(
role="Stolen Goods Loss Appraiser",
goal="Assess the value of stolen items and provide a professional insurance appraisal report.",
backstory="You are an experienced insurance appraiser specializing in fine art and valuables. You provide detailed assessments based on your expertise.",
llm="sap/gpt-4o", # provider/llm - Using one of the models from SAP's model library in Generative AI Hub
verbose=True
)👆 You might have noticed that the properties of the agent are defined in natural language. This is because the LLM is reading the properties of the agent in natural language to understand what the agent does, what it goal is and it's goal.
Every CrewAI agent needs at least one task, otherwise the agent will be inactive. Necessary parameters to fill are description, expected_output and agent.
# Create a task for the appraiser
appraise_loss_task = Task(
description="Provide a brief explanation of how an insurance appraiser would approach assessing stolen artwork and valuables.",
expected_output="A professional explanation of the appraisal process.",
agent=appraiser_agent
)👆 The task description is written in natural language for the LLM (GPT-4o) to read and understand. The LLM acts as the reasoning engine that interprets instructions and generates appropriate responses based on the agent's role, goal, and backstory.
Every crew needs to have at least one agent with at least one associated task. Both of which you defined above.
👉 Add your agent to a crew
# Create a crew with the appraiser agent
crew = Crew(
agents=[appraiser_agent],
tasks=[appraise_loss_task],
verbose=True
)
# Execute the crew
def main():
result = crew.kickoff()
print("\n" + "="*50)
print("Insurance Appraiser Report:")
print("="*50)
print(result)
if __name__ == "__main__":
main()👉 Execute the crew with the basic agent:
☝️ Make sure you're in the repository root directory (e.g.,
codejam-code-based-agents-1) when running this command. If you're already in thestarter-projectfolder, usepython basic_agent.pyinstead.
python project/Python/starter-project/basic_agent.pyYou should see:
- The appraiser agent thinking through the task
- A professional explanation of the appraisal process
👆 At the moment the LLM is just hallucinating because the actual RPT-1 tool is not defined yet. You will implement this in the next exercise.
You created and ran a working AI agent that:
- Agent Definition: Has a role, goal, and backstory that defines its identity and expertise
- Task Processing: Received a task description and used the LLM to generate an appropriate response
- Crew Execution: Ran the agent through CrewAI's orchestration framework
The basic workflow is:
Task → Agent (Role/Goal/Backstory) → LLM Processing (GPT-4o) → Response → Output
- AI Agents are autonomous systems that perceive, reason, and act
- CrewAI provides a structured framework with agents, tasks, and crews
- LiteLLM acts as a gateway between agent frameworks and LLM providers
- Generative AI Hub on SAP AI Core acts as a provider and powers agents with LLMs
In the following exercises, you will:
- ✅ Build a basic agent (this exercise)
- 📌 Add custom tools to your agents so they can access external data
- 📌 Create a complete crew with multiple agents working together
- 📌 Integrate the Grounding Service for better reasoning and fact-checking
- 📌 Solve the museum art theft mystery using your fully-featured agent team
Issue: ModuleNotFoundError: No module named 'crewai'
- Solution: Ensure you're in the correct Python environment:
source venv/bin/activateand runpip install crewai litellm