The brokerage agent#

A brokerage support agent with every guard in place. Each guard is on its own line, in the order it runs, with its action spelled out even where it is the default.

enkrypt = Enkrypt(
    enforce=True,  # False: only log what the guards would do
    max_wait=60,  # seconds a guard waits out Enkrypt's rate limit
)
no_advice = enkrypt.code_of_conduct(
    "Never give personalized investment advice.", on_violation="block"
)
user_pii = enkrypt.pii(entities=["pii", "person", "ip_address"], on_violation="redact")
tools = ["lookup_account", "fetch_page"]

agent = Agent(
    os.environ.get("EXAMPLE_MODEL", "openai:gpt-6.1-sol"),
    instructions="You are a support assistant for a brokerage.",
    # The output pii check sends the answer back; give the model a few tries.
    retries={"output": 3},
    capabilities=[
        # The prompt: guards run top to bottom; the first refusal stops the chain.
        InputGuardrail(
            guard=[
                # local: rewrite keys with a known shape
                redact_secrets,
                # secrets the regexes missed: stop the run
                enkrypt.secrets(on_violation="raise"),
                # attacks: refuse before the model runs
                enkrypt.injection_attack(on_violation="block"),
                # personal data becomes <<PERSON:1>>
                user_pii,
                # requests for investment advice: refuse
                no_advice,
            ]
        ),
        # Tools: stacked, because each ToolGuardrail takes one guard per stage.
        # Arguments are checked top to bottom, results bottom to top.
        ToolGuardrail(
            guard=enkrypt.restore,  # arguments: tools get real values
            result_guard=user_pii,  # results, second: redact personal data
            tools=tools,
        ),
        ToolGuardrail(
            # results, first: the model sees "blocked" instead of the attack
            result_guard=enkrypt.injection_attack(on_violation="block"),
            tools=tools,
        ),
        # The answer: checks first, restore last, so no check sees real values.
        # On output, block raises OutputBlocked for the caller.
        OutputGuardrail(
            guard=[
                enkrypt.toxicity(on_violation="block"),
                # personal data the model added: have it answer again
                enkrypt.pii(entities=["pii", "person"], on_violation="retry"),
                no_advice,
                # last: the user sees real values
                enkrypt.restore,
            ]
        ),
    ],
)


The tools are stand-ins:

@agent.tool_plain
def lookup_account(name: str) -> str:
    """Look up a customer's account by their full name."""
    return f"Account of {name}: locked after three failed logins."


@agent.tool_plain
def fetch_page(url: str) -> str:
    """Fetch a page from the brokerage's help center."""
    return "To unlock an account, reset the password from the login page."


Running it#

It uses a real model. Set ENKRYPTAI_API_KEY and your model provider’s credentials, and choose the model with EXAMPLE_MODEL:

EXAMPLE_MODEL=bedrock:us.anthropic.claude-sonnet-4-5-20250929-v1:0 \
    pixi run -e examples python examples/brokerage_agent.py

From a run with Claude Sonnet 4.5 on Amazon Bedrock (trimmed):

user types:    Hi, I'm John Smith. Why is my account locked, and how do I unlock it?
model saw:     Hi, I'm <<PERSON:1>>. Why is my account locked, and how do I unlock it?
model calls:   lookup_account(name="<<PERSON:1>>")
model saw:     Account of <<PERSON:1>>: locked after three failed logins.
user sees:     Hello! I can see that your account was locked after three failed login
               attempts. [...] Password Reset: You can reset your password, which will
               usually unlock your account automatically. [...]

Walkthrough#

Setting up. One Enkrypt creates every guard. enforce=True and max_wait=60 are the defaults, written out: switch enforce off to only log (Observing before enforcing); max_wait is how long a guard waits out Enkrypt’s rate limit (Rate limits). Two guards are named because they are used on more than one edge: no_advice on the prompt and the answer, user_pii on the prompt and tool results.

The prompt. The guards run top to bottom, and the first to refuse ends the chain (Chains and ordering):

  1. redact_secrets rewrites credentials with a known shape, locally.

  2. enkrypt.secrets() raises on secrets the patterns missed.

  3. enkrypt.injection_attack() refuses attacks; the model is not called.

  4. user_pii replaces names and addresses with placeholders such as <<PERSON:1>>.

  5. no_advice refuses requests for investment advice. It sees placeholders, not names.

Tools. Each ToolGuardrail takes one guard per stage, so there are two. Argument guards run in list order, result guards in reverse:

  • enkrypt.restore on arguments: lookup_account receives John Smith, not <<PERSON:1>>.

  • On results, injection_attack runs first, because its ToolGuardrail is listed last; then user_pii redacts what the tool returned.

The answer. Checks first, enkrypt.restore last, so no check sees the user’s real data:

  1. toxicity blocks toxic answers. On an answer, block raises OutputBlocked.

  2. A second pii() with retry catches personal data the model put in its answer: data detection missed on the way in, or the model added. The agent allows three output retries (retries={"output": 3}) for this.

  3. no_advice blocks investment advice.

  4. enkrypt.restore puts the user’s real values back.

Left at their defaults. on_error="block" on every guard, so an Enkrypt outage refuses rather than allows; and the in-memory placeholder store, kept per conversation. A chat that saves its history to a database needs a store next to it (Conversations and stores).