<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/put-your-own-logic-inside-the-codex-agentic-loop-jowjgysff" -->

---
title: Put Your Own Logic Inside the Codex Agentic Loop | daily.dev
description: A walkthrough of Codex hooks, a mechanism for attaching custom logic to lifecycle events like SessionStart, PreToolUse, PostToolUse, Stop, and SessionEnd...
canonical: https://daily.dev/posts/put-your-own-logic-inside-the-codex-agentic-loop-jowjgysff
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Put Your Own Logic Inside the Codex Agentic Loop | daily.dev
og:description: A walkthrough of Codex hooks, a mechanism for attaching custom logic to lifecycle events like SessionStart, PreToolUse, PostToolUse, Stop, and SessionEnd...
og:url: https://daily.dev/posts/put-your-own-logic-inside-the-codex-agentic-loop-jowjgysff
og:image: https://api.daily.dev/og/posts/JowjgysFF.png
og:image:alt: Put Your Own Logic Inside the Codex Agentic Loop
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Put Your Own Logic Inside the Codex Agentic Loop

**[Towards Data Science](https://daily.dev/sources/tds)** · 8 min read · 1 upvotes · 0 comments

## Summary

A walkthrough of Codex hooks, a mechanism for attaching custom logic to lifecycle events like SessionStart, PreToolUse, PostToolUse, Stop, and SessionEnd during an agentic session. Hooks are configured with an event, a matcher, and a handler. A case study demonstrates using a Stop hook to enforce a quality gate on a deep-research task built with Codex, validating that a generated research brief contains enough sources and domain variety before the agent is allowed to finish, using a Python validation script and a hooks.json configuration.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://towardsdatascience.com/put-your-own-logic-inside-the-codex-agentic-loop>

## Questions this post answers

### How do I add a quality gate that validates Codex's output before it finishes a task?

Use a Stop hook, which fires when Codex is ready to finish its response. Configure it in .codex/hooks.json to run a command, such as a Python script that reads the last_assistant_message from stdin, checks it against your criteria, and returns a JSON object with a block decision and reason if it fails. Codex then incorporates the feedback and keeps working within the same run.

_See how daily.dev surfaces practical agent-workflow techniques like Codex hooks for builders automating quality checks._

### What are the different event types available for Codex hooks?

Codex hooks can attach to five lifecycle events: SessionStart (session begins), PreToolUse (before a tool call), PostToolUse (after a tool finishes), Stop (Codex is ready to finish its response), and SessionEnd (session ends). Each hook combines an event, an optional matcher to filter which occurrences trigger it, and a handler defining the action to run.

_Developers wiring custom agent logic can follow ongoing Codex tooling coverage on daily.dev._

### Does Codex support matchers on the Stop event to filter which tool calls trigger a hook?

No, Codex currently does not apply matchers to the Stop event, so a Stop hook configuration only needs the event and handler, without a matching rule to narrow when it runs. Matchers are instead used with events like PreToolUse, where many different tool calls can occur and a matcher lets you select only the ones you care about, such as shell commands.

_Stay ahead of these agent-framework nuances by following Codex updates on daily.dev._

## Similar posts on daily.dev

- [How I use the same Git work log with Codex and ChatGPT](https://daily.dev/posts/how-i-use-the-same-git-work-log-with-codex-and-chatgpt-dncckvdht) · Flavio Copes · 0 upvotes · 0 comments
- [Introducing Lifecycle Hooks in IBM Bob](https://daily.dev/posts/introducing-lifecycle-hooks-in-ibm-bob-ecyrrgk5v) · Niklas Heidloff · 0 upvotes · 0 comments
- [My Codex Workflow So Far and How It Keeps Me Focused](https://daily.dev/posts/my-codex-workflow-so-far-and-how-it-keeps-me-focused-oxhykk3m8) · Atomic Spin · 0 upvotes · 0 comments
- [Claude Code power user customization: How to configure hooks](https://daily.dev/posts/claude-code-power-user-customization-how-to-configure-hooks-i0akqx7ke) · Claude · 1 upvotes · 0 comments

---

Tags: [#python](https://daily.dev/tags/python), [#ai-agents](https://daily.dev/tags/ai-agents), [#prompt-engineering](https://daily.dev/tags/prompt-engineering), [#openai-codex](https://daily.dev/tags/openai-codex)

[View this post on daily.dev](https://daily.dev/posts/put-your-own-logic-inside-the-codex-agentic-loop-jowjgysff)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Put Your Own Logic Inside the Codex Agentic Loop","url":"https://daily.dev/posts/put-your-own-logic-inside-the-codex-agentic-loop-jowjgysff","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/put-your-own-logic-inside-the-codex-agentic-loop-jowjgysff"},"datePublished":"2026-08-24T18:26:40.864Z","dateModified":"2026-09-13T19:46:01.310Z","description":"A walkthrough of Codex hooks, a mechanism for attaching custom logic to lifecycle events like SessionStart, PreToolUse, PostToolUse, Stop, and SessionEnd...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/13ae203f332eb8afb24aa026c8fe59dc?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/13ae203f332eb8afb24aa026c8fe59dc?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Towards Data Science","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Towards Data Science","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/tds","url":"https://daily.dev/sources/tds"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/put-your-own-logic-inside-the-codex-agentic-loop-jowjgysff","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"python,ai-agents,prompt-engineering,openai-codex","timeRequired":"PT8M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Towards Data Science","item":"https://daily.dev/sources/tds"},{"@type":"ListItem","position":3,"name":"Put Your Own Logic Inside the Codex Agentic Loop"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/put-your-own-logic-inside-the-codex-agentic-loop-jowjgysff#faq","mainEntity":[{"@type":"Question","name":"How do I add a quality gate that validates Codex's output before it finishes a task?","acceptedAnswer":{"@type":"Answer","text":"Use a Stop hook, which fires when Codex is ready to finish its response. Configure it in .codex/hooks.json to run a command, such as a Python script that reads the last_assistant_message from stdin, checks it against your criteria, and returns a JSON object with a block decision and reason if it fails. Codex then incorporates the feedback and keeps working within the same run. See how daily.dev surfaces practical agent-workflow techniques like Codex hooks for builders automating quality checks."}},{"@type":"Question","name":"What are the different event types available for Codex hooks?","acceptedAnswer":{"@type":"Answer","text":"Codex hooks can attach to five lifecycle events: SessionStart (session begins), PreToolUse (before a tool call), PostToolUse (after a tool finishes), Stop (Codex is ready to finish its response), and SessionEnd (session ends). Each hook combines an event, an optional matcher to filter which occurrences trigger it, and a handler defining the action to run. Developers wiring custom agent logic can follow ongoing Codex tooling coverage on daily.dev."}},{"@type":"Question","name":"Does Codex support matchers on the Stop event to filter which tool calls trigger a hook?","acceptedAnswer":{"@type":"Answer","text":"No, Codex currently does not apply matchers to the Stop event, so a Stop hook configuration only needs the event and handler, without a matching rule to narrow when it runs. Matchers are instead used with events like PreToolUse, where many different tool calls can occur and a matcher lets you select only the ones you care about, such as shell commands. Stay ahead of these agent-framework nuances by following Codex updates on daily.dev."}}]}
```

