Free self-study · No account needed · Progress stays in this browser. Back it up before clearing site data or changing devices.

← Week 10: Tool Calling

Week 10 / Lesson 3

Authorizing and deduplicating tool execution

Bind permissions and approval to the exact operation and preserve retry identity.

Section 3.1 1 of 7

Overview

Focus

Bind permissions and approval to the exact operation and preserve retry identity.

Your route through the lesson

  • 3.2 Durable idempotency records
  • 3.3 Least authority and executor permissions
  • 3.4 Exact-action approval and changed parameters
  • 3.5 Put it into practice
  • 3.6 Guided comparison
  • 3.7 Approval with a changed destination

All required teaching is included here. Review selections are saved. Choose a correct answer for every question to continue; retries are unlimited. Study time and personal notes are optional.

Section 3.2 2 of 7

Durable idempotency records

Focus

Persist an operation key and parameter fingerprint before reporting deduplicated results.

Idempotency means repeated application has the same intended effect as one application. For a mutation service, a durable operation key can identify one logical request. Store the key, a fingerprint of relevant parameters, status, and result. Repeated delivery with the same key and same fingerprint returns the existing result; the same key with different parameters returns a conflict.

The fingerprint is a stable digest of a canonical representation: include operation type, target, amount, destination, caller scope, and any policy-sensitive conditions. A digest is a compact comparison value, not encryption and not permission. Sorting dictionary keys before encoding prevents mere field order from changing the fingerprint.

The operation record and effect should commit atomically where possible. Otherwise a crash between “effect happened” and “key stored” can allow duplication. A local database transaction can store a refund row and operation result together. Across external systems, use their idempotency facility or a documented reconciliation protocol. Do not claim that an in-memory set provides crash-safe deduplication across restarts.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. The same operation key arrives with a different amount. What should a deduplicating service do?
2. Why store the effect and operation record in one transaction when possible?
3. An in-memory set records seen keys, then the process restarts. What limitation appears?

Section 3.3 3 of 7

Least authority and executor permissions

Focus

Check least authority at the mutation boundary even for a duplicate request.

Least authority gives a component only the operations and resources it needs. A read-only support assistant can inspect order status without refund permission. A refund executor can restrict the caller to a particular organization and maximum amount. The model does not choose its own role.

Suppose an authenticated viewer resubmits a known operation key whose result contains private account details. Deduplication should not return that result before checking current access. A retry key is an identifier, not a bearer permission to read any stored result. Authorization applies to initial execution, status reads, and duplicate-result retrieval.

The lab’s trusted actor object supplies a permission set. The executor checks refund authority and target ownership before checking approval and committing. It also prevents a caller from using another owner’s operation key. A valid input, matching fingerprint, and existing result are all insufficient if the current caller lacks access. This keeps convenience mechanisms from becoming alternate routes around the normal security boundary.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. A viewer knows a valid refund operation key. Does that key authorize reading its private result?
2. Which permission set best fits an assistant that only answers order-status questions?
3. A request passes schema validation and matches a stored fingerprint. What still matters?

Section 3.4 4 of 7

Exact-action approval and changed parameters

Focus

Bind human approval to the exact reviewed action and invalidate changed parameters.

Human approval is a recorded decision about a specific action. It should identify the approving person, operation type, target, amount, destination, expiry, and relevant policy state. A vague “looks good” detached from parameters cannot safely authorize an arbitrary later mutation.

Consider a 500-cent refund to the original payment method for order O7. The approval record binds those fields and the caller’s scope. If the model changes the amount to 700 or changes the destination to a new account, the old approval no longer matches. The executor rejects the changed action before any effect.

Approval supplements permissions; it does not create them. A read-only caller with a copied approval record still cannot refund. Similarly, an approval may expire or become invalid when an order changes. The executor checks current conditions at execution, not only when the approval was created. In this lesson the approving actor and records are synthetic fixtures, so no real person is being asked to approve a real financial action.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. Approval covers 500 cents to the original payment method. A proposal changes the destination. What should happen?
2. A read-only caller presents a valid approval record. Is that sufficient to refund?
3. Why give an approval an expiry or state condition?

Section 3.5 5 of 7

Put it into practice

Focus

Run a persistent refund simulator and verify duplicate, changed, and denied requests.

Save week10.py in your exercise folder and run python week10.py --lesson 3. The lab creates a temporary SQLite ledger, a synthetic approval, and a durable operation record. It commits one permitted refund, closes and reopens the database, then redelivers the same logical request. The stored result is returned with no second refund.

hashlib provides standard digest functions. json.dumps with sort_keys=True gives a stable field order, and encode converts text to bytes before hashing:

import hashlib, json
action = {"order": "O7", "cents": 500, "destination": "original"}
encoded = json.dumps(action, sort_keys=True).encode("utf-8")
fingerprint = hashlib.sha256(encoded).hexdigest()
assert len(fingerprint) == 64

The hash is not encryption; the original values still need appropriate storage controls. The lab rejects changed amounts, a changed destination, an expired approval, and a read-only caller. PASS means one synthetic ledger effect and correctly denied alternatives, not a real refund or a complete production payment integration.

Download this week’s Python lab ↓

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. What does sort_keys=True help achieve before hashing a dictionary?
2. A same-key retry after reopening returns the original result and the ledger still has one row. What is demonstrated?
3. The digest has 64 hexadecimal characters. What should not be inferred?

Section 3.6 6 of 7

Guided comparison

Focus

Use the operation ledger to distinguish a duplicate from a changed action.

The completed trace submits refund-31 for order O7, 300 cents, destination original, under an actor with refund permission. Its fingerprint matches the unexpired approval. A transaction writes one refund and the committed operation result. A same-key retry after reopening the database matches the fingerprint and returns that result without another ledger row.

A second request uses refund-31 but changes the amount to 700. The stored fingerprint differs, so the result is conflict. A third request uses a new key but changes the destination while reusing the old approval. It fails approval matching. The new key avoids an idempotency conflict but does not bypass exact-action review.

Finally, a read-only caller submits the original action. Permission checking denies it before result disclosure or mutation. These are three distinct boundaries: same-key parameter consistency, current authority, and approved action identity. The report records the denial category and effect count for each, so a successful no-duplicate check cannot conceal an unauthorized new operation.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. A new operation key is used for a changed destination with an old approval. Does the new key solve the approval mismatch?
2. The same key and same parameters return a stored committed result. How many new effects should occur?
3. Why record both denial category and effect count?

Section 3.7 7 of 7

Approval with a changed destination

Focus

Invalidate approval when the destination changes after review.

A user reviews a report-send operation addressed to the internal incident mailbox. Before execution, a model-generated plan substitutes an external address that looks similar. The report body and operation name remain unchanged, but the recipient controls who receives the information, so the effect is materially different.

The completed executor compares the full action fingerprint with the approval record. The changed destination causes a mismatch and zero sends. It records a safe error that identifies the changed field without logging confidential report contents. A corrected request can reuse the original reviewed destination if all other conditions still hold; a genuinely new destination needs its own valid authority and review.

This case extends the refund lesson to information disclosure. Money is not the only sensitive effect. A destination, file path, project identifier, or audience can define the action’s risk. Binding approval to exact parameters keeps a plausible-looking plan revision from silently broadening the user’s decision. The approval check occurs at the actual send boundary, where the effect can still be prevented.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. A reviewed internal report destination changes to an external address. Which result is correct?
2. An approved report-send destination changes from an internal mailbox to an external address. Which denial log is safest while remaining useful?
3. Where must the exact-action approval check occur?

Your personal notes

Optional, ungraded, and saved only in this browser. Export a backup from the Academy homepage to keep them.

References and further reading
Ask your own AI assistant

Copy this prompt into an assistant you already use. Nothing is sent automatically, and your personal notes are not included.

Next lesson: Writing a reusable agent skill →