← Developers
Level 1: code Framework

Python: Anthropic SDK and tool use

With tool use, the application executes the tool and returns the result. Return a structured held result so the model can request assistance instead of proceeding without the data.

Configuration and reference code for Python: Anthropic SDK and tool use

The guard

Same file as every Python runtime.

securd_guard.py
# securd_guard.py
# Held and blocked names resolve to the policy's block page address.
# Put that address (from the console, Sites > your site) in SECURD_BLOCK_ADDRS.
import os
import socket
import logging

SECURD_BLOCK_ADDRS = {a for a in os.getenv("SECURD_BLOCK_ADDRS", "").split(",") if a}
log = logging.getLogger("securd")


class SecurdHeld(Exception):
    """Destination is held at the Greywall or blocked by the policy for this agent role."""


def securd_guard(hostname: str, agent_role: str) -> None:
    try:
        answers = {info[4][0] for info in socket.getaddrinfo(hostname, 443)}
    except socket.gaierror as exc:
        log.warning("securd.unresolved", extra={"agent_role": agent_role, "host": hostname})
        raise SecurdHeld(f"{hostname} did not resolve inside {agent_role}") from exc
    if answers & SECURD_BLOCK_ADDRS:
        log.warning("securd.held", extra={"agent_role": agent_role, "host": hostname})
        raise SecurdHeld(f"{hostname} is held or blocked for {agent_role}")

Tool execution loop

loop.py
import json
from urllib.parse import urlparse
import anthropic, httpx
from securd_guard import securd_guard, SecurdHeld

client = anthropic.Anthropic()
AGENT_ROLE = "analyst-agent"

def run_tool(name: str, args: dict) -> str:
    if name == "fetch":
        host = urlparse(args["url"]).hostname or ""
        try:
            securd_guard(host, AGENT_ROLE)
        except SecurdHeld as held:
            return json.dumps({"status": "held", "reason": str(held), "host": host})
        return httpx.get(args["url"], timeout=20).text[:8000]
    return json.dumps({"status": "unknown_tool"})

Evaluate Agent DNS with your own agent traffic

Deploy on a single policy in learning mode and review the recorded destinations with your team.