Your profile

One profile for every MSDEVBUILD site. Only you can see it, and the site owner if you write in.

Lesson 18 of 39
❮Prev Next❯

Agents with Retrieval, Function Calling and Memory

Module 2: GenAI & Agents ⏱ 13 min read Updated

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    30-second answer

    Give an agent the tool that matches the need. Function calling reaches live data and actions, where the model asks and your code runs the function. File search and Azure AI Search ground answers on documents. OpenAPI and MCP tools call outside services. Memory stores facts about a user, scoped per user, so a new conversation next week still knows the preference. The conversation only holds one chat.

    Simulate it » No Azure account needed

    Exam objective: 2.2.2 Build agents that integrate retrieval, function calling and conversation memory. 2.2.3 Integrate agent tools, including APIs, knowledge stores, search, content understanding and custom functions. (Skills measured as of April 16, 2026)

    Simulations

    Feel it before you read it. No account needed, nothing leaves this page.

    Azure portal: Attach file search, a function and memory to one agent Attach file search, a function and memory to one agent.
    </> Embed

    Embed this simulation

    Free for trainers, teachers and bloggers. Paste this into your page:

    Loading the portal…

    Tools come in kinds. <strong>File search</strong> reads documents, <strong>Azure AI Search</strong> reads your index, <strong>MCP</strong> and <strong>OpenAPI</strong> call outside services, and a <strong>function</strong> runs your own code.

    👆 Click the glowing item in the portal to continue

    Read the steps as text
    1. Tools come in kinds. File search reads documents, Azure AI Search reads your index, MCP and OpenAPI call outside services, and a function runs your own code.
    2. File search chunks and embeds uploaded files into a vector store. The agent searches it when a question needs the documents.
    3. Memory keeps facts about a user across conversations. Set the scope so each customer only sees their own.
    4. Try it. A policy question uses file search, an order question uses the function, and a new conversation next week still knows the preference.

    🧪 Simulation for learning. Screens are simplified versions of the Azure portal; names, prices and IDs are examples. Nothing here touches real Azure.

    😟 THE PROBLEM

    The agent can’t see order data, can’t read the policy PDFs and forgets the customer every time.

    😊 THE SOLUTION

    Give it a function for live data, file search for documents and memory for the customer.

    • Live order status✓ Function calling
    • Policy documents✓ File search or Azure AI Search
    • “I told you last week”✓ Memory scoped to the user
    🛠️ Solved with: Agent tools and memory

    Start with a question

    How does a good shop assistant know your order, the policy and you?

    Three different things are at work. She looks at the computer for where your parcel is right now. She flips the policy folder for the return rules. And she remembers you: “You like WhatsApp messages, don’t you?”

    An agent has the same three. A function reads the live order system. File search or Azure AI Search reads the policy documents. Memory remembers you the next time you come in.

    Explain it like I’m 10

    The technical explanation

    Which tool for which need

    The agent needsToolNotes
    Live data or an actionFunctionThe model asks, your code runs it, you send the result back
    Answers from documents users or apps provideFile searchVector store over uploaded files
    Answers from a curated company indexAzure AI Search (or a Foundry IQ knowledge base)Hybrid search, citations
    A REST API with a specOpenAPI toolDescribe it once, the agent calls it
    A remote tool serverMCP toolChoose allowed tools and approval
    Fields from a document or imageA Content Understanding analyzer, called from a function or toolPredictable extraction
    Facts about a user over timeMemoryScoped per user

    The function-calling loop

    conversation = openai.conversations.create()
    r = openai.responses.create(conversation=conversation.id, input="Where is order ORD-1042?")
    for item in r.output:
        if item.type == "function_call":                      # the model ASKS
            result = lookup_order(**json.loads(item.arguments))   # your code RUNS it
            r = openai.responses.create(conversation=conversation.id,
                  input=[{"type": "function_call_output", "call_id": item.call_id,
                          "output": json.dumps(result)}])    # send the result back
    print(r.output_text)

    The model never runs your code. That’s why a function is the safest way to reach private systems: you decide what it can do and you can add checks inside it.

    Memory across conversations

    A conversation holds one chat. Memory (preview as of 25 November 2026) extracts durable facts, such as “prefers WhatsApp updates”, stores them in a memory store, and retrieves them in a later chat. It stores user profile facts and chat summaries.

    tools.append(MemorySearchPreviewTool(memory_store_name="customer-memory",
                                         scope="{{$userId}}",       # one memory per customer
                                         update_delay=300))

    Set the scope so each user sees only their own memory, and set a retention period. A memory store needs a chat deployment and an embedding deployment, because recall uses search over the stored items.

    Architect level Why a function beats pasting data into the prompt

    Order data changes every minute and belongs to one customer. Putting it in the prompt up front wastes tokens and risks showing the wrong customer’s data. A function fetches only what the question needs, for the signed-in customer, at the moment of the question. It can also enforce rules, such as only returning orders for the current user ID, because your code decides what it returns.

    Real-world business scenario

    Retail

    Contoso Retail (fictional)

    The problem
    Customers repeated their preferences in every chat, and the agent couldn't see order status or read the returns policy.
    What they used
    One order-agent with a get_order_status function, file search over the policy PDFs, and a customer-memory store scoped by user ID with a 90-day retention.
    Why that choice
    Each need has its own tool: live data through code, documents through search, preferences through memory.
    The result
    The agent answered order and policy questions in one chat and remembered WhatsApp as the preferred channel in later ones.

    When to use it and when not to

    ✓ Use it when…

    • The agent needs live data, documents or user preferences it can't know on its own.
    • You want answers to cite sources.
    • Users return and should be recognised across sessions.

    ✕ Think twice when…

    • Memory for shared company knowledge. Use a knowledge base or search.
    • Letting a tool do anything. Limit what each function can do.
    • Adding every tool just in case. Each tool adds confusion and risk.

    ↔ Alternatives to consider

    • A Foundry IQ knowledge base for managed multi-source retrieval.
    • A workflow if the steps are fixed.
    • A single prompt if no tools are needed.
    Ready for more?The full lesson adds the technical explanation, when to use it, interview questions and revision notes.

    Test yourself with exercises

    5 original practice questions in the AI-103 style. Pick your answers, then check them. You will see why each option is right or wrong. Score 75% or more and this topic is marked complete.

    Question 1

    Question 1 of 5

    An agent must report the live delivery status of an order held in the company order system.

    Which capability should you add?

    Question 2

    Question 2 of 5

    Users upload a contract during a conversation and ask questions about it.

    Which tool fits?

    Question 3

    Question 3 of 5

    An agent should remember that a customer prefers WhatsApp updates in conversations that happen weeks apart.

    What should you use?

    Question 4

    Question 4 of 5

    In function calling, the model returns a request to call get_order_status.

    Which component runs the function?

    Question 5

    Question 5 of 5 · Select 2

    An agent must call a partner REST API that has an OpenAPI specification and must also ground answers on an existing Azure AI Search index.

    Which two tools should you add? Select two.

    Interview preparation

    Say your answer out loud first, then open the model answer. Interviewers care more about your reasoning than the exact words.

    Beginner Who runs a function when an agent calls it?

    Model answer

    Your code does. The model only asks, for example "call get_order_status with ORD-1042". Your app runs it, then sends the result back so the model can write the answer.

    Intermediate When do you pick file search and when Azure AI Search?

    Model answer

    File search suits documents given to the agent or a conversation, such as an uploaded contract. Azure AI Search suits a curated index the company maintains, with hybrid search and ranking.

    Experienced developer How do you stop memory leaking between customers?

    Model answer

    I scope the memory store per user, with the user ID as the scope key, so searches and updates only touch that user's items. I also set a retention period and keep an eye on what it stores.

    Scenario The agent must also call a partner API. What are the options?

    Model answer

    If the partner publishes an OpenAPI spec, I add an OpenAPI tool. If they run an MCP server, I add an MCP tool and decide which of its tools are allowed and whether approval is required. For my own logic, a function.

    Quick revision

    Key points

    • Functions reach live data and actions; your code runs them.
    • File search grounds on files; Azure AI Search on a curated index.
    • OpenAPI and MCP tools reach outside services.
    • Memory carries user facts across conversations, scoped per user.
    • The conversation only covers the current chat.

    Important terminology

    Function calling
    The model returns a request to call your function with arguments; your code runs it and sends the result back.
    File search
    An agent tool that searches files in a vector store, such as uploaded policy documents.
    Memory
    Long-term storage of facts about a user, extracted from conversations and recalled in later ones.
    Memory scope
    The key, such as a user ID, that decides whose memory is read and written.
    MCP tool
    A tool that connects an agent to a remote Model Context Protocol server and its tools.
    OpenAPI tool
    A tool that lets an agent call a REST API described by an OpenAPI specification.

    Common mistakes

    • Using memory for company documents.
    • Forgetting that your code must run the function.
    • Memory with no scope, so users could see each other's facts.

    Exam tips

    • "Live order status" = function calling.
    • "Documents users upload mid-chat" = file search.
    • "Remember preferences across sessions" = memory scoped to the user.

    Five quick revision questions

    1. 1.Who executes a function call?Show

      The application code.

    2. 2.Which tool searches uploaded files?Show

      File search.

    3. 3.What keeps facts between conversations?Show

      Memory.

    4. 4.What decides whose memory is used?Show

      The scope key.

    5. 5.Which tool calls a REST API with a spec?Show

      An OpenAPI tool.

    Official references

    Finished this lesson? Tick it off. Your progress is saved in this browser.

    Next up: Multi-Agent Orchestration

    Studying with a friend? Send them this lesson

    It's free and needs no sign-up. Their progress saves in their own browser.

    Lesson done? Make it stick with a quick gameSort It, Match the Pairs, Lightning Round and Who Am I? Earn XP and badges. Play ❯

    Install MSDEVBUILD Azure

    Free · no account · works offline

    Save the whole course for offline ›

    Hi, I’m your Nimbus

    Your study buddy. Pick what we do next.

    MSDEVBUILD · Multi-Stack Dev Build

    Sign in to MSDEVBUILD

    One account for Azure certifications and the blog: sign in here and you are signed in on both. Your XP, streak, practice deck and certificate stay in sync on every device. Optional: everything works without an account.