diff --git a/.env.example b/.env.example index e8b5f14..d8c7c37 100644 --- a/.env.example +++ b/.env.example @@ -5,6 +5,8 @@ APP_ENV=development # docker build args NEXT_PUBLIC_API_URL="http://localhost:8000/api" +AGENT_RECURSION_LIMIT=30 + # Search Engine, Supported values: tavily (recommended), duckduckgo, brave_search, arxiv SEARCH_API=tavily TAVILY_API_KEY=tvly-xxx diff --git a/src/graph/nodes.py b/src/graph/nodes.py index fedfd3b..64d3536 100644 --- a/src/graph/nodes.py +++ b/src/graph/nodes.py @@ -3,6 +3,7 @@ import json import logging +import os from typing import Annotated, Literal from langchain_core.messages import AIMessage, HumanMessage @@ -350,7 +351,31 @@ async def _execute_agent_step( ) # Invoke the agent - result = await agent.ainvoke(input=agent_input) + default_recursion_limit = 25 + try: + env_value_str = os.getenv("AGENT_RECURSION_LIMIT", str(default_recursion_limit)) + parsed_limit = int(env_value_str) + + if parsed_limit > 0: + recursion_limit = parsed_limit + logger.info(f"Recursion limit set to: {recursion_limit}") + else: + logger.warning( + f"AGENT_RECURSION_LIMIT value '{env_value_str}' (parsed as {parsed_limit}) is not positive. " + f"Using default value {default_recursion_limit}." + ) + recursion_limit = default_recursion_limit + except ValueError: + raw_env_value = os.getenv("AGENT_RECURSION_LIMIT") + logger.warning( + f"Invalid AGENT_RECURSION_LIMIT value: '{raw_env_value}'. " + f"Using default value {default_recursion_limit}." + ) + recursion_limit = default_recursion_limit + + result = await agent.ainvoke( + input=agent_input, config={"recursion_limit": recursion_limit} + ) # Process the result response_content = result["messages"][-1].content