Agent Foundry
All Problems

#20. Multi-Step Tool Chain

MediumTool CallingOrchestration

The Problem

You have a research assistant agent equipped with three tools: search_articles, extract_entities, and summarize. The correct workflow is a strict pipeline: search → extract entities → summarize. However, the agent frequently skips steps — it might jump directly to summarize without searching first, or skip entity extraction entirely. The result is shallow, unreliable briefs that miss key details. Your job is to enforce the three-step sequence so every research request goes through all stages in order.

Examples

Example 1

User input: Research the latest trends in AI and give me a brief

Current (bad) output: The agent calls summarize directly with the user's query, producing a generic summary based on the LLM's memory — no real search was performed and no entities were extracted.

Expected (good) output: The agent first calls search_articles("latest trends in AI"), then passes those results to extract_entities(...), and finally feeds both into summarize(...). The final output is grounded in search results with named entities.

Example 2

User input: Give me a research brief on quantum computing startups

Current (bad) output: The agent calls search_articles and then immediately summarize, skipping entity extraction. The summary lacks specific company names and key figures.

Expected (good) output: All three tools are called in order. The summary includes specific entities (company names, founders, technologies) extracted in step two.

Your Task

  • Modify the agent's prompt or orchestration to enforce the strict tool ordering: search → extract_entities → summarize.
  • Ensure each tool's output is passed as input to the next tool in the chain.
  • The agent must never skip a step or call tools out of order.
  • Do not modify the tool implementations themselves.

Evaluation

Submissions are checked for the following:

  • All three tools are called: The agent invokes search, extract_entities, and summarize — none are skipped.
  • Tools called in correct order: The sequence is always search → extract_entities → summarize.
  • Outputs are chained between steps: Each tool receives the output of the previous tool as its input.

Constraints

  • All three tools (search, extract_entities, summarize) must be called in sequence
  • The agent must not skip any step or jump directly to summarization
  • Each tool's output must feed into the next tool as input
  • No changes to the individual tool implementations
Starter Code
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

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

@tool
def search_articles(query: str) -> str:
    """Search for articles on a topic."""
    return f"Found 3 articles about {query}: Article A discusses market trends, Article B covers recent innovations, Article C analyzes industry impact."

@tool
def extract_entities(text: str) -> str:
    """Extract key entities (people, companies, topics) from text."""
    return "Entities: [TechCorp, AI industry, John Smith, market growth, 2024 forecast]"

@tool
def summarize(text: str) -> str:
    """Summarize the given text into a concise brief."""
    return "Summary: The AI industry is experiencing rapid growth driven by TechCorp and similar companies, with analysts like John Smith forecasting continued expansion through 2024."

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a research assistant with access to search, entity extraction, and summarization tools."),
    ("human", "{input}"),
    ("placeholder", "{agent_scratchpad}"),
])

# BUG: The agent has all 3 tools but often skips steps — it jumps straight to summarize
# or calls tools out of order. It should always follow: search -> extract_entities -> summarize
tools = [search_articles, extract_entities, summarize]
agent = create_tool_calling_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools)

result = executor.invoke({"input": "Research the latest trends in AI and give me a brief"})
print(result["output"])
Open in Google Colab
Evaluation Criteria0/3