Prompt Engineering

Agentic Tool Use: Building Multi-Step AI Workflows with External APIs and RAG

Artificial Intelligence has evolved beyond static chat interfaces. Today, the frontier of LLM development lies in Agentic Workflows. Unlike traditional prompting where the model answers directly, agentic systems reason, plan, and execute actions using external tools. This paradigm shift allows AI to interact with databases, invoke REST APIs, and perform complex multi-step reasoning tasks that go beyond simple text generation.

The Evolution from Chatbots to Agents

In a standard chatbot setup, the LLM is the sole source of truth. If you need to check the current stock price of Apple, the model hallucinates or provides outdated information. In an agentic workflow, the model acts as a brain that controls limbs—the tools. It recognizes a need for external data, selects the appropriate tool (e.g., a financial API), executes the call, interprets the result, and synthesizes a final answer.

This architecture relies on three core components:

  • Planning: Breaking down complex user requests into manageable steps.
  • Tool Use: Interface definitions (schemas) that allow the model to call functions.
  • Feedback Loops: Using the output of a tool to inform the next step in the chain.

Integrating RAG for Grounded Reasoning

Tool use becomes significantly more powerful when combined with Retrieval-Augmented Generation (RAG). While traditional RAG retrieves static text from a vector database, agentic RAG can retrieve specific data points and then use them to query dynamic APIs. For instance, a support agent might retrieve a customer's ticket history (RAG) and then use a ticket-update tool to change the status based on the retrieved context.

To implement this, developers must structure their prompts to explicitly define the tool schema. This ensures the model understands the input and output types, reducing parsing errors and improving reliability.

Implementing Tool Definitions in Code

Below is a practical example using Python and a conceptual agent framework. We define a tool schema that the LLM can utilize to fetch weather data, demonstrating how JSON schemas guide the model's function calling capabilities.


def get_weather(location: str, unit: str = "fahrenheit") -> dict:
    """
    Fetch current weather for a given location.
    
    Args:
        location (str): The city and state, e.g., 'San Francisco, CA'
        unit (str): The temperature unit, either 'fahrenheit' or 'celsius'
    
    Returns:
        dict: A dictionary containing temperature and conditions
    """
    # Simulated API call
    return {
        "location": location,
        "temperature": 72,
        "unit": unit,
        "description": "Sunny"
    }

# Define the tool structure for the LLM
tool_definition = {
    "name": "get_weather",
    "description": "Get the current weather in a location",
    "parameters": {
        "type": "object",
        "properties": {
            "location": {
                "type": "string",
                "description": "The city and state, e.g. San Francisco, CA"
            },
            "unit": {
                "type": "string",
                "enum": ["celsius", "fahrenheit"],
                "description": "The temperature unit"
            }
        },
        "required": ["location"]
    }
}

Orchestrating Multi-Step Chains

The true power of agentic workflows emerges in multi-step chains. Consider a travel assistant agent. The user asks, "Book me a flight to London and summarize the local weather there." The agent must first invoke a flight booking API, then parse the confirmation details, and finally invoke a weather API for London. It then synthesizes both outputs into a coherent response.

To achieve this, developers should implement loops within their agent logic. The agent checks for "thought" tokens, executes tools, receives "observation" tokens, and repeats until the final answer is ready. This iterative process mimics human problem-solving and drastically reduces hallucination rates by grounding every claim in verified external data.

Conclusion

Building agentic applications requires a shift in mindset from prompt-centric design to system-centric design. By combining RAG for historical context, external APIs for real-time data, and robust tool-use schemas, developers can create AI assistants that are not just conversational, but truly productive. As the ecosystem matures, we will see more standardized frameworks emerge, making these complex workflows accessible to a broader range of engineers.

Share: