Agent Foundry
All Problems

#60. Graceful Timeout Handler

EasyError Recovery

The Problem

Your agent relies on an external API tool that occasionally takes 30+ seconds to respond. When this happens the entire application hangs — no spinner, no message, just dead silence until the call eventually completes (or the user kills the process). The tool itself is correct when it does return; the problem is that there is no timeout or fallback. Your job is to add a configurable timeout so the agent gracefully informs the user when a tool is taking too long, rather than freezing.

Examples

Example 1

User input: Look up the latest AI news

Current (bad) output: (Agent hangs for 30 seconds with no feedback, then finally returns the result — or the user force-quits.)

Expected (good) output: The agent attempts the tool call. After a reasonable timeout (e.g. 10 seconds), it responds: "The external lookup is taking longer than expected. Please try again shortly." — instead of blocking forever.

Example 2

User input: Summarize today's tech headlines

Current (bad) output: (30-second freeze, no indication anything is happening.)

Expected (good) output: If the tool responds within the timeout, the agent returns results normally. If not, it returns a clear, friendly timeout message and does not crash.

Example 3

User input: What's the weather right now?

Current (bad) output: (Unresponsive UI while the slow tool runs.)

Expected (good) output: "I wasn't able to fetch the weather in time. You can try again or check a weather site directly."

Your Task

Add a configurable timeout around the tool call so the agent:

  • Stops waiting after a set number of seconds (e.g. 10s).
  • Returns a user-friendly fallback message when the timeout fires.
  • Still returns normal results when the tool responds within the time limit.
  • Does not remove or bypass the slow tool.

Evaluation

Submissions are checked for the following:

  • Timeout is enforced: The agent does not hang indefinitely when a tool takes too long.
  • Returns friendly message: On timeout, the user sees a helpful message instead of an error or crash.
  • Fast tools still work: Tools that respond within the timeout continue to work normally.

Constraints

  • You must not remove or disable the slow tool
  • The timeout threshold must be configurable, not hardcoded
  • The agent must return a user-friendly message on timeout
  • Normal tool responses that complete in time must still work
Starter Code
import time
from langchain_openai import ChatOpenAI
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.tools import tool

@tool
def slow_api_lookup(query: str) -> str:
    """Look up information from an external API."""
    # BUG: This tool takes 30 seconds and the agent has no timeout protection
    time.sleep(30)
    return f"Results for: {query}"

llm = ChatOpenAI(model="gpt-4o-mini")

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant. Use tools to answer questions."),
    ("human", "{input}"),
    ("placeholder", "{agent_scratchpad}"),
])

agent = create_tool_calling_agent(llm, [slow_api_lookup], prompt)
executor = AgentExecutor(agent=agent, tools=[slow_api_lookup])

# Test: This will hang for 30 seconds with no feedback
result = executor.invoke({"input": "Look up the latest AI news"})
print(result["output"])
Open in Google Colab
Evaluation Criteria0/3