Skip to Content

What an AI agent really is

An agent is a loop. It thinks, acts, observes, and goes round again until it can answer. Watch one full turn of a real ADK agent.

8 minFree

The agent loop
  1. Think
  2. Act
  3. Observe
  4. Answer
FIG. AGENT-LOOP / 06 PARTSYour appuser messageRunnerdrives the turnSessionevent historyLlmAgentweather_agentGeminithe LLMget_weatherPython function

Press Play to watch it run, or step through with ← →. Then switch to Practice and run it yourself.

00/12
import asyncio

from google.adk.agents.llm_agent import Agent
from google.adk.runners import Runner
from google.adk.sessions import InMemorySessionService
from google.genai import types


def get_weather(city: str) -> dict:
    """Returns the current weather for a city."""
    return {"status": "success", "city": city, "temp_c": 31, "condition": "humid"}


root_agent = Agent(
    name="weather_agent",
    model="gemini-flash-latest",
    description="Answers questions about the weather.",
    instruction="Answer weather questions. Use get_weather for live data.",
    tools=[get_weather],
)


async def main(question: str):
    sessions = InMemorySessionService()
    await sessions.create_session(app_name="demo", user_id="u1", session_id="s1")
    runner = Runner(agent=root_agent, app_name="demo", session_service=sessions)

    message = types.Content(role="user", parts=[types.Part(text=question)])
    async for event in runner.run_async(
        user_id="u1", session_id="s1", new_message=message
    ):
        if event.is_final_response():
            print(event.content.parts[0].text)


asyncio.run(main("Weather in Chennai?"))

Session events

Empty. Events appear here as the Session records them.

Background: the idea in plain words1 min read

A chatbot sends your message to a model and shows the reply. That works for general questions. But a model only knows what it learned in training, so alone it can't tell you today's weather or check an order.

An agent can also do things: call an API (a way for one program to ask another for data or actions), query a database, run code. The trick is that the model never does them itself. It asks, your program does the work, and the result goes back to the model. Each thing an agent can do is called a tool.

That cycle is the agent loop:

  • Think: the model reads the conversation and decides what to do next.
  • Act: if it needs information or an action, it asks for a tool call, and your code runs the tool.
  • Observe: the tool's result is added to the conversation.

A tiny example. You ask "Weather in Chennai?" Think: the model does not know today's weather, so it asks for the get_weather tool with the city Chennai. Act: your code runs the tool. Observe: the result, 31°C and humid, is added to the conversation. Think again: now the model has what it needs, so it answers in plain text.

Some questions need no tool. For "Capital of France?" the model already knows the answer, so it replies in the first round.

The loop repeats until the model can answer in plain text. Press Play below, then switch the Try it selector to see a question that needs no tool at all.

What to notice

  1. The model never runs your code. It returns a function_call, which is a structured request. ADK runs get_weather for you.
  2. Tools are plain Python functions. ADK reads the function name, parameters and docstring, and turns them into a description the model can understand. A clear docstring helps the model pick the right tool.
  3. Everything becomes an Event. The user's message, the tool call, the tool's result and the final answer are all saved to the session. That history is what the model sees on its next round.
  4. The number of loops isn't fixed. "Capital of France?" takes one round. "Weather in Chennai?" takes two. A harder task might call several tools in a row.
Go deeper

Words to know

1 min read
WordMeaning in simple words
AgentA model plus a program that lets it use tools, running in a loop
ToolSomething the agent can use, such as a function that fetches the weather. Your code runs it
Agent loopThink, act, observe, repeated until the model can answer
function_callThe model's request to run a tool. It is a request, not an action
SessionThe stored history of one conversation
EventOne entry in that history: a user message, a tool call, a tool result or an answer

Check your understanding

1. Who actually runs get_weather?
2. Why does the model get called twice for “Weather in Chennai?”
3. Where does the agent keep the conversation history?
Sources: the official documentation2 links

This lesson is based on the official documentation. Check it for the latest details.

Something wrong or unclear in this lesson? Tell us, it gets fixed.