.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:
# Example — verify current install command at platform.openai.com
npm install -g @openai/codex
codex loginStore API keys in environment variables or your OS secret store — never commit them:
# 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
cd ~/src/OrderFlow
codexStart with a bounded prompt:
Add a FluentValidation validator for
CreateOrderRequestwith rules: CustomerId required, at least one line item, Quantity > 0. Rundotnet testafter.
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.csAsk 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)
- Branch — never run agents on
main - Small diff — review file-by-file
- Tests —
dotnet testmust pass; add tests for new behavior - Security — scan for hardcoded secrets, SQL string concat, disabled auth
- Dependencies — reject mystery
PackageReferenceunless justified
git checkout -b feature/codex-order-validation
# after agent run
git diff
dotnet test5. 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.jsonSDK 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/CancellationTokenin new APIs - Ask agent to run
dotnet formatif 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/prodwithout review)
Summary
- Install CLI and authenticate securely
- Start with small, testable tasks on a feature branch
- Constrain stack, patterns, and commands (
dotnet test) - Review diffs like any external contributor
Next: MCP servers in .NET · Build AI chatbot · Full .NET course