|
| 1 | +--- |
| 2 | +title: 'OpenAI Codex CLI' |
| 3 | +description: 'Add usage tracking, cost controls, and security guardrails to your Codex CLI deployment' |
| 4 | +--- |
| 5 | + |
| 6 | +OpenAI Codex CLI is a lightweight coding agent that runs directly in your terminal, letting you interact with AI to analyze, modify, and execute code in your projects. While Codex delivers powerful code generation and execution capabilities, Portkey adds essential enterprise controls for production deployments: |
| 7 | + |
| 8 | +- **Unified AI Gateway** - Single interface for 250+ LLMs with API key management (beyond Codex's default model options) |
| 9 | +- **Centralized AI observability**: Real-time usage tracking for 40+ key metrics and logs for every request |
| 10 | +- **Governance** - Real-time spend tracking, set budget limits and RBAC in your Codex setup |
| 11 | +- **Security Guardrails** - PII detection, content filtering, and compliance controls |
| 12 | + |
| 13 | +This guide will walk you through integrating Portkey with OpenAI Codex CLI and setting up essential enterprise features including usage tracking, access controls, and budget management. |
| 14 | + |
| 15 | +<Note> |
| 16 | + If you are an enterprise looking to use Codex CLI in your organization, [check out this section](#3-set-up-enterprise-governance-for-codex). |
| 17 | +</Note> |
| 18 | + |
| 19 | +# 1. Setting up Portkey |
| 20 | +Portkey allows you to use 250+ LLMs with your Codex CLI setup, with minimal configuration required. Let's set up the core components in Portkey that you'll need for integration. |
| 21 | + |
| 22 | +<Steps> |
| 23 | +<Step title="Create Virtual Key"> |
| 24 | +Virtual Keys are Portkey's secure way to manage your LLM provider API keys. Think of them like disposable credit cards for your LLM API keys, providing essential controls like: |
| 25 | +- Budget limits for API usage |
| 26 | +- Rate limiting capabilities |
| 27 | +- Secure API key storage |
| 28 | + |
| 29 | +To create a virtual key: |
| 30 | +Go to [Virtual Keys](https://app.portkey.ai/virtual-keys) in the Portkey App. Save and copy the virtual key ID |
| 31 | + |
| 32 | +<Frame> |
| 33 | +<img src="/images/integrations/openai/virtual-key-2.png" width="500"/> |
| 34 | +</Frame> |
| 35 | + |
| 36 | +<Note> |
| 37 | +Save your virtual key ID - you'll need it for the next step. |
| 38 | +</Note> |
| 39 | +</Step> |
| 40 | + |
| 41 | +<Step title="Create Default Config"> |
| 42 | + |
| 43 | +Configs in Portkey are JSON objects that define how your requests are routed. They help with implementing features like advanced routing, fallbacks, and retries. |
| 44 | + |
| 45 | +We need to create a default config to route our requests to the virtual key created in Step 1. |
| 46 | + |
| 47 | +To create your config: |
| 48 | +1. Go to [Configs](https://app.portkey.ai/configs) in Portkey dashboard |
| 49 | +2. Create new config with: |
| 50 | + ```json |
| 51 | + { |
| 52 | + "virtual_key": "YOUR_VIRTUAL_KEY_FROM_STEP1", |
| 53 | + "override_params": { |
| 54 | + "model": "gpt-4o" // Your preferred model name |
| 55 | + } |
| 56 | + } |
| 57 | + ``` |
| 58 | +3. Save and note the Config name for the next step |
| 59 | + |
| 60 | +<Frame> |
| 61 | +<img src="/images/integrations/config.png" width="500"/> |
| 62 | +</Frame> |
| 63 | + |
| 64 | +<Note> |
| 65 | +This basic config connects to your virtual key. You can add more advanced portkey features later. |
| 66 | +</Note> |
| 67 | +</Step> |
| 68 | + |
| 69 | +<Step title="Configure Portkey API Key"> |
| 70 | + |
| 71 | +Now create Portkey API key access point and attach the config you created in Step 2: |
| 72 | + |
| 73 | +1. Go to [API Keys](https://app.portkey.ai/api-keys) in Portkey and Create new API key |
| 74 | +2. Select your config from `Step 2` |
| 75 | +3. Generate and save your API key |
| 76 | + |
| 77 | +<Frame> |
| 78 | +<img src="/images/integrations/api-key.png" width="500"/> |
| 79 | +</Frame> |
| 80 | + |
| 81 | +<Note> |
| 82 | +Save your API key securely - you'll need it for Codex CLI integration. |
| 83 | +</Note> |
| 84 | +</Step> |
| 85 | +</Steps> |
| 86 | + |
| 87 | +# 2. Integrate Portkey with OpenAI Codex CLI |
| 88 | + |
| 89 | +Now that you have your Portkey components set up, let's connect them to OpenAI Codex CLI. The integration leverages Codex's provider configuration options to route all requests through Portkey's AI Gateway. |
| 90 | + |
| 91 | +<Note> |
| 92 | +You need your Portkey API Key from [Step 1](#1-setting-up-portkey) before going further. |
| 93 | +</Note> |
| 94 | + |
| 95 | +## Using Portkey Configuration File |
| 96 | + |
| 97 | +OpenAI Codex CLI supports a configuration file where you can specify your AI provider settings. Let's set up this configuration to use Portkey: |
| 98 | + |
| 99 | +1. Create or edit the Codex configuration file at `~/.codex/config.json`: |
| 100 | + |
| 101 | +```json |
| 102 | +{ |
| 103 | + "provider": "portkey", |
| 104 | + "model": "gpt-4o", |
| 105 | + "providers": { |
| 106 | + "portkey": { |
| 107 | + "name": "Portkey", |
| 108 | + "baseURL": "https://api.portkey.ai/v1", |
| 109 | + "envKey": "PORTKEY_API_KEY" |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +2. Set your Portkey API key as an environment variable: |
| 116 | + |
| 117 | +```shell |
| 118 | +export PORTKEY_API_KEY="your-portkey-api-key-here" |
| 119 | +``` |
| 120 | + |
| 121 | +> **Note:** This command sets the key only for your current terminal session. You can add the `export` line to your shell's configuration file (e.g., `~/.zshrc`) for persistence, or place it in a `.env` file at the root of your project. |
| 122 | +
|
| 123 | +3. Test your integration: |
| 124 | + |
| 125 | +```shell |
| 126 | +codex "explain this repository to me" |
| 127 | +``` |
| 128 | + |
| 129 | + |
| 130 | +# 3. Set Up Enterprise Governance for Codex |
| 131 | + |
| 132 | +**Why Enterprise Governance?** |
| 133 | +If you are using Codex CLI inside your organization, you need to consider several governance aspects: |
| 134 | +- **Cost Management**: Controlling and tracking AI spending across teams |
| 135 | +- **Access Control**: Managing which teams can use specific models |
| 136 | +- **Usage Analytics**: Understanding how AI is being used across the organization |
| 137 | +- **Security & Compliance**: Maintaining enterprise security standards |
| 138 | +- **Reliability**: Ensuring consistent service across all users |
| 139 | + |
| 140 | +Portkey adds a comprehensive governance layer to address these enterprise needs. Let's implement these controls step by step. |
| 141 | + |
| 142 | +**Enterprise Implementation Guide** |
| 143 | + |
| 144 | +<AccordionGroup> |
| 145 | + <Accordion title="Step 1: Implement Budget Controls & Rate Limits"> |
| 146 | +### Step 1: Implement Budget Controls & Rate Limits |
| 147 | + |
| 148 | +Virtual Keys enable granular control over LLM access at the team/department level. This helps you: |
| 149 | +- Set up [budget limits](/product/ai-gateway/virtual-keys/budget-limits) |
| 150 | +- Prevent unexpected usage spikes using Rate limits |
| 151 | +- Track departmental spending |
| 152 | + |
| 153 | +#### Setting Up Department-Specific Controls: |
| 154 | +1. Navigate to [Virtual Keys](https://app.portkey.ai/virtual-keys) in Portkey dashboard |
| 155 | +2. Create new Virtual Key for each department with budget limits and rate limits |
| 156 | +3. Configure department-specific limits |
| 157 | + |
| 158 | +<Frame> |
| 159 | +<img src="/images/integrations/openai/virtual-key-2.png" width="500"/> |
| 160 | +</Frame> |
| 161 | + |
| 162 | + </Accordion> |
| 163 | + |
| 164 | + <Accordion title="Step 2: Define Model Access Rules"> |
| 165 | +### Step 2: Define Model Access Rules |
| 166 | + |
| 167 | +As your AI usage scales, controlling which teams can access specific models becomes crucial. Portkey Configs provide this control layer with features like: |
| 168 | + |
| 169 | +#### Access Control Features: |
| 170 | +- **Model Restrictions**: Limit access to specific models |
| 171 | +- **Data Protection**: Implement guardrails for sensitive data |
| 172 | +- **Reliability Controls**: Add fallbacks and retry logic |
| 173 | + |
| 174 | +#### Example Configuration: |
| 175 | +Here's a basic configuration to route requests to OpenAI, specifically using GPT-4o: |
| 176 | + |
| 177 | +```json |
| 178 | +{ |
| 179 | + "strategy": { |
| 180 | + "mode": "single" |
| 181 | + }, |
| 182 | + "targets": [ |
| 183 | + { |
| 184 | + "virtual_key": "YOUR_OPENAI_VIRTUAL_KEY", |
| 185 | + "override_params": { |
| 186 | + "model": "gpt-4o" |
| 187 | + } |
| 188 | + } |
| 189 | + ] |
| 190 | +} |
| 191 | +``` |
| 192 | + |
| 193 | +Create your config on the [Configs page](https://app.portkey.ai/configs) in your Portkey dashboard. You'll need the config ID for connecting to Codex CLI's setup. |
| 194 | + |
| 195 | + |
| 196 | +<Note> |
| 197 | +Configs can be updated anytime to adjust controls without affecting running applications. |
| 198 | +</Note> |
| 199 | + </Accordion> |
| 200 | + |
| 201 | + <Accordion title="Step 3: Implement Access Controls"> |
| 202 | +### Step 3: Implement Access Controls |
| 203 | + |
| 204 | +Create User-specific API keys that automatically: |
| 205 | +- Track usage per user/team with the help of virtual keys |
| 206 | +- Apply appropriate configs to route requests |
| 207 | +- Collect relevant metadata to filter logs |
| 208 | +- Enforce access permissions |
| 209 | + |
| 210 | + |
| 211 | +Create API keys through: |
| 212 | +- [Portkey App](https://app.portkey.ai/) |
| 213 | +- [API Key Management API](/api-reference/admin-api/control-plane/api-keys/create-api-key) |
| 214 | + |
| 215 | + |
| 216 | +Example using Python SDK: |
| 217 | +```python |
| 218 | +from portkey_ai import Portkey |
| 219 | + |
| 220 | +portkey = Portkey(api_key="YOUR_ADMIN_API_KEY") |
| 221 | + |
| 222 | +api_key = portkey.api_keys.create( |
| 223 | + name="engineering-team", |
| 224 | + type="organisation", |
| 225 | + workspace_id="YOUR_WORKSPACE_ID", |
| 226 | + defaults={ |
| 227 | + "config_id": "your-config-id", |
| 228 | + "metadata": { |
| 229 | + "environment": "production", |
| 230 | + "department": "engineering" |
| 231 | + } |
| 232 | + }, |
| 233 | + scopes=["logs.view", "configs.read"] |
| 234 | +) |
| 235 | +``` |
| 236 | + |
| 237 | +For detailed key management instructions, see our [API Keys documentation](/api-reference/admin-api/control-plane/api-keys/create-api-key). |
| 238 | + </Accordion> |
| 239 | + |
| 240 | + <Accordion title="Step 4: Deploy & Monitor"> |
| 241 | +### Step 4: Deploy & Monitor |
| 242 | +After distributing API keys to your team members, your enterprise-ready Codex CLI setup is ready to go. Each team member can now use their designated API keys with appropriate access levels and budget controls. |
| 243 | +Apply your governance setup using the integration steps from earlier sections |
| 244 | +Monitor usage in Portkey dashboard: |
| 245 | +- Cost tracking by department |
| 246 | +- Model usage patterns |
| 247 | +- Request volumes |
| 248 | +- Error rates |
| 249 | +</Accordion> |
| 250 | + |
| 251 | +</AccordionGroup> |
| 252 | + |
| 253 | +<Check> |
| 254 | +### Enterprise Features Now Available |
| 255 | +**Codex CLI now has:** |
| 256 | + |
| 257 | +- Departmental budget controls |
| 258 | +- Model access governance |
| 259 | +- Usage tracking & attribution |
| 260 | +- Security guardrails |
| 261 | +- Reliability features |
| 262 | + |
| 263 | +</Check> |
| 264 | + |
| 265 | +# Portkey Features |
| 266 | +Now that you have enterprise-grade Codex CLI setup, let's explore the comprehensive features Portkey provides to ensure secure, efficient, and cost-effective AI operations. |
| 267 | + |
| 268 | +### 1. Comprehensive Metrics |
| 269 | +Using Portkey you can track 40+ key metrics including cost, token usage, response time, and performance across all your LLM providers in real time. You can also filter these metrics based on custom metadata that you can set in your configs. Learn more about metadata here. |
| 270 | + |
| 271 | +<Frame> |
| 272 | + <img src="/images/integrations/observability.png" width="600"/> |
| 273 | +</Frame> |
| 274 | + |
| 275 | +### 2. Advanced Logs |
| 276 | +Portkey's logging dashboard provides detailed logs for every request made to your LLMs. These logs include: |
| 277 | +- Complete request and response tracking |
| 278 | +- Metadata tags for filtering |
| 279 | +- Cost attribution and much more... |
| 280 | + |
| 281 | +<Frame> |
| 282 | +<img src="/images/llms/openai/logs.png"></img> |
| 283 | +</Frame> |
| 284 | + |
| 285 | + |
| 286 | + |
| 287 | + |
| 288 | +### 3. Unified Access to 250+ LLMs |
| 289 | + |
| 290 | +You can easily switch between 250+ LLMs. Call various LLMs such as Anthropic, Gemini, Mistral, Azure OpenAI, Google Vertex AI, AWS Bedrock, and many more by simply changing the `virtual_key` in your default `config` object. |
| 291 | + |
| 292 | + |
| 293 | +### 4. Advanced Metadata Tracking |
| 294 | +Using Portkey, you can add custom metadata to your LLM requests for detailed tracking and analytics. Use metadata tags to filter logs, track usage, and attribute costs across departments and teams. |
| 295 | + |
| 296 | +<Card title="Custom Metadata" icon="coins" href="/docs/product/ai-gateway/metadata"> |
| 297 | +</Card> |
| 298 | + |
| 299 | + |
| 300 | + |
| 301 | +### 5. Enterprise Access Management |
| 302 | + |
| 303 | +<CardGroup cols={2}> |
| 304 | +<Card title="Budget Controls" icon="coins" href="/docs/product/ai-gateway/virtual-keys/budget-limits"> |
| 305 | +Set and manage spending limits across teams and departments. Control costs with granular budget limits and usage tracking. |
| 306 | +</Card> |
| 307 | + |
| 308 | +<Card title="Single Sign-On (SSO)" icon="key" href="/docs/product/enterprise-offering/org-management/sso"> |
| 309 | +Enterprise-grade SSO integration with support for SAML 2.0, Okta, Azure AD, and custom providers for secure authentication. |
| 310 | +</Card> |
| 311 | + |
| 312 | +<Card title="Organization Management" icon="building" href="/docs/product/enterprise-offering/org-management"> |
| 313 | +Hierarchical organization structure with workspaces, teams, and role-based access control for enterprise-scale deployments. |
| 314 | +</Card> |
| 315 | + |
| 316 | +<Card title="Access Rules & Audit Logs" icon="shield-check" href="/docs/product/enterprise-offering/access-control-management#audit-logs"> |
| 317 | +Comprehensive access control rules and detailed audit logging for security compliance and usage tracking. |
| 318 | +</Card> |
| 319 | +</CardGroup> |
| 320 | + |
| 321 | + |
| 322 | +### 6. Reliability Features |
| 323 | +<CardGroup cols={3}> |
| 324 | + <Card title="Fallbacks" icon="life-ring" href="/product/ai-gateway/fallbacks"> |
| 325 | + Automatically switch to backup targets if the primary target fails. |
| 326 | + </Card> |
| 327 | + <Card title="Conditional Routing" icon="route" href="/product/ai-gateway/conditional-routing"> |
| 328 | + Route requests to different targets based on specified conditions. |
| 329 | + </Card> |
| 330 | + <Card title="Load Balancing" icon="key" href="/docs/product/ai-gateway/load-balancing"> |
| 331 | + Distribute requests across multiple targets based on defined weights. |
| 332 | + </Card> |
| 333 | + <Card title="Caching" icon="database" href="/product/ai-gateway/caching"> |
| 334 | + Enable caching of responses to improve performance and reduce costs. |
| 335 | + </Card> |
| 336 | + <Card title="Smart Retries" icon="database" href="/product/ai-gateway/retries"> |
| 337 | +Automatic retry handling with exponential backoff for failed requests |
| 338 | +</Card> |
| 339 | +<Card title="Budget Limits" icon="shield-check" href="/product/ai-gateway/virtual-keys/budget-limits"> |
| 340 | + Set and manage budget limits across teams and departments. Control costs with granular budget limits and usage tracking. |
| 341 | +</Card> |
| 342 | +</CardGroup> |
| 343 | + |
| 344 | + |
| 345 | + |
| 346 | +### 7. Advanced Guardrails |
| 347 | + |
| 348 | +Protect your Codex CLI's data and enhance reliability with real-time checks on LLM inputs and outputs. Leverage guardrails to: |
| 349 | +- Prevent sensitive data leaks |
| 350 | +- Enforce compliance with organizational policies |
| 351 | +- PII detection and masking |
| 352 | +- Content filtering |
| 353 | +- Custom security rules |
| 354 | +- Data compliance checks |
| 355 | + |
| 356 | +<Card title="Guardrails" icon="shield-check" href="/docs/product/guardrails"> |
| 357 | +Implement real-time protection for your LLM interactions with automatic detection and filtering of sensitive content, PII, and custom security rules. Enable comprehensive data protection while maintaining compliance with organizational policies. |
| 358 | +</Card> |
| 359 | + |
| 360 | +# FAQs |
| 361 | +<AccordionGroup> |
| 362 | +<Accordion title="How do I update my Virtual Key limits after creation?"> |
| 363 | + You can update your Virtual Key limits at any time from the Portkey dashboard:1. Go to Virtual Keys section2. Click on the Virtual Key you want to modify3. Update the budget or rate limits4. Save your changes |
| 364 | +</Accordion> |
| 365 | +<Accordion title="Can I use multiple LLM providers with the same API key?"> |
| 366 | + Yes! You can create multiple Virtual Keys (one for each provider) and attach them to a single config. This config can then be connected to your API key, allowing you to use multiple providers through a single API key. |
| 367 | +</Accordion> |
| 368 | +<Accordion title="How do I track costs for different teams?"> |
| 369 | +Portkey provides several ways to track team costs: |
| 370 | +- Create separate Virtual Keys for each team |
| 371 | +- Use metadata tags in your configs |
| 372 | +- Set up team-specific API keys |
| 373 | +- Monitor usage in the analytics dashboard |
| 374 | +</Accordion> |
| 375 | +<Accordion title="What happens if a team exceeds their budget limit?"> |
| 376 | +When a team reaches their budget limit: |
| 377 | +1. Further requests will be blocked |
| 378 | +2. Team admins receive notifications |
| 379 | +3. Usage statistics remain available in dashboard |
| 380 | +4. Limits can be adjusted if needed |
| 381 | +</Accordion> |
| 382 | +<Accordion title="Can I use Portkey with the open source version of Codex CLI?"> |
| 383 | +Yes! Portkey fully integrates with [OpenAI's open source Codex CLI](https://github.com/openai/codex). This gives you the flexibility to leverage both the power of open source with the enterprise controls of Portkey. |
| 384 | +</Accordion> |
| 385 | +</AccordionGroup> |
| 386 | + |
| 387 | + |
| 388 | +# Next Steps |
| 389 | + |
| 390 | + **Join our Community** |
| 391 | +- [Discord Community](https://portkey.sh/discord-report) |
| 392 | +- [GitHub Repository](https://github.com/Portkey-AI) |
| 393 | + |
| 394 | +<Note> |
| 395 | +For enterprise support and custom features, contact our [enterprise team](https://calendly.com/portkey-ai). |
| 396 | +</Note> |
0 commit comments