AI Coding/OpenAI Codex Prompts

OpenAI Codex & ChatGPT Coding Prompts

The Codex brand has evolved, but the core insight behind it has not: structured prompts produce reliable, production-ready code while unstructured requests produce output that requires more editing than it saves. These prompts cover the full development workflow, generation, debugging, refactoring, testing, and documentation, tuned for GPT-4o and the o3/o4-mini reasoning models.

From Codex to GPT-4o: what changed and what stayed the same

OpenAI Codex, released in 2021, was the first broadly available model that could generate working code from natural language descriptions. It powered the original GitHub Copilot and demonstrated that AI-assisted coding was going to be a real productivity multiplier. By 2026, the underlying technology has advanced significantly, GPT-4o produces substantially higher quality code than Codex did, understands larger codebases, handles more complex reasoning, and is available in more capable variants like o3 and o4-mini for tasks requiring deep problem-solving.

What has not changed is the importance of prompt structure. Codex was sensitive to how you phrased requests. GPT-4o is less brittle, but it still produces dramatically better output when given clear context about the codebase, explicit constraints about behavior, and specific requirements about error handling and edge cases. The developers who get the most from these models treat prompting as a skill, they build and refine templates for recurring tasks rather than typing fresh prompts every time.

In 2026, the code generation landscape has also expanded with specialized tools. Cursor and Windsurf use multi-model architectures (mixing Claude and GPT models) for IDE-integrated workflows. GitHub Copilot has matured into a robust in-editor assistant with agent mode for larger scope tasks. The OpenAI API enables custom code generation pipelines. Understanding where each tool fits, and how prompts should be adapted for each context, is covered in the AI coding hub and the GitHub Copilot prompts guide.

Code generation: specification quality determines output quality

The single most important factor in AI code generation quality is specification precision. A vague specification, “write a function that handles user authentication”, produces generic, incomplete code that requires substantial revision. A precise specification , “write a JWT authentication middleware for Express.js that validates tokens in the Authorization header, extracts the userId from the payload, attaches it to req.user, and returns a 401 with an appropriate error message for expired tokens, invalid signatures, and missing headers separately”, produces a complete, working implementation with correct edge case handling.

The four elements of a high-quality code generation prompt: first, the technology context (language, framework version, libraries in use). Second, the functional specification (what the function or module should do, its inputs and outputs). Third, the error and edge case requirements (what should happen in non-happy-path scenarios). Fourth, the style constraints (naming conventions, type annotation requirements, async patterns, documentation style). These four elements take two to five minutes to specify and reduce post-generation editing from 30–60 minutes to 5–10 minutes.

For functions that interact with your existing codebase, provide the relevant type definitions, interface declarations, and import patterns. AI cannot reason about code it cannot see. Pasting the relevant database model, the relevant API types, or the interface the new function must satisfy produces output that integrates cleanly rather than output that demonstrates a pattern without fitting your actual architecture.

Debugging: from stack trace to root cause in minutes

AI debugging assistance has become one of the clearest productivity wins in day-to-day development. The model has seen millions of error patterns across every major language and framework, and for common errors, it identifies root causes accurately and quickly. The gap between “helpful” and “not helpful” in debugging conversations is almost entirely determined by how much context you provide.

The debugging prompt structure: paste the full error message and stack trace (not just the error message), paste the relevant code section, describe what you expected versus what happened, and note any recent changes. The stack trace is critical, it tells the model where in the call stack the error originated, which often points directly to the root cause. Error messages without stack traces give the model half the information it needs.

For TypeScript type errors specifically, one of the most time-consuming categories of errors for developers new to a typed codebase, AI is particularly useful. TypeScript errors are often precise but difficult to interpret without understanding the full type chain. Paste the error message, the type definition, and the usage site, and ask the model to explain the error in plain language before suggesting a fix. Understanding the error is more valuable than the fix alone. For a broader set of TypeScript and JavaScript coding prompts, see the AI prompts for coding library and the Replit agent prompts guide.

Test generation: coverage without the tedium

Writing tests is the most consistently under-done practice in software development, and the reasons are well-understood: tests for working code feel redundant, writing tests for edge cases requires careful thinking about scenarios that did not arise during implementation, and test writing competes with feature work for time. AI substantially lowers all three barriers.

For unit tests, the effective prompt specifies: the testing framework and any conventions (pytest, Jest, Vitest), the scenarios to cover (happy path, boundary values, error cases), how external dependencies should be handled (mock vs. real), and the naming convention to use. For API integration tests, specifying the test client and the specific HTTP scenarios (200, 404, 400, 401) produces a complete test file with minimal revision.

One underused technique: ask AI to identify edge cases you may have missed before writing the tests. Prompt: “Here is a function that [describe]. What are the edge cases and boundary conditions I should test that might not be immediately obvious?” This uses AI's pattern-recognition on millions of similar functions to surface scenarios that an individual developer might not think of. Then ask AI to generate tests for those scenarios.

Code documentation: the task developers skip and AI handles well

Documentation is the professional responsibility that most developers acknowledge is important and consistently delay. AI removes the friction almost entirely. Docstrings, README files, architecture explanations, inline comments for non-obvious logic, and changelog entries are all tasks where AI produces high-quality output with minimal input.

For docstrings, paste the function or class and specify the format (Google style, NumPy style, reStructuredText). AI generates complete docstrings including parameter types and descriptions, return values, and raised exceptions, in about 10 seconds per function. For an entire module, paste all the functions and ask for docstrings for all of them in one prompt.

For README generation, describe the project in a few sentences, list the main dependencies, and ask AI to structure a complete README with installation, usage examples, and configuration sections. The result requires customization but provides a complete framework in two minutes rather than 30. For architecture documentation that helps new engineers onboard, describe the main components and data flows and ask AI to write an onboarding-oriented explanation that covers the key design decisions.

For teams building AI-assisted development workflows, the best AI coding tools guide covers the tool landscape, and the AI tools for business hub covers how engineering teams integrate AI tools into broader organizational workflows.

Related coding resources

Frequently asked questions

What is OpenAI Codex and how does it relate to ChatGPT in 2026?
OpenAI Codex was the original code-specialized model behind GitHub Copilot (2021). By 2026, it has been superseded by GPT-4o and the o-series (o3, o4-mini), which significantly outperform Codex. When people refer to “Codex prompts” today, they mean prompts for OpenAI's code-capable models accessed through ChatGPT, the API, or GitHub Copilot. The prompting principles that worked for Codex, clear context, explicit constraints, example-driven specification, apply with even more power to the newer models.
How should I structure a prompt to generate a complete function?
Four elements produce reliable results: (1) Technology context, language, framework, libraries. (2) Functional specification, inputs, outputs, what it should do. (3) Error handling, how to handle edge cases and non-happy paths. (4) Style constraints, type hints, naming conventions, documentation format. These four elements take 2–5 minutes to specify and reduce post-generation editing from 30–60 minutes to 5–10 minutes.
What is the best way to use AI for debugging?
Paste the full error message and stack trace, the relevant code section, what you expected versus what happened, and any recent changes. The stack trace is critical, it identifies where in the call chain the error originated. Error messages without stack traces give the model half the information it needs for accurate root cause identification.
How does ChatGPT compare to GitHub Copilot and Cursor for coding?
They occupy different workflow positions. Copilot is strongest for inline autocomplete and small-scope suggestions within the editor. Cursor is stronger for multi-file context and larger-scope refactoring. ChatGPT is strongest for specification-to-implementation generation, debugging with full context, and learning. Most productive developers in 2026 use all three depending on the task.
What are the most important limits of AI code generation?
Four key limits: (1) AI cannot reason about code it cannot see, provide relevant type definitions and interfaces. (2) Hallucinated APIs, AI sometimes generates calls to methods that do not exist; verify against current documentation. (3) Security gaps, AI-generated code often lacks input validation and security hardening; review user-facing inputs. (4) Test coverage gaps, AI generates tests for scenarios you describe, not ones you missed; review generated test suites for missing edge cases.

OpenAI Codex Prompts

Expert prompts for AI-powered code generation, Python, JavaScript, SQL, APIs, debugging, refactoring, and test writing.

PythonJavaScriptSQLDebugging

Python & Data Science

Python

Data processing pipeline

Write a Python function that:
- Reads a CSV file at [filepath] using pandas
- Cleans the data: drops rows with >50% null values, fills remaining nulls with column median for numerics and "Unknown" for strings
- Normalises numeric columns to 0-1 range (min-max scaling)
- Returns a cleaned DataFrame and a summary dict with: original row count, dropped rows, columns normalised
Include type hints, docstring, and handle FileNotFoundError gracefully.
Python

Async API client with retry logic

Write an async Python function that fetches data from [API endpoint] with:
- httpx async client
- Retry logic: 3 attempts with exponential backoff (1s, 2s, 4s)
- Timeout of 10 seconds per request
- Rate limiting: max 10 requests per second
- Returns parsed JSON response or raises a custom APIError with status code and message
Include type hints and a simple usage example.
Python

Class with context manager and logging

Create a Python class DatabaseConnection that:
- Connects to PostgreSQL using psycopg2 (connection params from environment variables)
- Implements __enter__ and __exit__ for context manager usage
- Logs all queries at DEBUG level with execution time
- Logs errors at ERROR level with full stack trace
- Automatically rolls back on exception, commits on clean exit
Include an example showing a SELECT query and an INSERT with error handling.
Python

CLI tool with argparse

Write a Python CLI tool that processes [task] with these arguments:
- Required: --input (file path)
- Optional: --output (file path, defaults to stdout)
- Optional: --format (choices: json, csv, table; default: table)
- Optional: --verbose flag for detailed logging
- --version flag showing "1.0.0"
Use argparse with helpful descriptions. Include input validation with clear error messages. Add a shebang line and make it executable.

JavaScript & TypeScript

TypeScript

Type-safe API service class

Create a TypeScript service class for the [API name] REST API:
- Constructor accepts baseURL and apiKey
- Generic fetch method: fetchData<T>(endpoint: string, options?: RequestInit): Promise<T>
- Specific methods for: getUser(id: string), listItems(params: QueryParams), createItem(data: CreateItemDTO), updateItem(id: string, data: UpdateItemDTO)
- All methods return typed responses using interface definitions you write
- Error handling: throws typed APIError class with status, message, and response body
- Include JSDoc comments for each method
React

Custom hook with caching

Write a React custom hook useData<T> that:
- Fetches data from a URL parameter
- Caches results in a Map (keyed by URL) to avoid re-fetching
- Returns: { data: T | null, loading: boolean, error: Error | null, refetch: () => void }
- Accepts optional refreshInterval (ms) for automatic re-fetching
- Cleans up intervals and cancels in-flight requests on unmount
- TypeScript with generics
Include a usage example with a typed interface.
Node.js

Express middleware for auth + rate limiting

Write Express.js middleware that:
1. authenticateJWT: verifies Bearer token from Authorization header, attaches decoded payload to req.user, returns 401 for invalid/expired tokens
2. rateLimit: allows 100 requests per IP per 15 minutes using an in-memory Map, returns 429 with Retry-After header when exceeded
3. requestLogger: logs method, path, status code, and duration in ms

Show how to chain them on a router: router.use(rateLimit, authenticateJWT, requestLogger)
TypeScript. No external auth libraries, use jsonwebtoken only.
TypeScript

Generic data transformation pipeline

Write a TypeScript pipeline utility that chains transformation functions:
- type TransformFn<T, U> = (input: T) => U
- function pipe<A, B, C, D>(...fns): (input: A) => D (overloads for 2-4 stages)
- function pipeAsync<A, B>(...fns): (input: A) => Promise<B> for async transforms
- Includes error boundary: if any step throws, wrap in PipelineError with step index and original error
Show an example: parse JSON → validate schema → transform shape → format output

Debugging & Code Review

Debugging

Root cause analysis from error

I'm getting this error in my [language/framework] application:
Error: [paste full error message and stack trace]

Relevant code:
[paste the function/module where the error occurs]

Context: [what were you trying to do? what did you expect to happen?]

Please:
1. Identify the root cause (not just the symptom)
2. Explain why this error occurs conceptually
3. Provide a specific fix with the corrected code
4. Suggest a test that would catch this regression in future
5. Note any related issues in the code worth fixing
Code Review

Security-focused code review

Review this code for security vulnerabilities:
[paste code]

Focus on:
1. Injection risks (SQL, command, LDAP), are user inputs sanitised?
2. Authentication/authorisation, are endpoints properly protected?
3. Sensitive data handling, credentials, PII, tokens
4. Dependency risks, are there known-vulnerable packages?
5. Error messages, do they leak implementation details?

Rate each issue Critical/High/Medium/Low. For Critical and High issues, provide a specific fix.
Refactoring

Refactor for readability and performance

Refactor this code to improve readability and performance:
[paste code]

Priorities:
1. Extract magic numbers and strings into named constants
2. Break functions longer than 20 lines into smaller, single-purpose functions
3. Replace nested if-else with early returns or strategy pattern where appropriate
4. Identify any O(n²) or worse complexity that could be optimised
5. Improve variable/function names to be self-documenting

Show the refactored version with a brief explanation of each change made.
Documentation

Generate code documentation

Generate comprehensive documentation for this code:
[paste code]

Include:
- Module/file overview: what does this code do and why does it exist?
- For each function/method: purpose, parameters (name, type, description), return value, side effects, exceptions thrown
- Usage examples for the main public API (2-3 examples)
- Any non-obvious design decisions and why they were made
Format as [JSDoc / Python docstrings / Markdown] appropriate for this language.

Test Writing

Unit Tests

Comprehensive unit test suite

Write unit tests for this function using [pytest / Jest / RSpec]:
[paste the function]

Cover:
1. Happy path: expected inputs produce expected outputs
2. Edge cases: empty input, null/undefined, boundary values, max/min
3. Error cases: invalid types, missing required fields, values out of range
4. Side effects: verify any database calls, API calls, or file writes are made correctly

Use descriptive test names in the format "it should [expected behaviour] when [condition]".
Mock any external dependencies. Aim for >90% branch coverage.
Integration Tests

API endpoint integration tests

Write integration tests for these API endpoints:
[list endpoints: method, path, expected behaviour]

For each endpoint test:
- 200/201 success case with valid payload
- 400 validation error cases (missing fields, wrong types)
- 401 unauthorised (no token / invalid token)
- 404 not found (for endpoints with ID params)
- 500 handling: mock the database to throw and verify error response format

Use [Supertest / FastAPI TestClient / RSpec request spec]. Include test database setup and teardown.
Mocking

Mock strategy for external dependencies

I need to test [module] which depends on [list dependencies: database, external API, file system, etc.].
Write the mocking strategy:
1. For each dependency, show how to mock it in [testing framework]
2. Identify what the mock should verify vs. just stub
3. Show how to test both success and failure scenarios for each dependency
4. Explain how to avoid over-mocking (testing mocks instead of real behaviour)
5. Provide the test setup/teardown code for managing mock lifecycle
TDD

TDD workflow for a new feature

I want to build: [describe the feature]
Walk me through building it test-first (TDD):
1. Write the failing test for the simplest case
2. Write the minimum code to make it pass
3. Refactor without breaking the test
4. Write the next failing test for the next requirement
5. Repeat

Continue this cycle for: [list 3-4 key requirements of the feature]
Use [language] with [testing framework]. Keep each step minimal, this is the red-green-refactor loop.

SQL & Data Queries

SQL

Complex analytical query

Write a SQL query for [database: PostgreSQL / MySQL / BigQuery] that:
- Calculates [metric] from table [table name] with columns [list relevant columns]
- Groups by [dimensions]
- Filters to [date range / conditions]
- Ranks results using window functions
- Handles nulls appropriately
- Is optimised for large datasets (hint on index usage if relevant)

Include a brief explanation of each CTE or complex clause.
SQL

Database schema with indexes

Design a PostgreSQL schema for [use case, e.g., "a SaaS subscription billing system"].
Requirements:
- [List 3-5 entities and their key attributes]
- [List key relationships: one-to-many, many-to-many]
- [Performance requirement: e.g., "fast lookup by user_id and date range"]

Include:
- CREATE TABLE statements with appropriate data types and constraints
- Foreign key relationships
- Indexes for expected query patterns
- A brief explanation of design decisions (why UUID vs serial, why this index)
SQL

Query optimisation and explain plan

This query is running slowly (>5 seconds on ~2M rows):
[paste slow query]

Table structure: [paste CREATE TABLE or describe columns and approximate row counts]
Existing indexes: [list them]

Please:
1. Identify the likely performance bottleneck(s)
2. Suggest specific index additions or changes
3. Rewrite the query if there's a more efficient approach
4. Explain what to look for in the EXPLAIN ANALYZE output to confirm the fix works
SQL

Data migration script

Write a SQL migration script to [describe the schema change, e.g., "split the full_name column into first_name and last_name", or "add a status enum column with a default value"].

The migration should:
- Be idempotent (safe to run twice)
- Handle existing data (backfill the new column/table)
- Include a rollback script
- Add a comment explaining what the migration does and why
- Work for [PostgreSQL / MySQL] version [X]

API Integration & Architecture

REST API

REST API client with auth and pagination

Build a [language] REST API client for [service] that handles:
- Authentication: [API key header / OAuth2 bearer token / basic auth]
- Pagination: automatically fetches all pages when results exceed one page
- Rate limiting: respects the [X] requests/minute limit using a token bucket approach
- Retry: 3 retries with exponential backoff for 429 and 5xx responses
- Response parsing: typed response models for each endpoint

Endpoints needed:
- GET /[resource] (list with filters)
- GET /[resource]/{id} (get single)
- POST /[resource] (create)
- PATCH /[resource]/{id} (update)

Include type definitions/interfaces and a usage example.
WebSocket

WebSocket client with reconnection

Write a [language] WebSocket client that:
- Connects to [endpoint] with [authentication method]
- Automatically reconnects on disconnect with exponential backoff (max 5 retries)
- Maintains a message queue and replays unacknowledged messages after reconnect
- Dispatches incoming messages by type to registered handlers
- Exposes: connect(), disconnect(), send(type, payload), on(type, handler), off(type, handler)
- Logs connection lifecycle events (connected, disconnected, reconnecting, error) at appropriate log levels
Include TypeScript types if using JS/TS.
Microservices

Service-to-service communication pattern

Design the communication pattern between these microservices: [list your services]
Requirements:
- Synchronous for [list operations that need immediate response]
- Asynchronous for [list operations that can be eventual]
- Service A needs to know when Service B completes [specific operation]

Provide:
1. The recommended pattern for each communication type (REST, gRPC, message queue, event bus)
2. The message/event schema for async communications
3. How to handle partial failures and rollbacks
4. The retry and circuit breaker strategy
5. How to trace a request across all services for debugging
Database

Repository pattern implementation

Implement the Repository pattern for [entity, e.g., User, Order] in [language/framework]:
- IUserRepository interface with: findById, findByEmail, findAll(filters, pagination), create, update, delete
- PostgreSQLUserRepository implementing the interface using [ORM or raw SQL]
- InMemoryUserRepository for testing
- Factory function that returns the right implementation based on environment

The implementation should:
- Use transactions where data integrity requires it
- Log all queries in development
- Never expose raw DB errors to callers (wrap in domain errors)
- Handle optimistic locking for concurrent updates to the same record

Frequently Asked Questions

Quick Reference: Code Generation Best Practices

Always include in your prompt:

  • Programming language and version
  • Framework or library context
  • Input/output types and examples
  • Edge cases to handle explicitly
  • Error handling requirements
  • Performance or style constraints

Review generated code for:

  • Security vulnerabilities (injection, auth)
  • Missing error handling paths
  • Hardcoded credentials or values
  • Off-by-one errors and boundary cases
  • Compatibility with your language version
  • Test coverage of edge cases

Language and Framework Support

OpenAI Codex and GPT-4 code models support all major programming languages. Prompt quality matters more than language choice, the more context you provide, the better the output.

Python
JavaScript
TypeScript
Go
Rust
Java
C#
C++
Ruby
PHP
Swift
Kotlin
SQL
Bash
R
Scala

For best results with any language: specify the version (e.g. Python 3.11, Node 20, Go 1.22), mention the relevant framework, and paste real code rather than describing it abstractly.

Strongest for

Python, JavaScript, TypeScript, largest training data, most reliable output for these languages.

Works well for

Go, Rust, Java, C#, good results with clear context, but verify output carefully for idiomatic patterns.

Use with care for

Domain-specific languages, niche frameworks, or proprietary codebases, always review outputs against your own standards.

Security reminder: Never paste API keys, passwords, connection strings, or sensitive credentials into code generation prompts. Use placeholder names like YOUR_API_KEY or environment variable references, and replace them in your actual codebase. Generated code should always be reviewed before committing to production.
On hallucinated packages: Code models occasionally reference libraries or methods that do not exist. Always verify that any package name, version, or API method in generated code actually exists in the official documentation before adding it to your project.
Context window tip: For large files, paste only the relevant function or class, not the entire codebase. If you need the model to understand how multiple parts interact, describe the relationship in plain language rather than pasting everything. Targeted context produces more accurate, focused output.
Iterative refinement: Treat the first generated output as a draft, not a finished product. Follow up with: "Now add error handling for X", "Refactor this part to be more readable", or "Add a test for the edge case where the input is empty". Iteration almost always produces better results than a single long prompt.

Codex Prompt Patterns by Task Type

Generate a function
"Write a [language] function that [does X]. Accept [inputs] and return [output]. Handle [edge case] by [behaviour]."
Refactor code
"Refactor the following code to [goal, e.g. reduce complexity, use async/await]. Keep the same behaviour: [paste code]"
Add error handling
"Add error handling to this function. Catch [errors], log them with context, and return [fallback]. Do not change the happy path: [code]"
Write tests
"Write unit tests using [framework]. Cover: the happy path, empty inputs, null values, boundary conditions, and error states: [code]"
Explain code
"Explain what this code does in plain English. Then describe what happens if [edge case]. Highlight any potential bugs: [code]"
Convert languages
"Translate this [source language] code to [target language]. Preserve all logic. Use idiomatic [target language] patterns: [code]"
Write a SQL query
"Write a [dialect] query to [goal]. Tables: [schema]. Filter by [conditions]. Return [columns]. Optimise for [concern]."
Debug an error
"I'm getting this error: [message]. Here is the code: [code]. Here is the context: [what I was doing]. What is the cause and fix?"
Document code
"Add JSDoc / docstrings to every function in this file. Include: what it does, each parameter's type and purpose, what it returns, and any thrown exceptions: [code]"
Review for security
"Review this code for security vulnerabilities. Focus on: input validation, SQL injection risks, authentication logic, and any data exposed to the client: [code]"
Optimise performance
"This function is slow for large inputs. Profile it and suggest optimisations. Preserve correctness. Explain the time complexity before and after: [code]"
Migrate framework
"Migrate this [old framework] component to [new framework]. Maintain identical behaviour. Flag anything that has no direct equivalent and suggest alternatives: [code]"
Generate boilerplate
"Generate a [language] project structure for a [type of app, e.g. REST API, CLI tool]. Include: folder layout, dependency list, config files, and a working hello-world entry point."
Write a regex
"Write a regex in [language] that matches [description]. Provide: the regex, an explanation of each part, 3 examples that should match, and 3 that should not."
Code review prep
"Review this PR diff and identify: potential bugs, missing edge cases, performance concerns, style issues, and anything that should have a test: [paste diff]"
Generate mock data
"Generate 10 realistic mock records for a [entity] with these fields: [list fields and types]. Use realistic values, not Lorem ipsum. Output as JSON."
Add logging
"Add structured logging to this function using [library, e.g. pino, winston, zap]. Log: entry, exit, errors, and key decision points. Do not log sensitive values: [code]"
Design a schema
"Design a [SQL/MongoDB/Firestore] schema for a [type of application]. Include: entities, relationships, indexes for likely queries, and an explanation of your key design decisions."
Write a CLI tool
"Write a [language] CLI tool that [does X]. Include: argument parsing with --help text, sensible defaults, input validation, and human-readable error messages on failure."
Add types
"Add TypeScript types to this JavaScript file. Infer types from usage where possible. Use strict types, avoid any. Mark nullable fields explicitly: [code]"
Write a webhook handler
"Write a [language] webhook handler for [service, e.g. Stripe, GitHub]. Verify the signature, parse the payload, handle [event types], and return the correct response codes for success and failure."
Containerise an app
"Write a Dockerfile for this [language] application. Use a minimal base image, implement multi-stage builds to keep the final image small, and follow container security best practices."
Seed a database
"Write a database seed script in [language] that creates realistic test data for [describe schema]. Include variety in the data, different statuses, edge case values, and relationships between records."
Write a cron job
"Write a [language] cron job that [does X] on a [schedule, e.g. daily at midnight]. Include: idempotency so it is safe to run twice, logging, and alerting on failure."

Related Prompts

Don't stop here

What to read next

Hand-picked guides our readers explore right after this one.