Cert EspressoLearn. Practise. Find your flow.

A LITTLE FOCUS.
A BIG NEXT STEP.

Your pace. Your space.
Oracle · Agentic AI Foundations

A LangChain agent, under the hood

Agentic AI Foundations · Lesson 5 of 20 · 26:16

YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗

Learning with these videos? Subscribe for new courses and exam-style questions.

In this lesson

Prepare for the Oracle Agentic AI Foundations Associate exam (1Z0-1157-26). In this lesson: create_agent, running an agent, the loop under the hood (AI messages with tool calls, tool messages, and the message history), the harness around it, and how the loop stops. Includes three original practice questions, with Fairhaven Furniture, a fictional furniture seller, as the example.

Study notes

The text of this lesson, to read at your own pace. Information as of November 2026.

From tools to an agent

Fairhaven's prototype now has three tools: order status, return window, and delivery slots. The model can ask for them, but something still has to run them, send back the results, and decide when to stop. In LangChain, one function builds that loop for you.

create_agent

In LangChain, an agent is a model calling tools in a loop until a task is complete. You build one with create_agent: you pass the model, the list of tools, and an optional system prompt. create_agent builds the ReAct loop for you: reason, act, observe, and repeat, so you do not write the loop by hand. Around that loop, LangChain calls everything else the harness: the prompt, the tools, and middleware, for example to ask a human to approve a high impact action.

Running the agent

You run the agent with invoke, passing a list of messages that starts with the user's question. It returns the agent's state, and the messages in that state are the full record of the run: the question, every tool call, every tool result, and the final answer. That record is how you trace and debug what the agent did.

The loop under the hood

Here's the loop under the hood, as messages. First, the human message with the question. The model answers with an AI message that has no text, but a tool_calls list: the tool's name, its arguments, and an ID. The loop looks the name up in its registry of tools, runs the function, and adds a tool message with the result, and with the same tool_call_id, so the model knows which call it answers. The model reads the whole history again, and asks for another tool, here the return window. And when an AI message has no more tool calls, its text is the final answer, and the loop stops.

How the loop stops

So the loop ends in one of two ways. The normal way: the model returns an AI message without tool calls, and its text is the final answer. The safety way: a limit on the number of steps stops an agent that keeps calling tools, so set it before production, along with timeouts. And every step is visible in the messages, or in a tracing tool, so you can see exactly why the agent stopped.

Recap

Let's recap. create_agent takes a model, tools, and a system prompt, and builds the ReAct loop for you. invoke runs it with a list of messages, and the returned messages are the full record of the run. The model asks for tools in the tool_calls of an AI message, and each result comes back as a tool message with the same tool_call_id. And the loop stops when an AI message has no tool calls, or when a step limit is reached.

Quick checks for this lesson

The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.

  1. Fairhaven wants its agent to answer in a short, polite tone, in every conversation, whatever tools it uses. Where does this instruction belong?

    • In the docstring of every tool
    • In the system_prompt of create_agent
    • In the tool_call_id of each message
    • In the model string of init_chat_model
    Show the answer

    In the system_prompt of create_agent

    The system prompt sets the agent's behavior for every conversation, and create_agent takes it as a parameter. “In the docstring of every tool” would describe what each tool does, which is how the model chooses tools, not how it talks. “In the tool_call_id of each message” is only an identifier that links a tool result to its call. And “In the model string of init_chat_model” would choose which model runs, not the tone of its answers.

  2. A Fairhaven developer writes a loop by hand. After running a tool, the code adds a tool message, but with the wrong tool_call_id. What goes wrong?

    • The result is not linked to its call
    • The tool runs twice, once for each ID
    • The chat model switches to another provider
    • The system prompt is dropped from the history
    Show the answer

    The result is not linked to its call

    The tool_call_id links a tool message to the call that it answers, so with the wrong ID, the result is not linked to the request. “The tool runs twice, once for each ID” would need the loop to run the tool again, and an ID does not trigger anything. “The chat model switches to another provider” is unrelated: the provider comes from the model string. And “The system prompt is dropped from the history” is unrelated as well: the system prompt stays in place.

  3. In a test, after two tool results, Fairhaven’s agent returns an AI message with text and no tool calls. What does the loop do next?

    • It runs the last tool again to confirm
    • It asks the model to choose another tool
    • It sends the text back as a tool message
    • It stops and returns the final answer
    Show the answer

    It stops and returns the final answer

    An AI message without tool calls is the final answer, so the loop stops and returns it. “It runs the last tool again to confirm” would repeat work that the model did not ask for. “It asks the model to choose another tool” is not how the loop works: the model decides whether it needs a tool, and here it decided that it does not. And “It sends the text back as a tool message” would mislabel the answer, because tool messages carry tool results only.