Codex CLI Hooks

Akto Guardrails for Codex provides comprehensive security monitoring and validation for both chat interactions and tool executions — and works with both Codex CLI and Codex Desktop. It intercepts prompts before sending to Codex, validates tool calls before execution, blocks risky behavior, and reports all events to your Akto dashboard.

Key Features

  • Zero Installation - No standalone apps to install

  • Transparent Integration - Uses Codex's native hook mechanism (CLI and Desktop)

  • Real-time Protection - Validates every prompt and tool call

  • Centralized Monitoring - All events reported to Akto dashboard

  • Flexible Deployment - Supports Argus and Atlas modes

  • Configurable Behavior - Blocking or observation modes

  • Auto-detected API Host - Automatically resolves Codex API endpoint from environment

How It Works

Codex's hook system (shared by both CLI and Desktop) executes custom scripts at four critical points:

4 Hook Points:

  1. UserPromptSubmit - Validates prompts before sending to Codex API

  2. Stop - Ingests prompt/response pair when Codex finishes generating

  3. PreToolUse - Validates tool requests before execution (blocks if malicious)

  4. PostToolUse - Ingests tool input/output after execution (observational only)

Note: Codex currently only supports the Bash tool for PreToolUse and PostToolUse hooks (both CLI and Desktop).

File Structure

Key Files:

  • Wrapper scripts (.sh): Set environment variables, invoke Python scripts

    • ⚠️ Contains AKTO_DATA_INGESTION_URL placeholder - Must be replaced with your Akto instance URL

  • Python scripts (.py): Core validation and ingestion logic, Akto API communication

  • akto_machine_id.py: Generates unique device identifiers for Atlas mode

  • hooks.json: Links hooks to wrapper scripts

  • config.toml: Must enable the codex_hooks feature flag

Setup Guide

Prerequisites

  • Codex CLI or Codex Desktop installed

  • Akto instance URL

  • Python 3.7+

  • macOS, Linux, or Windows with bash/zsh

Installation Steps

1

Enable Codex Hooks Feature Flag

Codex hooks are experimental. Enable them in ~/.codex/config.toml (used by both CLI and Desktop):

2

Create Directories

3

Download Hook Scripts

4

Configure Akto Ingestion URL ⚠️ CRITICAL STEP

circle-exclamation

Automated replacement:

Manual replacement (alternative):

Edit each wrapper script and replace:

With:

Files to update:

  • akto-validate-prompt-wrapper.sh

  • akto-validate-response-wrapper.sh

  • akto-validate-pre-tool-wrapper.sh

  • akto-validate-post-tool-wrapper.sh

5

Configure Hooks

Copy hooks.json to ~/.codex/hooks.json:

Note: You can also place hooks.json at <repo>/.codex/hooks.json for repository-level hooks.

6

Configure Hook Behavior (Optional)

Edit wrapper scripts to customize:

Mode Options:

  • Argus: Standard validation and reporting

  • Atlas: Includes device-specific metadata

Sync Mode:

  • true: Blocks threats (prompt validation + tool validation)

  • false: Reports but allows execution

7

Verify Installation

Check logs to confirm hooks are working:

Test by running a Codex command:

  • CLI: codex "What is 2+2?"

  • Desktop: Open Codex Desktop and send a message in the chat

You should see log entries indicating validation occurred.

Configuration Reference

Wrapper Script Variables

Environment Variables (Optional)

Override defaults via environment variables in ~/.zshrc or ~/.bashrc:

Then reload your shell:

Codex API Host Auto-Detection

The Codex API host and path are automatically resolved from the same environment variables Codex CLI uses:

Scenario
Host
Path

OPENAI_BASE_URL set

value of OPENAI_BASE_URL

/v1/responses

OPENAI_API_KEY set

api.openai.com

/v1/responses

ChatGPT browser login

chatgpt.com

/backend-api/codex/responses

Hook Input Fields

All hooks receive a common JSON payload on stdin, plus event-specific fields:

Event
Additional Fields

UserPromptSubmit

prompt

Stop

last_assistant_message, stop_hook_active

PreToolUse

tool_name, tool_use_id, tool_input

PostToolUse

tool_name, tool_use_id, tool_input, tool_response

Troubleshooting

Hooks Not Executing

Ingestion URL Not Configured

Check Logs for Errors

Events Not in Dashboard

Service Unavailable

If Akto is unreachable:

  • With AKTO_SYNC_MODE=true: hooks fail open and allow execution (fail-safe)

  • With AKTO_SYNC_MODE=false: hooks skip ingestion silently

Uninstallation

To completely remove Akto hooks from Codex CLI or Codex Desktop:

Complete Removal

Selective Removal (Keep Logs)

Backup Before Removal

Verify Removal

Restore Codex to Default

After uninstallation, Codex CLI and Codex Desktop will operate without Akto security monitoring. Test with:

  • CLI: codex "Test message"

  • Desktop: Open Codex Desktop and send a message — no hook logs should appear

Enterprise Deployment

Automated Deployment Script

Deploy to developers:

Quick Setup Summary

Resources

Last updated