Agent Foundry
All Problems

#25. Nested Tool Calls

MediumTool CallingOrchestration

The Problem

Your customer service agent has two tools: find_user (looks up a user by name, returns their ID) and get_orders (fetches orders by user ID). The correct flow is: call find_user to get the ID, then pass that ID to get_orders. However, the agent frequently fails to chain these calls — it either skips find_user and hallucinates a user ID, or passes the user's name directly to get_orders (which expects an ID). Your job is to ensure the agent correctly chains the output of the first tool into the second.

Examples

Example 1

User input: Show me Alice's recent orders

Current (bad) output: The agent calls get_orders("alice") directly — passing a name instead of an ID. The tool returns "No orders found" because it doesn't recognize "alice" as a valid user ID.

Expected (good) output: The agent first calls find_user("alice") → gets USR-001, then calls get_orders("USR-001") → gets the actual orders. Responds: Alice has 2 recent orders: Order #1234 (Laptop, $999) and Order #1235 (Mouse, $29).

Example 2

User input: What did Bob order recently?

Current (bad) output: The agent hallucinates get_orders("USR-123") — an invented ID that returns no results.

Expected (good) output: The agent calls find_user("bob") → gets USR-002, then get_orders("USR-002") → returns Bob's real orders.

Your Task

  • Ensure the agent always calls find_user first to obtain the user ID.
  • The returned user ID must be correctly extracted and passed to get_orders.
  • The agent must never guess, invent, or hardcode user IDs.
  • Do not modify the tool implementations.

Evaluation

Submissions are checked for the following:

  • find_user is called first: The agent always looks up the user to get their ID before fetching orders.
  • Correct ID is passed to get_orders: The user ID from find_user is properly extracted and used.
  • No hallucinated IDs: The agent never invents or guesses user IDs.

Constraints

  • The agent must call find_user first to get the user ID
  • The user ID from find_user must be passed to get_orders
  • The agent must not hallucinate or hardcode user IDs
  • No changes to the tool implementations
Starter Code
from langchain_openai import ChatOpenAI
from langchain_core.tools import tool
from langgraph.prebuilt import create_react_agent

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

@tool
def find_user(name: str) -> str:
    """Find a user by name and return their user ID."""
    users = {"alice": "USR-001", "bob": "USR-002", "charlie": "USR-003"}
    user_id = users.get(name.lower())
    if user_id:
        return f"Found user '{name}' with ID: {user_id}"
    return f"No user found with name '{name}'"

@tool
def get_orders(user_id: str) -> str:
    """Get recent orders for a user by their user ID."""
    orders = {
        "USR-001": "Order #1234: Laptop ($999), Order #1235: Mouse ($29)",
        "USR-002": "Order #1236: Keyboard ($79), Order #1237: Monitor ($449)",
        "USR-003": "No recent orders",
    }
    return orders.get(user_id, f"No orders found for user ID: {user_id}")

# BUG: The agent fails to chain find_user -> get_orders properly
# It either hallucinates a user ID or passes the name directly to get_orders
# TODO: Ensure the agent extracts the user ID from find_user and passes it to get_orders
agent = create_react_agent(llm, [find_user, get_orders])

result = agent.invoke({"messages": [("human", "Show me Alice's recent orders")]})
print(result["messages"][-1].content)
Open in Google Colab
Evaluation Criteria0/3