Learnixo

.NET & C# Development · Lesson 17 of 229

Getting Started with OpenAI Codex

OpenAI Codex (2026) is an agentic coding tool — it reads your repo, plans changes, runs commands, and iterates, not just autocomplete. For .NET teams already on GitHub and Azure, it's another option in the AI toolbox alongside Copilot, Cursor, and Claude Code.

Related: AI-assisted development · MCP in .NET · Course lesson


What Codex is (and isn't)

| Codex | Traditional Copilot inline | |-------|---------------------------| | Multi-step tasks across files | Single-line / block suggestions | | Can run tests & CLI | Stays in the editor | | Needs review like a junior PR | Lower blast radius per suggestion |

Senior rule: Treat output as a draft PR — you own what merges.


1. Install and authenticate

Follow the official OpenAI Codex CLI install docs for your OS (packages change frequently).

Typical flow:

Bash
# Example  verify current install command at platform.openai.com
npm install -g @openai/codex
codex login

Store API keys in environment variables or your OS secret store — never commit them:

Bash
# PowerShell
$env:OPENAI_API_KEY = "sk-..."

# bash
export OPENAI_API_KEY="sk-..."

For team projects, use org billing, usage limits, and audit logs.


2. First task in a .NET repo

Bash
cd ~/src/OrderFlow
codex

Start with a bounded prompt:

Add a FluentValidation validator for CreateOrderRequest with rules: CustomerId required, at least one line item, Quantity > 0. Run dotnet test after.

Good first tasks:

  • Add validators, DTOs, or mapping profiles
  • Write unit tests for a pure function
  • Fix a failing test with stack trace pasted
  • Update README or OpenAPI descriptions

Avoid first prompts like "rewrite the entire solution to microservices."


3. Prompting patterns that work on .NET

Give constraints

Stack: ASP.NET Core 8, EF Core, PostgreSQL.
Pattern: CQRS with MediatR — follow existing CreateOrderCommand handler.
Do not change public API routes.
Run: dotnet build && dotnet test before finishing.

Point to examples

Match error handling in OrdersController — use ProblemDetails like /src/Api/Problems/OrderProblemDetails.cs

Ask for a plan first

Plan only: list files you would change to add refresh tokens. Wait for my OK before editing.

4. Safe workflow (production mindset)

  1. Branch — never run agents on main
  2. Small diff — review file-by-file
  3. Testsdotnet test must pass; add tests for new behavior
  4. Security — scan for hardcoded secrets, SQL string concat, disabled auth
  5. Dependencies — reject mystery PackageReference unless justified
Bash
git checkout -b feature/codex-order-validation
# after agent run
git diff
dotnet test

5. Codex vs Claude Code vs Copilot

| Tool | Strength | |------|----------| | GitHub Copilot | Fast in-editor completion, PR summaries | | Claude Code | Deep reasoning, large context, terminal agent | | Codex | OpenAI ecosystem, agent loops, API parity with GPT models |

Many seniors use Copilot for speed + an agent for multi-file features.


6. .NET-specific tips

  • Include global.json SDK version in context
  • Mention nullable reference types enabled — reduces null warnings after codegen
  • For EF changes, ask for migration name and review Up/Down SQL
  • Prefer IAsyncEnumerable / CancellationToken in new APIs
  • Ask agent to run dotnet format if your repo uses it in CI

7. CI and governance

Teams often:

  • Block auto-merge on agent PRs
  • Require human review + green pipeline
  • Document allowed directories (/src, not /infra/prod without review)

Summary

  1. Install CLI and authenticate securely
  2. Start with small, testable tasks on a feature branch
  3. Constrain stack, patterns, and commands (dotnet test)
  4. Review diffs like any external contributor

Next: MCP servers in .NET · Build AI chatbot · Full .NET course