Guides›OpenAI Integration
OpenAI Integration
Securing your OpenAI API keys with AgentSecrets prevents prompt injection attacks, accidental debugging leaks, and compromised dependencies from ever reading your LLM credentials.
1Store your OpenAI Key (Auto Local + Cloud Sync)
Save your OpenAI secret key to your OS Keychain:
agentsecrets secrets set OPENAI_API_KEY=sk-proj-12345...
This automatically saves the secret to your local OS Keychain and synchronizes the encrypted ciphertext to your cloud workspace.
2Allowlist the OpenAI Domain
Authorize OpenAI in your workspace allowlist:
agentsecrets workspace allowlist add api.openai.com
3Using the Official OpenAI Python SDK
With Transparent HTTP Client Interception, you can use the official openai package directly:
import openai from agentsecrets import init, credential # 1. Enable interception once at startup init() # 2. Pass the zero-knowledge credential placeholder client = openai.OpenAI(api_key=credential.OPENAI_API_KEY) # 3. Call OpenAI naturally — raw keys never enter application RAM! chat_completion = client.chat.completions.create( model="gpt-4o", messages=[ {"role": "user", "content": "Explain zero-knowledge credential proxies in one sentence."} ], ) print(chat_completion.choices[0].message.content)
Why this is secure:
- Prompt Injection Defense: Even if an LLM is tricked into dumping initialized variables or inspecting memory,
client.api_keyonly ever contains"AS_SECRET_OPENAI_API_KEY". - Automatic Injection: Real keys are injected directly into the outbound TLS stream by the local proxy.
- Stream Support: Native SDK streaming (
stream=True) works seamlessly without changes.
Alternative: Direct Proxy Calls with AgentSecrets SDK
from agentsecrets import AgentSecrets with AgentSecrets() as client: response = client.call( "https://api.openai.com/v1/chat/completions", method="POST", bearer="OPENAI_API_KEY", body={ "model": "gpt-4o", "messages": [{"role": "user", "content": "Hello!"}] } ) print(response.json())
Was this helpful?
Thanks for your feedback!
Your feedback helps us improve the platform.