Agents keeps going in a loop

Hello Folks,
I am trying to create a simple Agent that downloads a paper and some of the key citations from Arxiv and trying to create a presentation. However, I do see that the agent keeps going in a loop.

# Agent: You are a Junior Researcher
## Thought: Thought: I have attempted the same extraction process repeatedly without success due to an apparent issue with the execution of the tool. I will ensure that my input and execution format are accurate before running the extraction process for paper content.
## Using tool: ArXiv Multi Paper Extractor
## Tool Input: 
"{\"paper_names\": [\"Decoupled graph convolution network for inferring substitutable and complementary items\", \"Inferring networks of substitutable and complementary products\", \"Bundle recommendation in e-commerce\"]}"
## Tool Output: 
I tried reusing the same input, I must stop using this action input. I'll try something else instead.




# Agent: You are a Junior Researcher
## Thought: Thought: I will change my approach and gather the cited paper names into a clean format so that I can extract their details correctly.
## Using tool: ArXiv Multi Paper Extractor
## Tool Input: 
"{\"paper_names\": [\"Decoupled graph convolution network for inferring substitutable and complementary items\", \"Inferring networks of substitutable and complementary products\", \"Bundle recommendation in e-commerce\"]}"
## Tool Output: 
I tried reusing the same input, I must stop using this action input. I'll try something else instead.




# Agent: You are a Junior Researcher
## Thought: Thought: I need to find a way to retrieve the citation details without falling into the same repetitive execution. The plan is to execute a clean and concise extraction for the three cited papers.
## Using tool: ArXiv Multi Paper Extractor
## Tool Input: 
"{\"paper_names\": [\"Decoupled graph convolution network for inferring substitutable and complementary items\", \"Inferring networks of substitutable and complementary products\", \"Bundle recommendation in e-commerce\"]}"
## Tool Output: 
I tried reusing the same input, I must stop using this action input. I'll try something else instead.

Here is the complete code:

from crewai import Agent, Task, Crew
import os
from tools import arxiv_pdf_extractor, arxiv_paper_link_finder, arxiv_multi_paper_extractor
from crewai import LLM
from typing import List
from pydantic import BaseModel

class Citation(BaseModel):
    citation_name: str
    citation_url: str

class CrawlerOutput(BaseModel):
    paper_name: str
    paper_url: str
    paper_content: str
    citations: List[Citation]

# llm=LLM(model="ollama/phi", base_url="http://localhost:11434")
# os.environ["OPENAI_MODEL_NAME"] = 'o1-mini'
llm = LLM(model="gpt-4o")
arxiv_pdf_extractor_tool = arxiv_pdf_extractor #@tool("ArXiv PDF Extractor")
arxiv_paper_link_tool = arxiv_paper_link_finder #@tool("ArXiv Paper Link Finder")

crawler = Agent(    
    role="Paper Crawler",
    goal="Download the paper at this {link}",
    backstory="""You are a crawler who downloads the paper at the given link""",
    allow_delegation=False,
	verbose=True,
    tools=[arxiv_pdf_extractor_tool]
)

junior_researcher = Agent(
    role="You are a Junior Researcher",
    goal="Provide a full content of the paper and all the content of the cited papers.",
    backstory="""
    You are an A+ Junior Researcher who extract citations from the content provided by the Paper Crawler and then you extract content for all the cited papers You do this by searching for the cited papers using 
    the arxiv_multi_paper_extractor to extract the summary of the cited paper. Make sure you pass paper names to the arxiv_multi_paper_extractor as paper_names List[str] parameters. Make sure you only
    extract citation from the current paper and not from the cited papers.
    """,
    allow_delegation=True,
	verbose=True,
    tools=[arxiv_multi_paper_extractor]
)

researcher = Agent(
    role="Senior Researcher and Educator",
    goal="Take a look at the current paper and its citations and then use it to create a detailed slides to teach the key concepts, methodologies, validation, and results of the paper",
    backstory="""
    You are an expert in the field of computer science, artificial intelligence, machine learning, deep learning, and mathematics.
    You look at all the content provided by the Paper Crawler and the Junior Researcher and create engaging presentation slides that teach the key concepts, focus more on why and how they are implemented in the paper.
    If this is something new and novel explain how it differs from the existing methods and why it is better.
    Always include the current deficiencies in the field and how the paper is addressing them.
    """,
    allow_delegation=False,
	verbose=True,    
    llm=llm
)

# slides_creator = Agent(
#     role="Engaging Slides Creator",
#     goal="Create slides to teach the user about the paper at this {link} using the core ideas identified by the Expert Paper Researcher and the explanation provided by the Amazing Educator",
#     backstory="You are an expert at creating slides to teach about a technical paper. You are very good at making the slides engaging and interesting to watch.",
#     allow_delegation=False,
# 	verbose=True
# )

crawl = Task(
    description=(
        """ 1. Download the paper at this {link}
            2. Provide full content of the paper and top 3 citations to other papers that are refered in the context of the topic            
        """        
    ),
    expected_output="""Full paper content and citations""",
    agent=crawler,
    verbose=True,
    output_schema=CrawlerOutput
)

cited_papers_researcher = Task(    
    description="""Take the content provided by the crawler
    1. Use the content provided by the crawler to extract the citations
    2. Use the arxiv_multi_paper_extractor to extract all the content of the top 5 cited papers. Make sure you pass the citation name to the arxiv_multi_paper_extractor as paper_names List[str] parameters
    3. Dont extract citations from the cited papers
    4. Return the content of the current paper and all the content of the cited papers.
    """,
    expected_output="""Full content of the current paper, along with abstract and summary of the cited papers separated by '-------'""",    
    verbose=True,
    agent=junior_researcher,
)

research = Task(
    description="""Take the content provided by the crawler and junior researcher
    1. Understand the whole paper and the citations that are refered in the context of the topic        
    2. Create a detailed slides to teach the key concepts, explain how they are implemented in the paper and what results they got. Double down on techniques and technical details. Include any differences from the existing methods and why it is better.
    3. Include results and validation in the slides and explain them in detail.
    4. Have 4 to 5 bullet points in each slide and make it as engaging as possible.
    5. Include how this paper overcomes the current deficiencies in the field and how it is better than the existing methods.
    6. Important: Don't say they discussed about this in the paper. Extract that topic from the paper and include it in the slides.
    7. Include a couple of slides that extract content from the cited papers and explain them in detail and how they are related to the current paper's topic.
    """,
    expected_output="""Provide a high level overview of the paper and the core contribution. Please generate content for a presentation don't include more than 15 slides.
    Include content which you think are important things readers should know about the paper. Use storytelling techniques or other frameworks to make it more engaging. 
    Use first person POV to tell the story. Provide the content in a markdown format that I can just copy and paste into the slides.""",
    verbose=True,
    agent=researcher,
    llm=llm
    
)


# research = Task(
#     description="""Take the content provided by the crawler and junior researcher
#     1. Understand the whole paper and the citations that are refered in the context of the topic        
#     2. Create a detailed slides to teach the key concepts, explain how they are implemented in the paper and what results they got. Double down on techniques and technical details. Include any differences from the existing methods and why it is better.
#     3. Include results and validation in the slides and explain them in detail.
#     4. Have 4 to 5 bullet points in each slide and make it as engaging as possible.
#     5. Here is the how I want the slides to be structured:
#         Slide 1: Title of the paper and authors
#         Slide 2: Abstract
#         Slide 3: Introduction        
#         Slide 4: Problem Statement
#         Slide 5: Current Deficiencies in the field
#         Slide 6: Proposed Solution
#         Slide 7: How it works
#         Slide 8: Detailed Technical Breakdown and techniques
#         Slide 9: Any Charts or Graphs
#         Slide 10: Experiments and Results
#         Slide 11: Analysis
#         Slide 12: Conclusion and Future Work
#     """,
#     expected_output="""A detailed 12 slides presentation that teaches the key concepts,key techniques, detailed implementation, and results""",
#     verbose=True,
#     agent=researcher    
    
# )

crew = Crew(
    agents=[crawler, junior_researcher, researcher],
    tasks=[crawl, cited_papers_researcher, research],
    verbose=True,
    memory=True
)

result = crew.kickoff(inputs={"link": "https://arxiv.org/pdf/2402.03277"})

Hi, I am also seeing the same problem.

I am trying to create a simple agent that analyzes the data provide in a csv file. Looks like that the agent goes into a loop with the following output repeated.

Can someone please help?

----------------------- start of output -------------------------------------------
Use the following format:

Thought: you should always think about what to do
Action: the action to take, only one name of [Read a file’s content, Code Interpreter], just the name, exactly as it’s written.
Action Input: the input to the action, just a simple python dictionary, enclosed in curly braces, using " to wrap keys and values.
Observation: the result of the action

Once all necessary information is gathered:

Thought: I now know the final answer
Final Answer: the final answer to the original input question
Analyze Data and Generate Insights
"{"name": "./nobel.csv", "args_schema": {}}"e[00m
-------------------------- end of output ------------------------------------

I encountered the same problem. When I set allow_code_execution=True , an infinite loop occurred. I guess this is because the prompt prepared by crewai highlighted tool usage, so the model thought it was necessary to use the tool.

Still no answer from your tests ? I’m having the same issue with summarizing a pdf.

Can you give a bit more detail on what you are doing and the errors you are getting. The exact same ones mentioned above?

Yes this exact message : “I tried reusing the same input, I must stop using this action input. I’ll try something else instead.”

I wrote a tool to extract some text from a PDF. The first time, the agent uses the tool correctly and output the text but do nothing else (I asked for a summary). Then, the agent keep looping with the message without using the tool anymore. At some point, after tens of loops, the agent says it stopped because it was not able to use the tool. This message is also weird as it was able to extract the text on the first try.

Which model are you using?

I’m using ollama loacally with llama3.2

so Ollama models are a hit or miss with tool calling. can you try the same with gpt-4o-mini and let me know if it still throws the same errors. beyond this, we can try different techniques of processing the paper like using knowledge in crewai (Knowledge - CrewAI).

you can write a function to download the paper from arxiv and decorate it with @before_kickoff since you really don’t need an agent for that. After that, the first agent takes in the paper and uses PDFKnowledgeSource class to index and embed the paper.
let me know if this helpful.

Is the returned text longer than 2048 tokens? If that’s the case, this issue was discussed in this thread.

I will not use gpt-4o-mini (sorry) as it is non-local and cost money while I already have the computing capabilities in house.
I’m trying to do a proof of concept for local agent based LLM for people here at institut Pasteur. We do not have the right to send data outside of the company, so, even for my toy example I’ll stick with local LLMs.

I’ll try Knowledge but it seems way less effective than tools as you have to define by advance what to use and not let the agent decide. Maybe the Ollama/crewai compatibility is not mature yet as you suggested. Internally does the crewai code rely on the ollama tool calling ?

Thanks for you previous answer !

If you only want local models, can you try Qwen 2.5 models, I saw someone mentioning that they’re great at tool calling.

You might have to evaluate various local models until you get one that suitable for your use case.

Thanks
It is working fine with Qwen 2.5

1 Like