Agents and tool use

Let an agent choose tools without letting it grant itself access.

Visual lessons: 3 · 6:48 total Read estimate: 18 min Practice estimate: 45 min Updated: 2026-10-03
In this lesson · 07 of 10All lessons in this track →

Saved in this browser. View your learning.

Start with the idea

Let an agent choose tools without letting it grant itself access.

A model may propose an action. Application code must check the tool, inputs and permissions before running it. Keep limits on calls, time and repeated failures.

Agent: A system that uses model outputs to select actions and observe their results within an allowed task.

See the example and check your understanding

A made-up teaching example

The mock model requests a document as a guest. The tool blocks access. The model then claims success, but no successful tool result supports that claim.

Ask yourself

Can the answer cite a document after the read was denied?

Show the explanation

No. Use actual tool results, not the model’s claim. A repeated-read loop must also stop at its allowed call limit.

Start with these checks

  • Check permission before execution.
  • Track actual tool outcomes.
  • Enforce stop limits outside the prompt.

Use the different practice case below and check every requirement in its full instructions.

Go to the practice task ↓

The video explains the main idea. Use the sources, instructions and checks below to complete the practice.

Visual lesson1:40

Captions appear here during playback.

Agents and tool use

Let an agent choose tools without letting it grant itself access.

Download transcriptOpen video file

Source-based teaching example · Fictional data · Reviewed 2026-10-03. Microsoft Mark · locally synthesized. On a small screen, use fullscreen for diagram details or read the transcript.

Read the descriptive transcript

0:00 · What you will learn

Let an agent choose tools without letting it grant itself access.

What appears on screen

Let an agent choose tools without letting it grant itself access.

0:06 · The idea

A model may propose an action. Application code must check the tool, inputs and permissions before running it. Keep limits on calls, time and repeated failures.

What appears on screen

A model may propose an action. Application code must check the tool, inputs and permissions before running it. Keep limits on calls, time and repeated failures.

0:21 · One useful term

Agent. A system that uses model outputs to select actions and observe their results within an allowed task.

What appears on screen

A system that uses model outputs to select actions and observe their results within an allowed task.

0:31 · A worked example

The mock model requests a document as a guest. The tool blocks access. The model then claims success, but no successful tool result supports that claim.

What appears on screen

The mock model requests a document as a guest. The tool blocks access. The model then claims success, but no successful tool result supports that claim.

0:45 · Your turn to think

Can the answer cite a document after the read was denied? Pause and decide before continuing.

What appears on screen

Can the answer cite a document after the read was denied? Pause the video if you need more time.

1:02 · Why this answer works

No. Use actual tool results, not the model’s claim. A repeated-read loop must also stop at its allowed call limit.

What appears on screen

No. Use actual tool results, not the model’s claim. A repeated-read loop must also stop at its allowed call limit.

1:13 · Check 1

Check permission before execution.

What appears on screen

Check permission before execution.

1:18 · Check 2

Track actual tool outcomes.

What appears on screen

Track actual tool outcomes.

1:23 · Check 3

Enforce stop limits outside the prompt.

What appears on screen

Enforce stop limits outside the prompt.

1:29 · Now try a different case

Open the lesson and try its different practice case. Use every check in the full task. Watching alone does not mark the lesson complete.

What appears on screen

Open the lesson Try the new case Check and revise The full task and its requirements are in the linked lesson.

Watching explains the example. Complete the lesson practice to check what you can do. Explore all visual lessons →

Lesson preparation and outcomes

Prerequisites: Validated dictionaries, exceptions and status handling.

By the end, you can: Build an executor boundary and distinguish workflow ordering from agent selection.

Worked scenario and practice feedback

A mock model requests read_document for doc-1 as guest and then asserts success. The dispatcher denies access; the harness cannot cite the document because no successful observation exists. A second run repeats allowed reads and must stop at the call budget.

Guided and independent practice: Use the dispatcher and mock loop below. Independently inject cancellation and a tool result claiming new permission.

Answer criteria: No unlisted action, invalid arguments or unauthorized read executes; total-call/deadline/no-progress limits and terminal status are visible. If the local pack or interpreter is unavailable, trace the synthetic input, expected output and failure guard on paper; label execution unverified. Required practice uses no paid API calls, accounts, purchase, operational data or actual deployment.

An agentic harness permits model-selected actions within enforced boundaries. Learn when selection helps the task and how to stop unsupported or unauthorized actions.

Part 1 What an agent actually is.

An agentic harness lets a model select a next action from observations while application code governs what can execute. A goal can be precise, one tool can be sufficient, and a successful run may finish after one action. Tool count, vague goals and minimum iteration counts do not define agency.

A fixed workflow chooses its transitions in code. An agent chooses among permitted transitions using a model. Use the fixed workflow when order and rules are known; use agentic selection when observations genuinely change the next useful action. Both need bounded failure behavior.

Start with the task and unacceptable outcome. Select a model that can handle the required input and tool protocol in the approved deployment. Then design the harness: available tools, arguments, state, budgets, observations, termination and human decisions. The workflow reference separates model capability, deployment location and harness behavior. A frontier model can be used in a constrained workflow; a local model can be used in an agent; neither label settles quality or authorization.

Part 2 Validate tool requests before dispatch.

A model tool request is data to validate, not permission to act. Give each tool an exact schema with required fields, permitted enums, bounded strings and additionalProperties: false where supported. Validate again in application code. Use the authenticated actor and resource policy outside the prompt.

This complete offline dispatcher demonstrates the boundary. It makes no model, network or file call.

CATALOG = {"doc-1": "Workshop starts at 09:00."}

def execute(call, actor):
    if not isinstance(call, dict):
        return {"status": "error", "error": "invalid_arguments"}
    if call.get("name") != "read_document":
        return {"status": "error", "error": "unknown_tool"}
    args = call.get("arguments", {})
    if not isinstance(args, dict) or set(args) != {"document_id"} or set(call) != {"name", "arguments"}:
        return {"status": "error", "error": "invalid_arguments"}
    document_id = args["document_id"]
    if not isinstance(document_id, str) or not document_id.strip() or len(document_id) > 40:
        return {"status": "error", "error": "invalid_identifier"}
    if actor != "learner" or document_id not in CATALOG:
        return {"status": "error", "error": "denied"}
    return {"status": "complete", "source_id": document_id,
            "text": CATALOG[document_id]}

call = {"name": "read_document", "arguments": {"document_id": "doc-1"}}
assert execute(call, "learner")["status"] == "complete"
assert execute(call, "guest")["error"] == "denied"
assert execute({"name": "delete_document"}, "learner")["error"] == "unknown_tool"
for malformed in (None, [], {"name": "read_document", "arguments": None},
                  {"name": "read_document", "arguments": ["document_id"]}):
    assert execute(malformed, "learner")["error"] == "invalid_arguments"
print("normal, denied, unknown-tool and malformed-request checks passed")

A real loop also normalizes complete, partial, refusal, cancellation and error outcomes, preserves tool-call IDs and required provider state, validates result content and stops on deadlines, exhausted budgets or repeated lack of progress. Never assume the final block is text. Cap total tool calls as well as steps because one response may request several tools.

MCP standardizes exchange between clients and tool servers; it does not grant permission or make returned content trustworthy. Server authentication, least privilege, resource authorization, schema validation and egress policy remain application responsibilities. For consequential actions require an approval bound to actor, exact action, arguments/resource version and expiry; a changed request invalidates it. Use idempotency keys, cancellation checks and recovery records before retrying any action with effects.

Part 3 Where agents earn their cost.

Agentic selection helps a diagnostic task when the next inspection depends on the result of the previous one. A documentation investigation may need a search, then a source read, then a targeted follow-up. A comparison of two already supplied documents may only need one generation call. A known form-processing sequence may be simpler as a deterministic workflow.

Compare a fixed workflow and an agent on the same model, allowed tools, input cases and budgets. Compare models using the same harness. If you change model and harness together, you cannot attribute the result. Record success, unsupported claims, denial behavior, human interventions, tool counts and completion time; do not infer efficiency from a impressive transcript.

Part 4 Where agents fail and what to do about it.

Common failures require code-level controls:

  • Wandering: enforce a deadline, call cap and no-progress counter; return a partial result with unresolved items.
  • Tool misuse: reject invalid arguments before execution and return a typed repairable error; do not expand the tool list.
  • Fabricated success: verify result status and evidence IDs before claiming completion.
  • Wrong ordering: make prerequisites explicit in a state machine; parallelize only independent reads.
  • Duplicate effects after a timeout: reconcile recorded action status using an idempotency key; do not blindly retry.
  • Context loss: persist the task, decisions, evidence IDs and pending actions outside the model. Compaction is lossy; retain approvals and policy outside summaries.

Prompt wording can guide decisions but cannot replace enforcement. If the harness cannot verify a required outcome, stop and ask the named human decision owner. A fallback is safe only if evaluated for that task and deployment.

Interface/reference limits

Use the workflow reference to connect task, model, deployment, harness/tools, evidence and the responsible human decision. Synthetic demonstrations establish only the stated tested conditions.

Primary source checked October 2, 2026: OpenAI function calling.

Lesson status:PRACTICE REVIEW REQUIRED

Review your practiceComplete the knowledge check