Generative AIPrompt engineeringPrompting techniques

Reason and act (ReAct) prompting

Theory

Canada's population

Report a typo

You're building an app that answers population-related questions. The goal is for the LLM to answer questions like:

What is the current population of Canada?

You'd like the LLM to reason step-by-step, fetch real-time population data, and present a concise, well-formatted answer. The user should only see the final result, but your task is to write a prompt that guides the LLM to go through the ReAct process internally. The prompt should instruct the LLM to:

  1. Follow the ReAct approach.

  2. Use only one tool, named GET_POPULATION, which performs a REST call to:

    https://api.worldpop.io/v1/population?country=CA

    The LLM should figure out the standard ISO Alpha-2 country code, like "US", "IN", or "BR" based on the country name. But in this case, we're only interested in Canada. It returns a JSON response containing the population.

  3. Require strict output formatting:

    Thought: ...
    Action: ...
    Observation: ...
    Thought: ...
    Final Answer: The current population of Canada is X.
  4. Ensure the final answer:

    • Uses the country name explicitly (not just the country code).

    • Formats the number using commas (e.g., 785,000).

    • Does not include unnecessary information (like source URLs or tool names).

  5. Specify that if an error or unexpected output occurs, the agent should gracefully end with:

    Final Answer: Sorry, I couldn't retrieve population data for Canada at the moment.
Enter a prompt (at least 10 symbols)

Create a free account to access the full topic