Skip to content
AgenticAIWiki
  1. Learn
  2. Part 7
  3. 34. Designing good tools
  4. 34.5

Lesson 34.5 · 5 of 6 · 4 min read · Reviewed 24 Nov 2026

Limiting what a tool can touch

By the end, you can

  • Limit what each tool can access.
  • Design tools so a misused call does little harm.

The model decides when to call a tool. Your code decides what the tool can do. That’s where safety belongs.

Narrow beats general

General (risky) Narrow (safer)
run_sql(query) get_order(order_id)
http_request(url, …) get_shipping_status(tracking_id)
shell(command) run_tests(path)
send_email(to, body) email_customer_about_order(order_id, template)

General tools are tempting because one tool covers many cases. But they turn any mistake, or any prompt injectionPrompt injection: An attack where text crafted by someone else makes a model ignore its instructions or take unintended actions., into a big problem.

Scope inside the tool

  • Credentials: read-only database users; tokensToken: The unit a model reads and writes: a word, part of a word, a space or a symbol, mapped to an integer id. scoped to one service.
  • Data: filter by the current customer’s ID in code, not by trusting the model.
  • Limits: cap refund amounts, result counts and file paths in the tool itself.
  • Side effects: split read tools from write tools, so you can grant reads freely and guard writes (chapter 45).

Key points

  • A tool should only reach the data and actions its job needs.
  • Scope credentials, paths and queries inside the tool, not in the prompt.
  • Prefer narrow actions (issue_refund up to a limit) to general ones (run_sql).
Hands-on labRuns in the Playground

Scope a file tool to one folder

Keep a file-reading tool inside its folder and under a size cap, whatever path it is asked for.

You will have: A read_doc() that resolves paths safely, and a log of six requests.

Steps

  1. Run the starter and see which requests get through.
  2. Resolve the path with posixpath.normpath before checking it.
  3. Refuse anything outside ROOT with a permission error.
  4. Add the size cap and run it again.

Done when

  • All three escape attempts are blocked.
  • The two real docs are read.
  • The big log returns a validation error.

Open in the Playground No API key needed.

Starter code (Python)
# Scope a file-reading tool to one folder, with a size cap, whatever path the model asks for.
import posixpath

ROOT = "/workspace/docs"
FILES = {  # an in-memory stand-in for the disk
    "/workspace/docs/guide.md": "How to reset a password ...",
    "/workspace/docs/faq/billing.md": "Invoices are sent monthly ...",
    "/workspace/secrets/.env": "NOT FOR THE MODEL",
    "/workspace/docs/big.log": "x" * 50000,
}
MAX_CHARS = 10000


def read_doc(path):
    full = ROOT + "/" + path  # TODO: use posixpath.normpath(posixpath.join(ROOT, path)) so ".." is resolved
    # TODO: refuse any resolved path that is not inside ROOT (return a permission error)
    # TODO: refuse files longer than MAX_CHARS with a validation error
    if full not in FILES:
        return {"is_error": True, "content": "validation: no such file"}
    return {"is_error": False, "content": FILES[full]}


asks = ["guide.md", "faq/billing.md", "../secrets/.env", "/workspace/secrets/.env", "faq/../../secrets/.env", "big.log"]
blocked = 0
for a in asks:
    r = read_doc(a)
    blocked += r["is_error"] and r["content"].startswith("permission")
    print(f"{a:<26} -> {'ERROR ' + r['content'] if r['is_error'] else 'ok'}")
print("blocked outside the folder:", blocked)
Show a solution
# Scope a file-reading tool to one folder, with a size cap, whatever path the model asks for.
import posixpath

ROOT = "/workspace/docs"
FILES = {  # an in-memory stand-in for the disk
    "/workspace/docs/guide.md": "How to reset a password ...",
    "/workspace/docs/faq/billing.md": "Invoices are sent monthly ...",
    "/workspace/secrets/.env": "NOT FOR THE MODEL",
    "/workspace/docs/big.log": "x" * 50000,
}
MAX_CHARS = 10000


def read_doc(path):
    full = posixpath.normpath(posixpath.join(ROOT, path))
    if full != ROOT and not full.startswith(ROOT + "/"):
        return {"is_error": True, "content": "permission: path is outside the docs folder"}
    if full not in FILES:
        return {"is_error": True, "content": "validation: no such file"}
    text = FILES[full]
    if len(text) > MAX_CHARS:
        return {"is_error": True, "content": f"validation: file is over {MAX_CHARS} characters; ask for a section"}
    return {"is_error": False, "content": text}


asks = ["guide.md", "faq/billing.md", "../secrets/.env", "/workspace/secrets/.env", "faq/../../secrets/.env", "big.log"]
blocked = 0
for a in asks:
    r = read_doc(a)
    blocked += r["is_error"] and r["content"].startswith("permission")
    print(f"{a:<26} -> {'ERROR ' + r['content'] if r['is_error'] else 'ok'}")
print("blocked outside the folder:", blocked)
Quick checkWhich tool design is safest for a support agent?
How sure are you?
Pick an answer and how sure you are