Build ReAct Agent from Scratch Using LangGraph
End-to-End guide to implement ReAct Agent [Code Included]
26 min read3 days ago
–
Analogy:
Life is full of challenges.
Last week, I was about to move out of my house, suddenly it started raining. I thought I should not get wet. So I took an umbrella.
Simple, right? Yet, this small decision reflects something powerful.
As humans, we constantly observe the world around us, reason about the situation, and take action accordingly.
This intuitive cycle of Observation → Reasoning → Action is exactly what ReAct agents are designed to emulate.
Press enter or click to view image in full size
ReAct Agent (Image By Author: Generated by ChatGPT)
In this article, we will walk through building an end-to-end ReAct age…
Build ReAct Agent from Scratch Using LangGraph
End-to-End guide to implement ReAct Agent [Code Included]
26 min read3 days ago
–
Analogy:
Life is full of challenges.
Last week, I was about to move out of my house, suddenly it started raining. I thought I should not get wet. So I took an umbrella.
Simple, right? Yet, this small decision reflects something powerful.
As humans, we constantly observe the world around us, reason about the situation, and take action accordingly.
This intuitive cycle of Observation → Reasoning → Action is exactly what ReAct agents are designed to emulate.
Press enter or click to view image in full size
ReAct Agent (Image By Author: Generated by ChatGPT)
In this article, we will walk through building an end-to-end ReAct agent using LangGraph, applying it to a practical and meaningful problem statement.
Introduction to ReAct Framework (in the context of Generative AI)
ReAct stands for Reasoning and Acting. At its core, a ReAct agent mimics the way humans approach problems:
- Observation — The agent perceives the environment or receives input.
- Reasoning — It internally thinks through possible actions, evaluates options, and predicts outcomes.
- Action — Finally, it takes a step in the world, such as querying a database, calling an API, or generating a response.
This cycle allows the agent to adapt dynamically, combining logic and actions to solve complex tasks rather than just following a fixed set of instructions.
For example, imagine a ReAct agent helping with travel planning:
- Observation: User wants to travel to Paris next month.
- Reasoning: Check for flights, accommodations, and visa requirements.
- Action: Suggest the best options and book tickets if requested.
By reasoning before acting, ReAct agents are more flexible, robust, and capable of handling real-world scenarios effectively.
Press enter or click to view image in full size
ReAct Agent Framework (Image by Author)
Let’s now implement ReAct based agent on a real world usecase. Complete Notebook can be found here.
Problem Statement
Problem:
Finding nearby hospitals or reliable medical information can be time-consuming, especially when users lack specific details like location or medical specialty. Existing systems often provide static data or require multiple manual searches.
Proposed Solution
We propose an interactive medical assistant that asks users for missing details, finds nearby hospitals, and provides quick, accurate medical information using smart tools and step-by-step reasoning.
Techstack
- Python
- LangGraph
- LangChain
- Chroma DB
- SerperAPI (websearch)
- OpenAI API (LLM)
- Geocoding API (To get GeoCode of the user location)
Importing the required Modules
import pandas as pdimport numpy as npimport jsonimport refrom typing import List, Dict, Any, Tupleimport faissfrom sentence_transformers import SentenceTransformerfrom openai import OpenAIimport timefrom sklearn.metrics import accuracy_score, precision_score, recall_score, f1_scoreimport seaborn as snsfrom dotenv import load_dotenvimport openaiimport osimport chromadbimport sqlite3from langchain_community.utilities import GoogleSerperAPIWrapperfrom math import radians, cos, sin, sqrt, atan2# ✅ Load environment variablesload_dotenv()openai_api_key = os.getenv("OPEN_AI_KEY")SERPER_API_KEY = os.getenv("SERPER_API_KEY")GEOCODE_API_KEY = os.getenv("GEOCODE_API_KEY")
Load the Data
A. Question & Answer Data
We are using medical Question & Answer Data which has ~15000 question and answer pairs for wide variety of health topics.
Source: Kaggle.
We sample the 500 rows from it for our expermentation.
## Data 1: reading the Comprehensive Medical Q&A Datasetdf_qa = pd.read_csv("medical_q_n_a.csv")## Data has 16407 rows, hence we will sample 500 rows for experimentationdf_qa = df_qa.sample(500, random_state=0).reset_index(drop=True)# Preparing the Dataframe for Vector DB by combining the Textdf_qa['combined_text'] = ( "Question: " + df_qa['Question'].astype(str) + ". " + "Answer: " + df_qa['Answer'].astype(str) + ". " + "Type: " + df_qa['qtype'].astype(str) + ". ")df_qa.head()
Press enter or click to view image in full size
First 5 rows of Medical Question & Answer Dataset
B. USA Hospital Data
This dataset contains information about hospitals in the USA including their location, type, number of beds, and other relevant details.
Source: Kaggle
## Data 2: reading the USA Hospitals Datasetdf_hospital = pd.read_csv("Hospitals.csv")print(df_hospital.shape)df_hospital[["LATITUDE", "LONGITUDE", "CITY", "STATE", "TYPE", "NAME", "ADDRESS", "BEDS", "WEBSITE", "COUNTRY"]].head()
Press enter or click to view image in full size
First 5 rows of the USA Hospital Dataset
Setting up the Vector Store
We will upload the Question & Answer pairs data to a chromadb (Open Source Vector Store) which will be later used by the ReAct agent as a Knowledge base if user asks health realted questions.
Creating ChromaDB Client and Collection
We create ChromDB client using chromadb library. Then we create the collection named “medical_q_n_a”.
Think of collections like tables in the database. (I have written a detailed article about ChromaDB here if you want to know more.)
import chromadb# Setting up the Chromadbclient = chromadb.PersistentClient(path="./chroma_db")# Collection 1 for medical Q&A Datasetcollection1 = client.get_or_create_collection(name="medical_q_n_a")
Updating the Data to the Collection
Please note here the chromadb will use default vector embedding model from sentence transformers.
# Add data to collection# here the chroma db will use default embeddings (sentence transformers)collection1.add( documents=df_qa['combined_text'].tolist(), metadatas=df_qa.to_dict(orient="records"), ids=df_qa.index.astype(str).tolist(),)
Testing the ChromaDB
# quick checkquery = "What are the treatments for Kawasaki disease ?"results = collection1.query(query_texts=[query], n_results=3)print(results)
Output: (This will give top 5 chunks)
{ "ids": [["1", "393", "60"]], "embeddings": null, "documents": [[ "Question: What are the treatments for Kawasaki disease ?. Answer: These resources address the diagnosis or management of Kawasaki disease: - Cincinnati Children's Hospital Medical Center - Genetic Testing Registry: Acute febrile mucocutaneous lymph node syndrome - National Heart, Lung, and Blood Institute: How is Kawasaki Disease Treated? These resources from MedlinePlus offer information about the diagnosis and management of various health conditions: - Diagnostic Tests - Drug Therapy - Surgery and Rehabilitation - Genetic Counseling - Palliative Care. Type: treatment. ", "Question: What are the treatments for Krabbe Disease ?. Answer: There is no cure for Krabbe disease. Results of a very small clinical trial of children with infantile Krabbe disease found that children who received umbilical cord blood stem cells from unrelated donors prior to symptom onset developed with little neurological impairment. Bone marrow transplantation may help some people. Generally, treatment for the disorder is symptomatic and supportive. Physical therapy may help maintain or increase muscle tone and circulation.. Type: treatment. ", "Question: What are the treatments for Ehlers-Danlos syndrome, progeroid type ?. Answer: How might Ehlers-Danlos syndrome progeroid type be treated? Individuals with Ehlers-Danlos Syndrome progeroid type can benefit from a variety of treatments depending on their symptoms. Affected children with weak muscle tone and delayed development might benefit from physiotherapy to improve muscle strength and coordination. Affected individuals with joint pain might benefit from anti-inflammatory drugs. Lifestyle changes or precautions during exercise or intense physical activity may be advised to reduce the chance of accidents to the skin and bone. It is recommended that affected individuals discuss treatment options with their healthcare provider.. Type: treatment. " ]], "uris": null, "included": ["metadatas", "documents", "distances"], "data": null, "metadatas": [[ { "Answer": "These resources address the diagnosis or management of Kawasaki disease: - Cincinnati Children's Hospital Medical Center - Genetic Testing Registry: Acute febrile mucocutaneous lymph node syndrome - National Heart, Lung, and Blood Institute: How is Kawasaki Disease Treated? These resources from MedlinePlus offer information about the diagnosis and management of various health conditions: - Diagnostic Tests - Drug Therapy - Surgery and Rehabilitation - Genetic Counseling - Palliative Care", "combined_text": "Question: What are the treatments for Kawasaki disease ?. Answer: These resources address the diagnosis or management of Kawasaki disease: - Cincinnati Children's Hospital Medical Center - Genetic Testing Registry: Acute febrile mucocutaneous lymph node syndrome - National Heart, Lung, and Blood Institute: How is Kawasaki Disease Treated? These resources from MedlinePlus offer information about the diagnosis and management of various health conditions: - Diagnostic Tests - Drug Therapy - Surgery and Rehabilitation - Genetic Counseling - Palliative Care. Type: treatment. ", "Question": "What are the treatments for Kawasaki disease ?", "qtype": "treatment" }, { "qtype": "treatment", "Question": "What are the treatments for Krabbe Disease ?", "combined_text": "Question: What are the treatments for Krabbe Disease ?. Answer: There is no cure for Krabbe disease. Results of a very small clinical trial of children with infantile Krabbe disease found that children who received umbilical cord blood stem cells from unrelated donors prior to symptom onset developed with little neurological impairment. Bone marrow transplantation may help some people. Generally, treatment for the disorder is symptomatic and supportive. Physical therapy may help maintain or increase muscle tone and circulation.. Type: treatment. ", "Answer": "There is no cure for Krabbe disease. Results of a very small clinical trial of children with infantile Krabbe disease found that children who received umbilical cord blood stem cells from unrelated donors prior to symptom onset developed with little neurological impairment. Bone marrow transplantation may help some people. Generally, treatment for the disorder is symptomatic and supportive. Physical therapy may help maintain or increase muscle tone and circulation." }, { "qtype": "treatment", "Question": "What are the treatments for Ehlers-Danlos syndrome, progeroid type ?", "combined_text": "Question: What are the treatments for Ehlers-Danlos syndrome, progeroid type ?. Answer: How might Ehlers-Danlos syndrome progeroid type be treated? Individuals with Ehlers-Danlos Syndrome progeroid type can benefit from a variety of treatments depending on their symptoms. Affected children with weak muscle tone and delayed development might benefit from physiotherapy to improve muscle strength and coordination. Affected individuals with joint pain might benefit from anti-inflammatory drugs. Lifestyle changes or precautions during exercise or intense physical activity may be advised to reduce the chance of accidents to the skin and bone. It is recommended that affected individuals discuss treatment options with their healthcare provider.. Type: treatment. ", "Answer": "How might Ehlers-Danlos syndrome progeroid type be treated? Individuals with Ehlers-Danlos Syndrome progeroid type can benefit from a variety of treatments depending on their symptoms. Affected children with weak muscle tone and delayed development might benefit from physiotherapy to improve muscle strength and coordination. Affected individuals with joint pain might benefit from anti-inflammatory drugs. Lifestyle changes or precautions during exercise or intense physical activity may be advised to reduce the chance of accidents to the skin and bone. It is recommended that affected individuals discuss treatment options with their healthcare provider." } ]], "distances": [[0.4760013818740845, 0.9692587852478027, 1.0237324237823486]]}
Setting up the WebSearch API
We use SerperAPI for Websearch. It gives google search results for a given query. You can get your free API (no credit card required) from here.
I have kept all my API keys in the .env file.
from langchain_community.utilities import GoogleSerperAPIWrapperSERPER_API_KEY = os.getenv("SERPER_API_KEY")search = GoogleSerperAPIWrapper()# testing the google search APIsearch.run(query="What are the various vaccines of COVID-19")
Output:
Different types of COVID-19 vaccines: How they work · mRNA vaccine · mRNA vaccine · Messenger RNA (mRNA) vaccine · Viral vector vaccine · Viral vector vaccine ... First introduced in December 2020, the original COVID mRNA vaccines from both Pfizer and Moderna protected against the original SARS-CoV-2 virus ... Currently, there are two types of COVID-19 vaccines for use in the United States: mRNA, and protein subunit vaccines. None of these vaccines can ... COVID-19 vaccines include viral vector, genetic, inactivated, attenuated, and protein vaccines. These use different methods to stimulate an immune response. Types of COVID-19 vaccines. Two types of COVID-19 vaccines are recommended for use in the United States: mRNA vaccines. Moderna COVID-19 Vaccine ... Pfizer, Moderna, Novavax. Helps protect people of all ages against COVID-19 illness that is severe, requires hospital care, or causes death. List of COVID-19 vaccine authorizations · Contents · Overview maps · Oxford–AstraZeneca · Pfizer–BioNTech · Janssen · Moderna · Sinopharm BIBP · Sputnik V. The COVID-19 VIS may be used when administering fully licensed vaccine (mRNA vaccine to people 12 years and older). For links to the current EUA ... WHO recommends a simplified single-dose regime for primary immunization for most COVID-19 vaccines which would improve acceptance and uptake and provide ... WHO has granted EUL to 12 vaccines, including Novavax (40 countries), Spikevax (88 countries), Comirnaty (149 countries), and Jcovden (113 countries).'
Setting up Geocode API
We use geocode API to get the longitude and latitude for any of given location. This will be used to locate the closest hopital based on user location.
You can get your free API (no credit card required) from here.
import requests# this function gets the latitude and longitude of a given address using the Geocode APIdef get_coordinates(address): url = "https://geocode.maps.co/search" params = {"q": address, "api_key": "GEOCODE_API_KEY"} # API key optional for free tier response = requests.get(url, params=params) if response.status_code == 200: data = response.json() if data: lat = data[0]["lat"] lon = data[0]["lon"] print(f"Latitude: {lat}, Longitude: {lon}") return lat, lon else: print("No results found.") else: print(f"Error: {response.status_code}")# Example usageget_coordinates("new york city")
Output:
Latitude: 40.7127281, Longitude: -74.0060152('40.7127281', '-74.0060152')
Setting up the Open AI API
We setup OpenAI API client to use gpt-5-mini. This SLM (Small Language Model) will be the brain of the agent.
Please note, you will need a valid API key use this.
from openai import OpenAIopenai_api_key = os.getenv("OPEN_AI_KEY")def get_llm_response(prompt: str) -> str: """Function to get response from LLM""" from openai import OpenAI client_llm = OpenAI(api_key=openai_api_key) response = client_llm.chat.completions.create( model="gpt-5-mini", messages=[{"role": "user", "content": prompt}] ) return response.choices[0].message.content## testing the LLM responseprompt = "Explain the impact of GenAI on healthcare in 30 words"response = get_llm_response(prompt)print(response)
Generative AI accelerates diagnostics, personalized treatment planning, drug discovery, and patient engagement; automates documentation and workflows, but raises privacy, bias, safety, and regulatory challenges requiring careful governance and clinical validation.
Now we start defining the tools for the ReAct Agent.
Defining the Tools
We will define four stateless tools as python functions.
A. Retrieval of the Context
This tool returns the relevant chunks from the Vector Database (ChromaDB) for a given query.
def retrieve_context_q_n_a(query: str): """Stateless: returns relevant medical Q&A context.""" print("---TOOLCALL: RETRIEVING CONTEXT---") results = collection1.query(query_texts=[query], n_results=3) context = "\n".join(results["documents"][0]) return {"context": context, "source": "Medical Q&A Collection"}
B. WebSearch
This tool returns the google search results for a given query using the Serper API.
def web_search(query: str): """Stateless: returns web search results.""" print("---TOOLCALL: PERFORMING WEB SEARCH---") search_results = search.run(query=query) print(search_results) return {"context": search_results, "source": "Web Search"}
C. Ask User
If the agent wants to ask a follow-up question (example: location) to the user then this function ask_user can be invoked.
In the notebook setup this displays a input widget in the notebook where user can enter the information.
def ask_user(question: str): """Ask the user for input and return the response.""" print(f"---USER INPUT REQUIRED---\n{question}") answer = input(f"{question}: ") # This will pause the notebook and wait for input return {"context": answer, "source": "User Input"}
D. Search Nearest Hospitals:
In order to search for nearest hospitals in USA, this tool can be invoked.
def search_nearest_hospital(user_location: str, specialty: str = None, top_n: int = 3): """Stateless: returns nearest hospitals based on location/specialty.""" print("---SEARCHING NEAREST HOSPITAL---") user_coords = get_coordinates(user_location) if not user_coords: return {"error": "Could not get coordinates"} user_lat, user_lon = user_coords def haversine(lat1, lon1, lat2, lon2): lat1, lon1, lat2, lon2 = map(radians, [float(lat1), float(lon1), float(lat2), float(lon2)]) dlon = lon2 - lon1 dlat = lat2 - lat1 a = sin(dlat/2)**2 + cos(lat1)*cos(lat2)*sin(dlon/2)**2 c = 2 * atan2(sqrt(a), sqrt(1-a)) return 6371 * c # km if specialty: filtered_df = df_hospital[df_hospital['TYPE'].str.contains(specialty, case=False, na=False)] else: filtered_df = df_hospital.copy() filtered_df['distance_km'] = filtered_df.apply( lambda row: haversine(user_lat, user_lon, row['LATITUDE'], row['LONGITUDE']), axis=1 ) nearest = filtered_df.sort_values('distance_km').head(top_n) nearest["distance_km"] = nearest["distance_km"].round(2) results = nearest[['NAME','ADDRESS','CITY','STATE','TYPE','BEDS','WEBSITE','distance_km']].to_dict(orient='records') return {"context": results, "source": "Hospital Search"}
Defining the ReAct Agent
Now we define the brain of the system → The ReAct Agent.
First we define the AgentState using TypeDict. Then we define the call_model function for the LLM call.
Note that, Here the most important part is the prompt technique. We are using ReAct style prompting technique, where the agent is guided to follow Observation → Thought → Action loop till the time agent has the final answer.
The call_model function sends the current conversation state (user query, past tool outputs, and available tools) to the LLM, asking it to decide the next step — either which tool to call next (with arguments) or to provide a final ANSWER. It then saves the model’s response back into the state so the workflow can continue in the next step.
import jsonfrom math import radians, cos, sin, sqrt, atan2from langgraph.graph import StateGraph, ENDfrom typing import TypedDict# === State Schema ===class AgentState(TypedDict): query: str last_agent_response: str tool_obervations: list num_steps: int user_location: str # Optional field for follow-up location info# === LLM Call ===def call_model(state: dict) -> dict: print("\n") print("=== STEP", state.get("num_steps", 0), "===") print("---CALLING LLM MODEL---") query = state.get("query", "") tool_obervations = "\n".join(state.get("tool_obervations", [])) user_location = state.get("user_location", "") tools_list = """ Available tools: 1. retrieve_context_q_n_a Description: Retrieve relevant medical Q&A documents. Arguments: query (string) 2. search_nearest_hospital Description: Find nearest hospitals. Arguments: user_location (string), specialty (string, optional), top_n (int, optional) 3. web_search Description: Perform a web search. Arguments: query (string) 4. ask_user Description: Ask the user for missing info. Arguments: question (string) """ prompt = f""" You are a medical assistant with access to these actions: {tools_list} User query: "{query}" Past tool_obervations: {tool_obervations} Instructions: 1. Always Start with THOUGHT, then decide on (ACTION and ARGUMENTS) or ANSWER. 2. Carefully check past tool_obervations to see if the answer is already available. 3. If not, choose the most relevant tool to gather more information. 4. If any required info is missing (e.g., user location), use 'ask_user'. 5. Please don't answer anything based on General knowledge or assumptions without sufficient information. 6. ARGUMENTS must be valid JSON with keys in double quotes. 7. Please don't add anything outside the specified format. --- Sample Session Example: User query: "What are the symptoms of Coronavirus?" THOUGHT: The user wants a cardiologist and has provided their location. I should find nearby hospitals with cardiology departments. ACTION: retrieve_context_q_n_a ARGUMENTS: {{"query": "symptoms of Coronavirus"}} [Tool results come back] THOUGHT: The retrieved context does not provides the symptoms of Coronavirus. I should now check the websearch. ACTION: websearch ARGUMENTS: {{"query": "symptoms of Coronavirus"}} [Tool results come back] THOUGHT: The retrieved context now provides the symptoms of Coronavirus. I should now check the websearch. ANSWER: "The symptoms of Coronavirus include fever, cough, fatigue, loss of taste or smell, and difficulty breathing.(LIMIT TO 30 WORDS)" --- """ response = get_llm_response(prompt) state["last_agent_response"] = response state["num_steps"] += 1 print(response) return state
Tool Caller Function
call_tool function reads the model’s last response to find which tool should be used (like web search or hospital lookup), extracts any JSON arguments, and then calls that tool with the given inputs. It then saves the tool’s results or user responses back into the state for the next step of the workflow.
# === Tool Caller ===def call_tool(state: dict) -> dict: action_text = state.get("last_agent_response", "") if "ACTION:" not in action_text: state.setdefault("tool_obervations", []).append(f"[No action found: {action_text}]") return state print(f"---CALLING TOOL---") tool_name = action_text.split("ACTION:")[1].split("\n")[0].strip() print(f"Tool to call: {tool_name}") args = {} if "ARGUMENTS:" in action_text: args_text = action_text.split("ARGUMENTS:")[1].strip() # Safely extract JSON block if args_text.startswith("{"): brace_count = 0 end_index = 0 for i, char in enumerate(args_text): if char == "{": brace_count += 1 elif char == "}": brace_count -= 1 if brace_count == 0: end_index = i + 1 break args_text = args_text[:end_index] try: args = json.loads(args_text) print(f"Parsed arguments: {args}") except json.JSONDecodeError: print(f"Failed to parse JSON arguments: {args_text}") state.setdefault("tool_obervations", []).append(f"[Failed to parse arguments: {args_text}]") args = {} # Map to stateless tools tool_map = { "retrieve_context_q_n_a": retrieve_context_q_n_a, "search_nearest_hospital": search_nearest_hospital, "web_search": web_search, "ask_user": ask_user } if tool_name in tool_map: results = tool_map[tool_name](**args) if tool_name == "ask_user": # Append user input to observations state.setdefault("tool_obervations", []).append(f"[User input: {results['context']}]") # Save user_location if question mentions it if "location" in args.get("question", "").lower(): state["user_location"] = results["context"] else: # Append tool results state.setdefault("tool_obervations", []).append(f"[{tool_name} results: {results['context']}]") state["last_tool_results"] = results print(results) return state state.setdefault("tool_obervations", []).append(f"[Unknown tool: {tool_name}]") return state
Putting a Stop Criteria
should_continue function checks if the model’s latest response contains an ANSWER, meaning the task is complete. If it finds “ANSWER:”, the workflow ends; otherwise, it continues to the next step.
# === Should Continue ===def should_continue(state: dict) -> str: last_agent_response = state.get("last_agent_response", "") if "ANSWER:" in last_agent_response.upper(): print("Found answer in the last action text, ending workflow.") return "end" print("--Continuing workflow.") return "continue"
Building the Graph
This code builds the workflow graph that controls how the medical assistant operates. It defines two main steps — the agent (LLM reasoning) and tools (function calls) — and connects them so the agent alternates between thinking and acting until an ANSWER ends the process.
# === Workflow Graph ===workflow = StateGraph(state_schema=AgentState)workflow.add_node("agent", call_model)workflow.add_node("tools", call_tool)workflow.set_entry_point("agent")workflow.add_conditional_edges( "agent", should_continue, {"continue": "tools", "end": END})workflow.add_edge("tools", "agent")agentic_graph = workflow.compile()# === Visualize ===from IPython.display import Image, displaydisplay(Image(agentic_graph.get_graph().draw_mermaid_png()))
Press enter or click to view image in full size
ReAct Agent (LangGraph)
Hurray!! Our ReAct Agent is ready. Now let’s test the agent:
Testing the ReAct Agent
Query 1: Medical Query
agent_state = { "query": "What are the symptoms of HIV virus?", "last_agent_response": "", "tool_obervations": [], "num_steps": 0}result = agentic_graph.invoke(agent_state)
Output:
=== STEP 0 ===---CALLING LLM MODEL---THOUGHT: I should first check existing medical Q&A documents for accurate information about HIV symptoms.ACTION: retrieve_context_q_n_aARGUMENTS: {"query": "symptoms of HIV"}--Continuing workflow.---CALLING TOOL---Tool to call: retrieve_context_q_n_aParsed arguments: {'query': 'symptoms of HIV'}---TOOLCALL: RETRIEVING CONTEXT---{'context': "Question: What are the symptoms of Hyper-IgD syndrome ?. Answer: What are the signs and symptoms of Hyper-IgD syndrome? Hyper IgD syndrome is characterized by periodic high fevers accompanied by lymphadenopathy, abdominal pain, diarrhea, headache, joint pain, hepatomegaly and/or splenomegaly, and skin lesions. Most episodes last several days and occur periodically throughout life. The frequency of episodes and their severity vary greatly from case to case. The first attack usually takes place during infancy. Patients may have no symptoms between attacks. However, in some patients, the attacks may be so frequent that the symptoms persist. The Human Phenotype Ontology provides the following list of signs and symptoms for Hyper-IgD syndrome. If the information is available, the table below includes how often the symptom is seen in people with this condition. You can use the MedlinePlus Medical Dictionary to look up the definitions for these medical terms. Signs and Symptoms Approximate number of patients (when available) Abdominal pain 90% Abnormality of temperature regulation 90% Arthralgia 90% Gastrointestinal hemorrhage 90% Hepatomegaly 90% Lymphadenopathy 90% Myalgia 90% Abnormality of the oral cavity 50% Arthritis 50% Diarrhea 50% Migraine 50% Urticaria 50% Vasculitis 50% Abnormal immunoglobulin level 7.5% Acrocyanosis 7.5% Cognitive impairment 7.5% Incoordination 7.5% Intestinal obstruction 7.5% Limitation of joint mobility 7.5% Peritonitis 7.5% Seizures 7.5% Subcutaneous hemorrhage 7.5% Rod-cone dystrophy 5% Autosomal recessive inheritance - Elevated erythrocyte sedimentation rate - Headache - Hypermelanotic macule - Increased IgA level - Leukocytosis - Nyctalopia - Optic disc pallor - Skin rash - Splenomegaly - Vertigo - Vomiting - The Human Phenotype Ontology (HPO) has collected information on how often a sign or symptom occurs in a condition. Much of this information comes from Orphanet, a European rare disease database. The frequency of a sign or symptom is usually listed as a rough estimate of the percentage of patients who have that feature. The frequency may also be listed as a fraction. The first number of the fraction is how many people had the symptom, and the second number is the total number of people who were examined in one study. For example, a frequency of 25/25 means that in a study of 25 people all patients were found to have that symptom. Because these frequencies are based on a specific study, the fractions may be different if another group of patients are examined. Sometimes, no information on frequency is available. In these cases, the sign or symptom may be rare or common.. Type: symptoms. \nQuestion: What is (are) AIDS-Related Lymphoma ?. Answer: Key Points\n - AIDS-related lymphoma is a disease in which malignant (cancer) cells form in the lymph system of patients who have acquired immunodeficiency syndrome (AIDS). - There are many different types of lymphoma. - Signs of AIDS-related lymphoma include weight loss, fever, and night sweats. - Tests that examine the lymph system and other parts of the body are used to help detect (find) and diagnose AIDS-related lymphoma. - Certain factors affect prognosis (chance of recovery) and treatment options.\n \n \n AIDS-related lymphoma is a disease in which malignant (cancer) cells form in the lymph system of patients who have acquired immunodeficiency syndrome (AIDS).\n AIDS is caused by the human immunodeficiency virus (HIV), which attacks and weakens the body's immune system. The immune system is then unable to fight infection and disease. People with HIV disease have an increased risk of infection and lymphoma or other types of cancer. A person with HIV disease who develops certain types of infections or cancer is then diagnosed with AIDS. Sometimes, people are diagnosed with AIDS and AIDS-related lymphoma at the same time. For information about AIDS and its treatment, please see the AIDSinfo website. AIDS-related lymphoma is a type of cancer that affects the lymph system, which is part of the body's immune system. The immune system protects the body from foreign substances, infection, and diseases. The lymph system is made up of the following: - Lymph: Colorless, watery fluid that carries white blood cells called lymphocytes through the lymph system. Lymphocytes protect the body against infections and the growth of tumors. - Lymph vessels: A network of thin tubes that collect lymph from different parts of the body and return it to the bloodstream. - Lymph nodes: Small, bean-shaped structures that filter lymph and store white blood cells that help fight infection and disease. Lymph nodes are located along the network of lymph vessels found throughout the body. Clusters of lymph nodes are found in the neck, underarm, abdomen, pelvis, and groin. - Spleen: An organ that makes lymphocytes, filters the blood, stores blood cells, and destroys old blood cells. The spleen is on the left side of the abdomen near the stomach. - Thymus: An organ in which lymphocytes grow and multiply. The thymus is in the chest behind the breastbone. - Tonsils: Two small masses of lymph tissue at the back of the throat. The tonsils make lymphocytes. - Bone marrow: The soft, spongy tissue in the center of large bones. Bone marrow makes white blood cells, red blood cells, and platelets. Lymph tissue is also found in other parts of the body such as the brain, stomach, thyroid gland, and skin. Sometimes AIDS-related lymphoma occurs outside the lymph nodes in the bone marrow, liver, meninges (thin membranes that cover the brain) and gastrointestinal tract. Less often, it may occur in the anus, heart, bile duct, gingiva, and muscles.\n \n \n There are many different types of lymphoma.\n Lymphomas are divided into two general types: - Hodgkin lymphoma. - Non-Hodgkin lymphoma. Both Hodgkin lymphoma and non-Hodgkin lymphoma may occur in patients with AIDS, but non-Hodgkin lymphoma is more common. When a person with AIDS has non-Hodgkin lymphoma, it is called AIDS-related lymphoma. When AIDS-related lymphoma occurs in the central nervous system (CNS), it is called AIDS-related primary CNS lymphoma. Non-Hodgkin lymphomas are grouped by the way their cells look under a microscope. They may be indolent (slow-growing) or aggressive (fast-growing). AIDS-related lymphomas are aggressive. There are two main types of AIDS-related non-Hodgkin lymphoma: - Diffuse large B-cell lymphoma (including B-cell immunoblastic lymphoma). - Burkitt or Burkitt-like lymphoma. For more information about lymphoma or AIDS-related cancers, see the following PDQ summaries: - Adult Non-Hodgkin Lymphoma Treatment - Childhood Non-Hodgkin Lymphoma Treatment - Primary CNS Lymphoma Treatment - Kaposi Sarcoma Treatment. Type: information. \nQuestion: What are the symptoms of ICF syndrome ?. Answer: What are the signs and symptoms of ICF syndrome? The Human Phenotype Ontology provides the following list of signs and symptoms for ICF syndrome. If the information is available, the table below includes how often the symptom is seen in people with this condition. You can use the MedlinePlus Medical Dictionary to look up the definitions for these medical terms. Signs and Symptoms Approximate number of patients (when available) Decreased antibody level in blood 90% Recurrent respiratory infections 90% Short stature 90% Abnormality of neutrophils 50% Anemia 50% Cellular immunodeficiency 50% Cognitive impairment 50% Communicating hydrocephalus 50% Depressed nasal bridge 50% Lymphopenia 50% Macrocephaly 50% Malabsorption 50% Abnormality of the tongue 7.5% Epicanthus 7.5% Hypertelorism 7.5% Low-set, posteriorly rotated ears 7.5% Malar flattening 7.5% Umbilical hernia 7.5% Anteverted nares - Autosomal recessive inheritance - Bronchiectasis - Chronic bronchitis - Diarrhea - Failure to thrive - Flat face - Immunodeficiency - Intellectual disability - Low-set ears - Macroglossia - Pneumonia - Protruding tongue - Sinusitis - T lymphocytopenia - The Human Phenotype Ontology (HPO) has collected information on how often a sign or symptom occurs in a condition. Much of this information comes from Orphanet, a European rare disease database. The frequency of a sign or symptom is usually listed as a rough estimate of the percentage of patients who have that feature. The frequency may also be listed as a fraction. The first number of the fraction is how many people had the symptom, and the second number is the total number of people who were examined in one study. For example, a frequency of 25/25 means that in a study of 25 people all patients were found to have that symptom. Because these frequencies are based on a specific study, the fractions may be different if another group of patients are examined. Sometimes, no information on frequency is available. In these cases, the sign or symptom may be rare or common.. Type: symptoms. ", 'source': 'Medical Q&A Collection'}=== STEP 1 ===---CALLING LLM MODEL---THOUGHT: The user asks for symptoms of the HIV virus; I should look up authoritative, up-to-date symptom information.ACTION: web_searchARGUMENTS: {"query": "symptoms of HIV virus"}--Continuing workflow.---CALLING TOOL---Tool to call: web_searchParsed arguments: {'query': 'symptoms of HIV virus'}---TOOLCALL: PERFORMING WEB SEARCH---Fever. Fatigue. Swollen lymph glands, which are often one of the first symptoms of HIV infection. Diarrhea. Weight loss. Symptoms can last anywhere from a few days to several weeks. But some people do not have any symptoms at all during this early stage of HIV. HIV is the virus that causes acquired immune deficiency syndrome (AIDS). HIV weakens your immune system by destroying your T-cells until you are unable to ... Most people have flu-like symptoms within 2 to 4 weeks after infection. Symptoms may last for a few days or several weeks. Having these symptoms ... Some teens and adults may develop a flu-like illness within a month or two after exposure to the HIV virus, although, many people do not develop any symptoms ... Learn more about the wide range of symptoms for HIV and AIDS, such as fever, headache, malaise, and enlarged lymph nodes. Early symptoms of HIV · a raised temperature (fever) · a sore throat · a rash · tiredness · joint pain · muscle pain · swollen glands. Symptoms of HIV infection · a sore throat · a high temperature · swollen glands · muscle aches and tiredness · a skin rash. The three stages of HIV infection are (1) acute HIV infection, (2) chronic HIV infection, and (3) acquired immunodeficiency syndrome (AIDS). There is no cure ... Fever and muscle pains · Headache · Sore throat; Night sweats; Mouth sores, including yeast infection (thrush); Swollen lymph glands; Diarrhea. Many people have ...{'context': 'Fever. Fatigue. Swollen lymph glands, which are often one of the first symptoms of HIV infection. Diarrhea. Weight loss. Symptoms can last anywhere from a few days to several weeks. But some people do not have any symptoms at all during this early stage of HIV. HIV is the virus that causes acquired immune deficiency syndrome (AIDS). HIV weakens your immune system by destroying your T-cells until you are unable to ... Most people have flu-like symptoms within 2 to 4 weeks after infection. Symptoms may last for a few days or several weeks. Having these symptoms ... Some teens and adults may develop a flu-like illness within a month or two after exposure to the HIV virus, although, many people do not develop any symptoms ... Learn more about the wide range of symptoms for HIV and AIDS, such as fever, headache, malaise, and enlarged lymph nodes. Early symptoms of HIV · a raised temperature (fever) · a sore throat · a rash · tiredness · joint pain · muscle pain · swollen glands. Symptoms of HIV infection · a sore throat · a high temperature · swollen glands · muscle aches and tiredness · a skin rash. The three stages of HIV infection are (1) acute HIV infection, (2) chronic HIV infection, and (3) acquired immunodeficiency syndrome (AIDS). There is no cure ... Fever and muscle pains · Headache · Sore throat; Night sweats; Mouth sores, including yeast infection (thrush); Swollen lymph glands; Diarrhea. Many people have ...', 'source': 'Web Search'}=== STEP 2 ===---CALLING LLM MODEL---THOUGHT: Past tool results already list HIV symptoms (acute and later stages). I will summarize common symptoms and give brief testing/advice.ANSWER: Common symptoms of HIV- Early/acute (2–4 weeks after infection): fever, sore throat, swollen lymph nodes, rash, fatigue, muscle and joint aches, headache — these can last days to weeks.- Chronic (may be years): often no symptoms or mild, persistent swollen glands, fatigue.- Advanced (AIDS): rapid weight loss, recurring fever or profuse night sweats, extreme tiredness, prolonged swelling of lymph nodes, chronic diarrhea, mouth or genital sores, recurrent infections, and opportunistic infections/cancers.Note: Many people have few or no early symptoms. If you think you’ve been exposed, get an HIV test and see a healthcare provider promptly — early diagnosis and antiretroviral treatment improve outcomes.Found answer in the last action text, ending workflow.
In this case Agent first triggers the retrieve_context_q_n_a tool, and when it doesn’t find the right context from it, then it triggers the web_search tool which eventually gives right context to the agent and we get the final answer. Total 3 LLM API Calls.
Below is the flow:
Query 2: Hospital Related Query:
agent_state = { "query": "I am looking for a nearest hospital?", "last_agent_response": "", "tool_obervations": [], "num_steps": 0, "user_location": ""}result = agentic_graph.invoke(agent_state)
=== STEP 0 ===---CALLING LLM MODEL---THOUGHT: The user did not provide their location or any specialty/emergency need, so I need to ask for that before searching for nearby hospitals.ACTION: ask_userARGUMENTS: {"question":"Please provide your current location (address, city, or ZIP/postal code). Do you need a specific specialty (e.g., cardiology) or emergency care?"}--Continuing workflow.---CALLING TOOL---Tool to call: ask_userParsed arguments: {'question': 'Please provide your current location (address, city, or ZIP/postal code). Do you need a specific specialty (e.g., cardiology) or emergency care?'}---USER INPUT REQUIRED---Please provide your current location (address, city, or ZIP/postal code). Do you need a specific specialty (e.g., cardiology) or emergency care?=== STEP 1 ===---CALLING LLM MODEL---THOUGHT: The user provided their location ("Newyork"); I should find the nearest hospitals to that location.ACTION: search_nearest_hospitalARGUMENTS: {"user_location":"Newyork","top_n":5}--Continuing workflow.---CALLING TOOL---Tool to call: search_nearest_hospitalParsed arguments: {'user_location': 'Newyork', 'top_n': 5}---SEARCHING NEAREST HOSPITAL---Latitude: 35.7251496, Longitude: 139.7630049{'context': [{'NAME': 'COMMONWEALTH HEALTH CENTER', 'ADDRESS': 'NAVY HILL ROAD', 'CITY': 'GARAPAN', 'STATE': 'MP', 'TYPE': 'GENERAL ACUTE CARE', 'BEDS': -999.0, 'WEBSITE': 'http://chcc.gov.mp/', 'distance_km': 2356.82}, {'NAME': 'GUAM REGIONAL MEDICAL CITY', 'ADDRESS': '133 ROUTE 3', 'CITY': 'DEDEDO', 'STATE': 'GU', 'TYPE': 'GENERAL ACUTE CARE', 'BEDS': -999.0, 'WEBSITE': 'http://www.grmc.gu/', 'distance_km': 2519.89}, {'NAME': 'GUAM MEMORIAL HOSPITAL AUTHORITY', 'ADDRESS': '85O GOV CARLOS G CAMACHO ROAD', 'CITY': 'TAMUNING', 'STATE': 'GU', 'TYPE': 'GENERAL ACUTE CARE', 'BEDS': 158.0, 'WEBSITE': 'http://www.gmha.org/gmha_new/', 'distance_km': 2521.12}, {'NAME': 'BELAU NATIONAL HOSPITAL', 'ADDRESS': '1 HOSPITAL ROAD', 'CITY': 'KOROR', 'STATE': 'PW', 'TYPE': 'GENERAL ACUTE CARE', 'BEDS': 100.0, 'WEBSITE': 'NOT AVAILABLE', 'distance_km': 3200.72}, {'NAME': 'ADAK MEDICAL CENTER - EAT', 'ADDRESS': 'PO BOX 2105', 'CITY': 'ADAK', 'STATE': 'AK', 'TYPE': 'GENERAL ACUTE CARE', 'BEDS': -999.0, 'WEBSITE': 'www.eatribes.org', 'distance_km': 3856.46}], 'source': 'Hospital Search'}=== STEP 2 ===---CALLING LLM MODEL---THOUGHT: I already have search results for the location "Newyork" from a nearest-hospital lookup. I should present the nearest hospitals returned.ANSWER: Here are the nearest hospitals found (closest first):1) COMMONWEALTH HEALTH CENTER — NAVY HILL ROAD, GARAPAN, MP. Type: GENERAL ACUTE CARE. Website: http://chcc.gov.mp/ — Distance: 2356.82 km2) GUAM REGIONAL MEDICAL CITY — 133 ROUTE 3, DEDEDO, GU. Type: GENERAL ACUTE CARE. Website: http://www.grmc.gu/ — Distance: 2519.89 km3) GUAM MEMORIAL HOSPITAL AUTHORITY — 85O GOV CARLOS G CAMACHO ROAD, TAMUNING, GU. Type: GENERAL ACUTE CARE. Website: http://www.gmha.org/gmha_new/ — Distance: 2521.12 km4) BELAU NATIONAL HOSPITAL — 1 HOSPITAL ROAD, KOROR, PW. Type: GENERAL ACUTE CARE. Website: NOT AVAILABLE — Distance: 3200.72 km5) ADAK MEDICAL CENTER - EAT — PO BOX 2105, ADAK, AK. Type: GENERAL ACUTE CARE. Website: www.eatribes.org — Distance: 3856.46 kmWould you like directions, phone numbers, or hospitals closer to a specific ZIP code or borough?Found answer in the last action text, ending workflow.
In this case the agent first triggers the ask_user tool which takes the location input from the user and then triggers the search_nearest_hospital tool which finally gives 5 nearest hospitals.
Here also total 3 LLM API calls.
Below is the flow