OpenAI-Compatible API for Advanced Reasoning Models with Function Calling
Base URL: https://api3.exploit.bot
Default API Key: eric (for testing)
API3 provides OpenAI-compatible endpoints for accessing advanced reasoning models with support for chat completions, function calling, custom reasoning efforts, image processing, and real-time streaming.
"eric" for immediate testing without setup.
API uses Bearer token authentication. The default test key is "eric".
Authorization: Bearer eric
# Using curl with Bearer token
curl -H "Authorization: Bearer eric" \
-H "Content-Type: application/json" \
https://api3.exploit.bot/v1/chat/completions
API3 provides access to a comprehensive model ecosystem with base models and reasoning variants for precise control over performance, speed, and cost.
The following models have been tested and confirmed to work with the current API setup:
| Model | Category | Default Reasoning | Response Time | Best For |
|---|---|---|---|---|
| gpt-5.1-codex-max | π₯ Premium Codex | High | 2-4s | Complex algorithms, production code, architecture |
| gpt-5.1-codex | π₯ Standard Codex | High | 2-3s | Balanced coding performance, daily development |
| gpt-5.1-codex-mini | β‘ Fast Codex | Medium | 1-2s | Quick scripts, prototyping, cost-effective |
| gpt-5.1 | π€ General Purpose | High | 2-3s | Conversations, analysis, mixed tasks |
| gpt-5 | π€ Legacy General | Medium | 1-2s | Basic tasks, compatibility |
Instead of using separate model variants, control reasoning effort dynamically via the x_codex parameter:
| Reasoning Level | API Parameter | Speed | Analysis Depth | Use Cases | Example |
|---|---|---|---|---|---|
| β‘ Low | "reasoning_effort": "low" |
π Fast (1-2s) | π’ Basic | Quick fixes, simple functions, rapid prototyping | Basic debugging, simple scripts |
| βοΈ Medium | "reasoning_effort": "medium" |
β‘ Balanced (2-3s) | π‘ Moderate | Production code, standard development tasks | Most daily coding work |
| π§ High | "reasoning_effort": "high" |
π’ Slow (3-5s) | π Deep | Complex algorithms, architecture, optimization | Advanced problem solving |
The following reasoning levels and model variants are not supported with the current account type:
These models return 400 errors indicating they're "not supported when using Codex with a ChatGPT account"
curl -X POST "https://api3.exploit.bot/v1/chat/completions" \
-H "Authorization: Bearer eric" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5.1-codex-max",
"messages": [{"role": "user", "content": "Write a Python function"}]
}'
curl -X POST "https://api3.exploit.bot/v1/chat/completions" \
-H "Authorization: Bearer eric" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5.1-codex",
"messages": [{"role": "user", "content": "Write a Python function"}],
"x_codex": {
"reasoning_effort": "high"
}
}'
curl -X POST "https://api3.exploit.bot/v1/chat/completions" \
-H "Authorization: Bearer eric" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5.1-codex",
"messages": [{"role": "user", "content": "Quick fix for syntax error"}],
"x_codex": {
"reasoning_effort": "low"
}
}'
| Scenario | Recommended Model | Reasoning Level | Expected Performance |
|---|---|---|---|
| π Production Applications | gpt-5.1-codex-max | Medium | Reliable, efficient, maintainable code |
| β‘ Quick Scripts | gpt-5.1-codex-mini | Low | Fast responses, cost-effective |
| π§ Complex Algorithms | gpt-5.1-codex-max | High | Deep analysis, optimized solutions |
| π£οΈ General Conversations | gpt-5.1 | Medium | Natural dialogue, broad knowledge |
| π§ Rapid Debugging | gpt-5.1-codex | Low | Quick fixes, basic debugging |
| ποΈ System Architecture | gpt-5.1-codex-max | High | Comprehensive design, best practices |
Lists all available models including reasoning effort aliases.
| Header | Value | Required |
|---|---|---|
| Content-Type | application/json | No |
| Authorization | Bearer eric | Yes |
Creates a chat completion response with optional function calling. OpenAI-compatible endpoint.
| Header | Value | Required |
|---|---|---|
| Content-Type | application/json | Yes |
| Authorization | Bearer eric | Yes |
When stream: true, responses are sent as Server-Sent Events:
data: {"choices":[{"delta":{"role":"assistant"},"index":0}]}
data: {"choices":[{"delta":{"content":"Hello"},"index":0}]}
data: {"choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_123","function":{"name":"weather","arguments":""}}]},"index":0}]}
data: {"choices":[{"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\"location\":\"New York\"}"}}]},"index":0}]}
data: {"choices":[{"delta":{},"index":0,"finish_reason":"tool_calls"}]}
data: [DONE]
Alternative endpoint for responses API compatibility (Anthropic-style).
For long-running operations, append ?async=true to any endpoint to submit as a background job.
Benefits: No timeouts, resume on disconnect, progress monitoring, better error handling.
?async=true parameter/v1/jobs/{job_id} for status/v1/jobs/{job_id}/streamcurl -X POST "https://api3.exploit.bot/v1/chat/completions?async=true" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "gpt-5.1-codex", "messages": [{"role": "user", "content": "Complex task..."}]}'
# Returns immediately:
{
"id": "98b2945f-0be2-4ac6-b1b4-cfde9b85a8eb",
"object": "job",
"status": "queued",
"created_at": 1764743637,
"request_type": "chat_completion",
"message": "Job submitted. Poll /v1/jobs/{id} for results..."
}
Get job status and results. Poll this endpoint to check if job is complete.
curl "https://api3.exploit.bot/v1/jobs/98b2945f-0be2-4ac6-b1b4-cfde9b85a8eb" \
-H "Authorization: Bearer YOUR_API_KEY"
# Completed job response:
{
"id": "98b2945f-0be2-4ac6-b1b4-cfde9b85a8eb",
"object": "job",
"status": "completed",
"created_at": 1764743637,
"updated_at": 1764743644,
"request_type": "chat_completion",
"result": {
"id": "98b2945f-0be2-4ac6-b1b4-cfde9b85a8eb",
"object": "chat.completion",
"choices": [{
"index": 0,
"message": {"role": "assistant", "content": "Response text"},
"finish_reason": "stop"
}],
"usage": {}
}
}
Stream job output in real-time via Server-Sent Events.
curl -N "https://api3.exploit.bot/v1/jobs/98b2945f.../stream" \
-H "Authorization: Bearer YOUR_API_KEY"
# Event stream:
event: status
data: {"type":"status","job_id":"...","status":"running"}
event: chunk
data: {"type":"chunk","chunk_index":0,"chunk":"Hello"}
event: final
data: {"type":"final","status":"completed","result":{...}}
data: [DONE]
List all jobs with pagination.
| Parameter | Default | Max |
|---|---|---|
| limit | 100 | 1000 |
| offset | 0 | - |
curl "https://api3.exploit.bot/v1/jobs?limit=10" \
-H "Authorization: Bearer YOUR_API_KEY"
Cancel a running or queued job.
curl -X DELETE "https://api3.exploit.bot/v1/jobs/98b2945f..." \
-H "Authorization: Bearer YOUR_API_KEY"
# Response:
{"status": "cancelled", "job_id": "98b2945f..."}
Health check endpoint.
Response: {"status": "ok"}
The Codex CLI provides powerful built-in tools that enable AI models to perform real-world actions beyond text generation. All tools are accessed through the x_codex parameter and work seamlessly with streaming.
| Tool | Description | Use Cases | Response Time | Security |
|---|---|---|---|---|
| web_search | Real-time web search with citations | Current events, weather, news, research | π 15-20s | π Requires network access |
| computer_use | File operations and script execution | File creation, editing, code generation | β‘ 2-3s | π Workspace restricted |
| shell | System command execution | Directory operations, automation | β‘ 2-4s | π Sandbox controlled |
Tools must be explicitly enabled in each request using the x_codex parameter. Multiple tools can be enabled simultaneously.
{
"model": "gpt-5.1-codex",
"messages": [
{"role": "user", "content": "What's the current weather in Tokyo?"}
],
"x_codex": {
"tools": ["web_search"],
"sandbox": "workspace-write",
"reasoning_effort": "medium"
}
}
{
"model": "gpt-5.1-codex-max",
"messages": [
{"role": "user", "content": "Research latest AI developments and create a summary markdown file"}
}
],
"x_codex": {
"tools": ["web_search", "computer_use"],
"sandbox": "workspace-write",
"reasoning_effort": "high",
"network_access": true
}
}
The web search tool provides access to current information from the internet with proper citations and source attribution.
curl -X POST "https://api3.exploit.bot/v1/chat/completions" \
-H "Authorization: Bearer eric" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5.1-codex",
"messages": [{"role": "user", "content": "Current weather in London?"}],
"x_codex": {
"tools": ["web_search"],
"network_access": true
}
}'
curl -X POST "https://api3.exploit.bot/v1/chat/completions" \
-H "Authorization: Bearer eric" \
-d '{
"model": "gpt-5.1-codex",
"messages": [{"role": "user", "content": "Latest AI breakthroughs"}],
"x_codex": {
"tools": ["web_search"],
"network_access": true,
"reasoning_effort": "high"
}
}'
The current temperature in London is 8Β°C (46Β°F) with light rain.
Sources:
- BBC Weather (london.weather.bbc.co.uk)
- Met Office (metoffice.gov.uk)
Enables file creation, editing, reading, and code generation within the secure workspace environment.
{
"model": "gpt-5.1-codex",
"messages": [
{"role": "user", "content": "Create a Python script that calculates fibonacci numbers"}
],
"x_codex": {
"tools": ["computer_use"],
"sandbox": "workspace-write"
}
}
{
"model": "gpt-5.1-codex",
"messages": [
{"role": "user", "content": "Read and summarize the contents of report.txt"}
],
"x_codex": {
"tools": ["computer_use"],
"sandbox": "workspace-write"
}
}
β
Created: /workspace/codex-api3/fibonacci.py
β
Created: /workspace/codex-api3/analysis_report.md
β
Created: /workspace/codex-api3/backup/2025-11-30_data.zip
Execute shell commands for system operations, automation, and file management tasks.
{
"model": "gpt-5.1-codex",
"messages": [
{"role": "user", "content": "List all Python files in the current directory"}
],
"x_codex": {
"tools": ["shell"],
"sandbox": "workspace-write"
}
}
{
"model": "gpt-5.1-codex",
"messages": [
{"role": "user", "content": "Show system uptime and disk usage"}
],
"x_codex": {
"tools": ["shell"],
"sandbox": "workspace-write"
}
}
All file operations are restricted to the /workspace/codex-api3 directory, ensuring secure containment.
| Mode | File Access | Network Access | System Access | Recommended For |
|---|---|---|---|---|
read-only |
β No file writes | β No network | β No system | Secure read-only queries |
workspace-write |
β Workspace files | βοΈ Per request | βοΈ Limited | Default balanced usage |
danger-full-access |
β οΈ Full system | β οΈ Full network | β οΈ Full system | π« Blocked by policy |
The danger-full-access mode is blocked by server policy for security reasons. Always use the least privileged mode necessary for your use case.
{
"x_codex": {
"tools": ["web_search", "computer_use", "shell"],
"sandbox": "workspace-write",
"reasoning_effort": "medium",
"network_access": true,
"hide_reasoning": false
}
}
| Parameter | Type | Values | Default | Description |
|---|---|---|---|---|
tools |
Array | ["web_search"], ["computer_use"], ["shell"] |
[] | Tools to enable for this request |
sandbox |
String | "read-only", "workspace-write", "danger-full-access" |
"read-only" |
File system access level |
reasoning_effort |
String | "low", "medium", "high" |
"medium" |
Model reasoning depth |
network_access |
Boolean | true, false |
false |
Allow web search network access |
hide_reasoning |
Boolean | true, false |
false |
Hide thinking/reasoning blocks |
Tools: ["web_search", "computer_use"]
Reasoning: Medium to High
Use: Current events, fact-checking, documentation
Tools: ["computer_use"]
Reasoning: Medium
Use: Scripts, files, automation
Tools: ["shell", "computer_use"]
Reasoning: Low to Medium
Use: File management, system info
Tools: ["web_search", "computer_use", "shell"]
Reasoning: High
Use: Research β Generate β Execute workflows
The Codex CLI tools system provides powerful capabilities directly integrated into the AI model:
Activation: Tools are automatically available when enabled in config.toml. Use appropriate x_codex parameters for security and performance control.
Streaming enables real-time delivery of response chunks as they're generated, providing immediate feedback and reducing perceived latency. The API implements OpenAI-compatible SSE streaming with token-by-token precision.
This wrapper forwards Codex CLI output verbatim with character-by-character precision, ensuring natural streaming boundaries that match OpenAI's SSE format.
Request streaming with stream: true in your API call:
curl -X POST "https://api3.exploit.bot/v1/chat/completions" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5.1-codex",
"messages": [
{"role": "user", "content": "Explain quantum computing"}
],
"stream": true
}'
Responses follow OpenAI's SSE format with data: prefixes:
data: {"choices":[{"delta":{"content":"Quantum"},"index":0}]}
data: {"choices":[{"delta":{"content":" computing"},"index":0}]}
data: {"choices":[{"delta":{"content":" is"},"index":0}]}
data: {"choices":[{"delta":{"content":" a"},"index":0}]}
data: [DONE]
Python example for handling SSE streams:
import requests
import json
def stream_chat_completion(messages):
response = requests.post(
"https://api3.exploit.bot/v1/chat/completions",
headers={
"Authorization": "Bearer YOUR_API_KEY",
"Content-Type": "application/json"
},
json={
"model": "gpt-5.1-codex",
"messages": messages,
"stream": True
},
stream=True
)
for line in response.iter_lines():
if line:
line = line.decode('utf-8')
if line.startswith('data: '):
data = line[6:] # Remove 'data: ' prefix
if data == '[DONE]':
break
try:
chunk = json.loads(data)
content = chunk['choices'][0]['delta'].get('content', '')
if content:
print(content, end='', flush=True)
except json.JSONDecodeError:
continue
# Usage
messages = [{"role": "user", "content": "Tell me a story"}]
stream_chat_completion(messages)
This API provides complete Unicode support for international languages, including Korean, Japanese, Chinese, Arabic, Cyrillic, and other character sets. All text processing maintains proper UTF-8 encoding across both streaming and non-streaming responses.
Full Korean language support has been implemented and tested with proper UTF-8 encoding for both input and output, including real-time streaming responses.
All incoming requests are processed with UTF-8 encoding:
Server-Sent Events (SSE) streaming includes proper UTF-8 charset specification:
Content-Type: text/event-stream; charset=utf-8
curl -X POST "https://api3.exploit.bot/v1/chat/completions" \
-H "Authorization: Bearer eric" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5.1-codex",
"messages": [
{"role": "user", "content": "μλ
νμΈμ! νκ΅μ μλλ μ΄λμΈκ°μ?"}
]
}'
Response:
{
"choices": [{
"message": {
"content": "μλ
νμΈμ! νκ΅μ μλλ μμΈμ
λλ€."
}
}]
}
curl -X POST "https://api3.exploit.bot/v1/chat/completions" \
-H "Authorization: Bearer eric" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5.1-codex",
"messages": [
{"role": "user", "content": "κΉμΉλ λΉλΉλ°₯μ νκ΅μ μ ν΅ μμμ
λλ€."}
],
"stream": true
}'
Streaming Response:
data: {"choices": [{"delta": {"content": "λ€, λ§μ΅λλ€!"}}]}
data: {"choices": [{"delta": {"content": " κΉμΉμ λΉλΉλ°₯μ"}}]}
data: {"choices": [{"delta": {"content": " μ€λ μ ν΅μ κ°μ§"}}]}
data: {"choices": [{"delta": {"content": " λνμ μΈ νκ΅ μμμ
λλ€."}}]}
data: [DONE]
The streaming implementation was specifically optimized for international character support:
# Fixed implementation in app/codex.py
import codecs
decoder = codecs.getincrementaldecoder('utf-8')(errors='ignore')
while True:
chunk = await proc.stdout.read(8) # Read 8-byte chunks
decoded_chars = decoder.decode(chunk) # Handle multi-byte chars
# Process characters with proper boundaries
curl -X POST "https://api3.exploit.bot/v1/chat/completions" \
-H "Authorization: Bearer eric" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5.1-codex",
"messages": [
{"role": "user", "content": "Hello, how are you?"}
]
}'
},
"finish_reason": "tool_calls"
}]
}
{
"model": "gpt-5.1-codex",
"messages": [
{"role": "user", "content": "What's the weather in Tokyo?"},
{
"role": "assistant",
"tool_calls": [{
"id": "call_1",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"location\":\"Tokyo\",\"units\":\"celsius\"}"
}
}]
},
{
"role": "tool",
"tool_call_id": "call_1",
"content": "{\"temperature\":22,\"condition\":\"sunny\",\"humidity\":65}"
}
]
}
{
"choices": [{
"message": {
"role": "assistant",
"content": "The weather in Tokyo is currently sunny with a temperature of 22Β°C and humidity at 65%."
},
"finish_reason": "stop"
}]
}
Streaming enables real-time delivery of response chunks as they're generated, providing immediate feedback and reducing perceived latency. The API implements OpenAI-compatible SSE streaming.
Set stream: true in your request:
{
"model": "gpt-5.1-codex",
"messages": [
{"role": "user", "content": "Write a short story"}
],
"stream": true
}
data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1704067200,"model":"gpt-5.1","choices":[{"index":0,"delta":{"content":"Once upon a time"}}]}
data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1704067200,"model":"gpt-5.1","choices":[{"index":0,"delta":{"content":" in a distant galaxy"}}]}
data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1704067200,"model":"gpt-5.1","choices":[{"index":0,"delta":{}}]}
data: {"choices":[{"delta":{"role":"assistant"},"index":0}]}
data: {"choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_123","function":{"name":"get_weather","arguments":""}}]},"index":0}]}
data: {"choices":[{"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\"location\":\"NYC\""}}]},"index":0}]}
data: {"choices":[{"delta":{"tool_calls":[{"index":0,"function":{"arguments":",\"units\":\"celsius\"}"}}]},"index":0}]}
data: {"choices":[{"delta":{"tool_calls":[{"index":0,"function":{}}]},"index":0,"finish_reason":"tool_calls"}]}
data: [DONE]
class StreamingChat {
constructor(apiKey = 'eric') {
this.baseURL = 'https://api3.exploit.bot/v1';
this.apiKey = apiKey;
}
async streamChat(messages, onChunk, onComplete, onError) {
try {
const response = await fetch(`${this.baseURL}/chat/completions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.apiKey}`,
'Accept': 'text/event-stream',
},
body: JSON.stringify({
model: 'gpt-5.1',
messages: messages,
stream: true
})
});
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
let fullContent = '';
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split('\n');
buffer = lines.pop(); // Keep incomplete line in buffer
for (const line of lines) {
if (line.startsWith('data: ')) {
const data = line.slice(6);
if (data === '[DONE]') {
onComplete(fullContent);
return;
}
try {
const chunk = JSON.parse(data);
const delta = chunk.choices[0]?.delta;
if (delta?.content) {
fullContent += delta.content;
onChunk(delta.content, chunk);
}
// Handle tool calls
if (delta?.tool_calls) {
onChunk({ tool_calls: delta.tool_calls }, chunk);
}
} catch (e) {
console.error('Parse error:', e);
}
}
}
}
} catch (error) {
onError(error);
}
}
}
// Usage
const chat = new StreamingChat();
chat.streamChat(
[{ role: 'user', content: 'Tell me a story' }],
(chunk, fullData) => {
process.stdout.write(chunk.content || '');
},
(fullContent) => {
console.log('\n\nComplete:', fullContent);
},
(error) => {
console.error('Error:', error);
}
);
import asyncio
import aiohttp
import json
async def stream_chat_completion(messages, model="gpt-5.1"):
url = "https://api3.exploit.bot/v1/chat/completions"
headers = {
"Authorization": "Bearer eric",
"Content-Type": "application/json"
}
payload = {
"model": model,
"messages": messages,
"stream": True
}
async with aiohttp.ClientSession() as session:
async with session.post(url, headers=headers, json=payload) as response:
full_content = ""
async for line in response.content:
line = line.decode('utf-8').strip()
if line.startswith('data: '):
data = line[6:]
if data == '[DONE]':
break
try:
chunk = json.loads(data)
delta = chunk.get('choices', [{}])[0].get('delta', {})
if 'content' in delta:
content = delta['content']
full_content += content
print(content, end='', flush=True)
# Handle tool calls
if 'tool_calls' in delta:
print(f"\nTool Call: {delta['tool_calls']}")
except json.JSONDecodeError:
continue
return full_content
# Usage
async def main():
messages = [{"role": "user", "content": "Explain quantum computing"}]
result = await stream_chat_completion(messages)
print(f"\n\nFull response: {result}")
asyncio.run(main())
import requests
import json
def stream_chat(messages, model="gpt-5.1"):
response = requests.post(
"https://api3.exploit.bot/v1/chat/completions",
headers={
"Authorization": "Bearer eric",
"Content-Type": "application/json"
},
json={
"model": model,
"messages": messages,
"stream": True
},
stream=True
)
full_content = ""
for line in response.iter_lines():
if line:
line = line.decode('utf-8')
if line.startswith('data: '):
data = line[6:]
if data == '[DONE]':
break
try:
chunk = json.loads(data)
delta = chunk.get('choices', [{}])[0].get('delta', {})
if 'content' in delta:
content = delta['content']
full_content += content
print(content, end='', flush=True)
except json.JSONDecodeError:
continue
return full_content
# Usage
result = stream_chat([{"role": "user", "content": "Hello!"}])
print(f"\nComplete: {result}")
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
)
type StreamChunk struct {
Choices []struct {
Delta struct {
Content string `json:"content,omitempty"`
Role string `json:"role,omitempty"`
ToolCalls []struct {
Index int `json:"index"`
ID string `json:"id"`
Type string `json:"type"`
Function struct {
Name string `json:"name,omitempty"`
Arguments string `json:"arguments,omitempty"`
} `json:"function,omitempty"`
} `json:"tool_calls,omitempty"`
} `json:"delta"`
FinishReason string `json:"finish_reason,omitempty"`
} `json:"choices"`
}
func streamChat(messages []map[string]string, model string) error {
reqBody := map[string]interface{}{
"model": model,
"messages": messages,
"stream": true,
}
jsonData, err := json.Marshal(reqBody)
if err != nil {
return err
}
req, err := http.NewRequest("POST", "https://api3.exploit.bot/v1/chat/completions", bytes.NewBuffer(jsonData))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer eric")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
line := scanner.Text()
if len(line) > 6 && line[:6] == "data: " {
data := line[6:]
if data == "[DONE]" {
break
}
var chunk StreamChunk
if err := json.Unmarshal([]byte(data), &chunk); err == nil {
if len(chunk.Choices) > 0 {
content := chunk.Choices[0].Delta.Content
if content != "" {
fmt.Print(content)
}
}
}
}
return scanner.Err()
}
func main() {
messages := []map[string]string{
{"role": "user", "content": "Explain Go programming"},
}
err := streamChat(messages, "gpt-5.1")
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
}
}
Connection: keep-alive for persistent connectionsAPI3 features optimized real-time streaming with 32-byte chunk reading and immediate output for minimal latency:
Unlike standard OpenAI streaming, API3 includes 100% of model thinking and reasoning in the stream:
Example stream showing reasoning:
data: {"choices":[{"delta":{"content":"π€ Thinking: I need to analyze the requirements..."}}]}
data: {"choices":[{"delta":{"content":"π First, let me create the file structure..."}}]}
data: {"choices":[{"delta":{"content":"π‘ I'll implement a recursive solution..."}}]}
data: {"choices":[{"delta":{"content":"β
File created: solution.py"}}]}
data: {"choices":[{"delta":{"content":"Here's the complete implementation..."}}]}
Watch tool calls execute in real-time with detailed progress information:
Example tool call stream:
data: {"choices":[{"delta":{"content":"π Creating test.py:1..."}}]}
data: {"choices":[{"delta":{"content":"β
Added fibonacci function with input validation"}}]}
data: {"choices":[{"delta":{"content":"π Writing main execution block..."}}]}
data: {"choices":[{"delta":{"content":"π§ͺ Adding test cases..."}}]}
data: {"choices":[{"delta":{"content":"β¨ Complete! The file is ready."}}]}
{
"model": "gpt-5.1-codex",
"messages": [...],
"stream": true,
"x_codex": {
"sandbox": "workspace-write", // Enable file operations
"reasoning_effort": "high", // Control thinking depth
"hide_reasoning": false, // Show reasoning process
"network_access": true, // Allow internet access
"expose_reasoning": true // Include detailed thoughts
}
}
| Effort Level | Description | Use Case | Speed |
|---|---|---|---|
minimal |
Quick responses, minimal thinking | Simple tasks, basic Q&A | β‘ Fastest |
low |
Light reasoning, quick solutions | Simple code, straightforward problems | π Fast |
medium |
Balanced reasoning and speed (default) | General purpose, most tasks | βοΈ Balanced |
high |
Deep analysis, thorough reasoning | Complex problems, architecture | π§ Detailed |
extra high |
Maximum reasoning, comprehensive analysis | Research, complex algorithms | π¬ Thorough |
// Request file creation
{
"model": "gpt-5.1-codex",
"messages": [
{"role": "user", "content": "Create a Python web server with Flask that handles API endpoints"}
],
"stream": true,
"x_codex": {
"sandbox": "workspace-write",
"reasoning_effort": "high"
}
}
// Stream shows real-time file operations
data: {"choices":[{"delta":{"content":"π Thinking: I'll create a Flask web server..."}}]}
data: {"choices":[{"delta":{"content":"π Creating app.py..."}}]}
data: {"choices":[{"delta":{"content":"β
Added Flask imports and app initialization"}}]}
data: {"choices":[{"delta":{"content":"π Adding /api/health endpoint..."}}]}
data: {"choices":[{"delta":{"content":"π Adding /api/data endpoint with JSON response..."}}]}
data: {"choices":[{"delta":{"content":"π§ͺ Adding test routes..."}}]}
data: {"choices":[{"delta":{"content":"β¨ Flask server complete!"}}]}
// Ask for code debugging
{
"model": "gpt-5.1-codex-high",
"messages": [
{"role": "user", "content": "Debug this slow SQL query and optimize it"}
],
"stream": true,
"x_codex": {
"reasoning_effort": "extra high"
}
}
// Stream shows debugging process
data: {"choices":[{"delta":{"content":"π Analyzing query performance..."}}]}
data: {"choices":[{"delta":{"content":"π Found missing index on user_id column"}}]}
data: {"choices":[{"delta":{"content":"β‘ Optimizing JOIN operations..."}}]}
data: {"choices":[{"delta":{"content":"π Adding composite index for better performance"}}]}
data: {"choices":[{"delta":{"content":"β
Query optimized - 100x faster!"}}]}
Issue Resolved: Parallel streaming from multiple models causing connection bottlenecks
Solution Implemented:
# Connection Pooling
CODEX_MAX_PARALLEL_REQUESTS=5 # Increased from default 2
CODEX_TIMEOUT=120 # 2-minute timeout
# Service Environment
PATH=/usr/bin:/usr/local/bin:/home/eric/.nvm/versions/node/v25.2.1/bin
CODEX_PATH=/home/eric/.nvm/versions/node/v25.2.1/bin/codex
| Metric | Before | After | Improvement |
|---|---|---|---|
| Concurrent Requests | 2 | 5 | +150% |
| Timeout Handling | Basic | 120s with cleanup | More reliable |
| Service Startup | Failing | Fast initialization | Fixed |
| Memory Usage | Variable | Stable 34MB | Consistent |
| UTF-8 Streaming | Chunk-based | Token-by-token | Better for international |
# Test: Korean Character Streaming (UTF-8 Validation)
curl -X POST https://api3.exploit.bot/v1/chat/completions \
-H "Authorization: Bearer eric" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5.1-codex",
"messages": [{"role": "user", "content": "Say hello in Korean: μλ
νμΈμ"}],
"stream": true
}'
# Response: Proper UTF-8 encoding
data: {"choices": [{"delta": {"content": "\uc548\ub155\ud558\uc138\uc694\n"}}]}
# Results: β
Perfect Unicode handling
# β
No character corruption
# β
Natural token boundaries
# β
International character support
// Content chunks (most common)
data: {"choices":[{"delta":{"content":"Response text"}}]}
// Tool call progress
data: {"choices":[{"delta":{"content":"π§ Creating file.py..."}}]}
// Reasoning/thinking
data: {"choices":[{"delta":{"content":"π§ Analyzing requirements..."}}]}
// Status updates
data: {"choices":[{"delta":{"content":"β
Operation completed"}}]}
// Error handling
data: {"choices":[{"delta":{"content":"β οΈ Warning: File already exists"}}]}
// Enhanced JavaScript error handling
class EnhancedStreamingChat {
async streamChat(messages, onChunk, onComplete, onError) {
try {
const response = await fetch(/* ... */);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const reader = response.body.getReader();
let buffer = '';
let chunkCount = 0;
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += new TextDecoder().decode(value);
const lines = buffer.split('\n');
buffer = lines.pop();
for (const line of lines) {
if (line.startsWith('data: ')) {
const data = line.slice(6);
if (data === '[DONE]') {
onComplete(fullContent, chunkCount);
return;
}
try {
const chunk = JSON.parse(data);
chunkCount++;
// Handle different content types
const delta = chunk.choices[0]?.delta;
if (delta?.content) {
if (delta.content.includes('β οΈ')) {
console.warn('Warning:', delta.content);
} else if (delta.content.includes('β')) {
onError(new Error('Operation failed: ' + delta.content));
return;
} else {
onChunk(delta.content, chunk);
}
}
} catch (e) {
console.error('Parse error for chunk:', line);
}
}
}
}
} catch (error) {
onError(error);
}
}
}
curl -X POST "https://api3.exploit.bot/v1/chat/completions" \
-H "Authorization: Bearer eric" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5.1-codex",
"messages": [
{"role": "user", "content": "Hello, how are you?"}
]
}'
curl -X POST "https://api3.exploit.bot/v1/chat/completions" \
-H "Authorization: Bearer eric" \
-H "Content-Type: application/json" \
-d '{
"model": "codex-cli high",
"messages": [
{"role": "user", "content": "Solve this complex algorithm problem"}
],
"x_codex": {
"reasoning_effort": "high"
}
}'
curl -X POST "https://api3.exploit.bot/v1/chat/completions" \
-H "Authorization: Bearer eric" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5.1-codex",
"messages": [
{"role": "user", "content": "What is the weather in Paris?"}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string"},
"units": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": ["location"]
}
}
}
],
"tool_choice": "auto"
}'
curl -X POST "https://api3.exploit.bot/v1/chat/completions" \
-H "Authorization: Bearer eric" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5.1-codex",
"messages": [
{"role": "user", "content": "Write a story about AI"}
],
"stream": true
}'
curl -X POST "https://api3.exploit.bot/v1/chat/completions" \
-H "Authorization: Bearer eric" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5.1-codex",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "Analyze this code and explain what it does"},
{
"type": "image_url",
"image_url": {
"url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."
}
}
]
}
]
}'
curl -X POST "https://api3.exploit.bot/v1/chat/completions" \
-H "Authorization: Bearer eric" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5.1-codex",
"messages": [
{"role": "user", "content": "Get weather for NYC, LA, and Chicago"}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string"}
},
"required": ["location"]
}
}
}
],
"parallel_tool_calls": true,
"tool_choice": "required"
}'
import requests
import json
def chat_completion(message, model="gpt-5.1"):
response = requests.post(
"https://api3.exploit.bot/v1/chat/completions",
headers={
"Authorization": "Bearer eric",
"Content-Type": "application/json"
},
json={
"model": model,
"messages": [{"role": "user", "content": message}]
}
)
if response.status_code == 200:
return response.json()["choices"][0]["message"]["content"]
else:
raise Exception(f"Error: {response.status_code} - {response.text}")
# Usage
result = chat_completion("Explain quantum computing")
print(result)
import asyncio
import aiohttp
import json
class AsyncAPI3Client:
def __init__(self, api_key="eric"):
self.base_url = "https://api3.exploit.bot/v1"
self.api_key = api_key
self.session = None
async def __aenter__(self):
self.session = aiohttp.ClientSession(
headers={"Authorization": f"Bearer {self.api_key}"}
)
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
if self.session:
await self.session.close()
async def chat(self, messages, model="gpt-5.1", **kwargs):
payload = {
"model": model,
"messages": messages,
**kwargs
}
async with self.session.post(
f"{self.base_url}/chat/completions",
json=payload
) as response:
if response.status == 200:
data = await response.json()
return data["choices"][0]["message"]
else:
text = await response.text()
raise Exception(f"Error {response.status}: {text}")
# Usage
async def main():
async with AsyncAPI3Client() as client:
response = await client.chat([
{"role": "user", "content": "Write a Python function"}
])
print(response.content)
asyncio.run(main())
import requests
def call_function_with_tools():
tools = [
{
"type": "function",
"function": {
"name": "calculate",
"description": "Perform mathematical calculations",
"parameters": {
"type": "object",
"properties": {
"expression": {
"type": "string",
"description": "Mathematical expression to evaluate"
}
},
"required": ["expression"]
}
}
}
]
messages = [
{"role": "user", "content": "What is 123 * 456?"}
]
# First request - get function call
response = requests.post(
"https://api3.exploit.bot/v1/chat/completions",
headers={
"Authorization": "Bearer eric",
"Content-Type": "application/json"
},
json={
"model": "gpt-5.1-codex",
"messages": messages,
"tools": tools,
"tool_choice": "required"
}
)
data = response.json()
tool_call = data["choices"][0]["message"]["tool_calls"][0]
# Execute the function
function_name = tool_call["function"]["name"]
arguments = json.loads(tool_call["function"]["arguments"])
if function_name == "calculate":
result = eval(arguments["expression"]) # Be careful with eval in production!
# Send result back
messages.append(data["choices"][0]["message"])
messages.append({
"role": "tool",
"tool_call_id": tool_call["id"],
"content": json.dumps({"result": result})
})
# Second request - get final response
final_response = requests.post(
"https://api3.exploit.bot/v1/chat/completions",
headers={
"Authorization": "Bearer eric",
"Content-Type": "application/json"
},
json={
"model": "gpt-5.1-codex",
"messages": messages
}
)
return final_response.json()["choices"][0]["message"]["content"]
# Usage
result = call_function_with_tools()
print(result)
def stream_chat(message, on_chunk=None):
response = requests.post(
"https://api3.exploit.bot/v1/chat/completions",
headers={
"Authorization": "Bearer eric",
"Content-Type": "application/json"
},
json={
"model": "gpt-5.1-codex",
"messages": [{"role": "user", "content": message}],
"stream": True
},
stream=True
)
full_content = ""
for line in response.iter_lines():
if line:
line = line.decode('utf-8')
if line.startswith('data: '):
data = line[6:]
if data == '[DONE]':
break
try:
chunk = json.loads(data)
delta = chunk.get('choices', [{}])[0].get('delta', {})
if 'content' in delta:
content = delta['content']
full_content += content
if on_chunk:
on_chunk(content)
print(content, end='', flush=True)
except json.JSONDecodeError:
continue
return full_content
# Usage
result = stream_chat("Tell me a joke")
print(f"\n\nComplete: {result}")
from openai import OpenAI
client = OpenAI(
api_key="eric",
base_url="https://api3.exploit.bot/v1"
)
def chat_with_sdk(message):
response = client.chat.completions.create(
model="gpt-5.1",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": message}
],
temperature=0.7,
max_tokens=1000
)
return response.choices[0].message.content
def stream_with_sdk(message):
stream = client.chat.completions.create(
model="gpt-5.1",
messages=[{"role": "user", "content": message}],
stream=True
)
full_response = ""
for chunk in stream:
content = chunk.choices[0].delta.content
if content:
full_response += content
print(content, end='', flush=True)
return full_response
def function_calling_with_sdk():
tools = [
{
"type": "function",
"function": {
"name": "get_stock_price",
"description": "Get current stock price",
"parameters": {
"type": "object",
"properties": {
"symbol": {"type": "string"},
"currency": {"type": "string", "enum": ["USD", "EUR"]}
},
"required": ["symbol"]
}
}
}
]
response = client.chat.completions.create(
model="gpt-5.1",
messages=[{"role": "user", "content": "What's AAPL stock price?"}],
tools=tools,
tool_choice="auto"
)
return response.choices[0].message
class API3Client {
constructor(apiKey = 'eric') {
this.baseURL = 'https://api3.exploit.bot/v1';
this.apiKey = apiKey;
}
async chat(messages, options = {}) {
const response = await fetch(`${this.baseURL}/chat/completions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.apiKey}`
},
body: JSON.stringify({
model: options.model || 'gpt-5.1',
messages,
stream: false,
...options
})
});
if (!response.ok) {
const error = await response.json();
throw new Error(`Error: ${response.status} - ${error.detail?.message || 'Unknown error'}`);
}
const data = await response.json();
return data.choices[0].message;
}
async stream(messages, onChunk, options = {}) {
const response = await fetch(`${this.baseURL}/chat/completions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.apiKey}`
},
body: JSON.stringify({
model: options.model || 'gpt-5.1',
messages,
stream: true,
...options
})
});
if (!response.ok) {
throw new Error(`Error: ${response.status}`);
}
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value);
const lines = buffer.split('\n');
buffer = lines.pop();
for (const line of lines) {
if (line.startsWith('data: ')) {
const data = line.slice(6);
if (data === '[DONE]') return;
try {
const chunk = JSON.parse(data);
const delta = chunk.choices[0]?.delta;
if (delta?.content) {
onChunk(delta.content, chunk);
}
} catch (e) {
console.error('Parse error:', e);
}
}
}
}
}
}
// Usage
const client = new API3Client();
// Basic chat
client.chat([
{ role: 'user', content: 'Hello!' }
]).then(response => {
console.log(response.content);
});
// Streaming
client.stream(
[{ role: 'user', content: 'Write a poem' }],
(chunk) => process.stdout.write(chunk)
).then(() => {
console.log('\nStream complete');
});
async function functionCallingExample() {
const tools = [
{
type: 'function',
function: {
name: 'send_email',
description: 'Send an email to a recipient',
parameters: {
type: 'object',
properties: {
to: { type: 'string', description: 'Email address' },
subject: { type: 'string', description: 'Email subject' },
body: { type: 'string', description: 'Email body' }
},
required: ['to', 'subject', 'body']
}
}
}
];
const messages = [
{ role: 'user', content: 'Send an email to john@example.com about the meeting' }
];
// Get function call
const response = await fetch('https://api3.exploit.bot/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': 'Bearer eric',
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'gpt-5.1',
messages,
tools,
tool_choice: 'required'
})
});
const data = await response.json();
const toolCall = data.choices[0].message.tool_calls[0];
// Execute function (mock implementation)
const args = JSON.parse(toolCall.function.arguments);
const emailResult = await sendEmail(args.to, args.subject, args.body);
// Continue conversation with result
messages.push(data.choices[0].message);
messages.push({
role: 'tool',
tool_call_id: toolCall.id,
content: JSON.stringify({ success: true, messageId: emailResult.id })
});
const finalResponse = await fetch('https://api3.exploit.bot/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': 'Bearer eric',
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'gpt-5.1',
messages
})
});
const finalData = await finalResponse.json();
return finalData.choices[0].message.content;
}
function streamingWithEventSource(messages) {
// Note: EventSource doesn't support POST, so we need to use fetch for streaming
const eventSource = new EventSource('/stream-endpoint'); // If you have a proxy
eventSource.onmessage = (event) => {
const data = JSON.parse(event.data);
console.log('Received:', data);
};
// Better approach: Use fetch with streaming as shown above
}
async function robustChat(message, retries = 3) {
for (let attempt = 1; attempt <= retries; attempt++) {
try {
const response = await fetch('https://api3.exploit.bot/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': 'Bearer eric',
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'gpt-5.1',
messages: [{ role: 'user', content: message }]
})
});
if (response.ok) {
const data = await response.json();
return data.choices[0].message.content;
} else if (response.status === 429) {
// Rate limited - wait and retry
const retryAfter = response.headers.get('Retry-After') || 1;
await new Promise(resolve => setTimeout(resolve, retryAfter * 1000));
continue;
} else {
const error = await response.json();
throw new Error(error.detail?.message || `HTTP ${response.status}`);
}
} catch (error) {
console.error(`Attempt ${attempt} failed:`, error);
if (attempt === retries) {
throw error;
}
// Exponential backoff
await new Promise(resolve => setTimeout(resolve, Math.pow(2, attempt) * 1000));
}
}
}
interface Message {
role: 'system' | 'user' | 'assistant' | 'tool';
content?: string;
tool_calls?: ToolCall[];
tool_call_id?: string;
}
interface ToolCall {
id: string;
type: 'function';
function: {
name: string;
arguments: string;
};
}
interface Tool {
type: 'function';
function: {
name: string;
description: string;
parameters: Record;
};
}
interface ChatCompletionRequest {
model: string;
messages: Message[];
stream?: boolean;
temperature?: number;
max_tokens?: number;
tools?: Tool[];
tool_choice?: 'auto' | 'required' | 'none' | { type: 'function'; function: { name: string } };
parallel_tool_calls?: boolean;
x_codex?: {
reasoning_effort?: 'minimal' | 'low' | 'medium' | 'high';
sandbox?: 'read-only' | 'danger-full-access';
network_access?: boolean;
hide_reasoning?: boolean;
};
}
interface ChatCompletionResponse {
id: string;
object: 'chat.completion';
created: number;
model: string;
choices: Array<{
index: number;
message: Message;
finish_reason: 'stop' | 'length' | 'tool_calls' | 'content_filter';
}>;
usage: {
prompt_tokens: number;
completion_tokens: number;
total_tokens: number;
};
}
class API3Client {
private readonly baseURL: string;
private readonly apiKey: string;
constructor(apiKey: string = 'eric', baseURL: string = 'https://api3.exploit.bot/v1') {
this.apiKey = apiKey;
this.baseURL = baseURL;
}
async chat(request: ChatCompletionRequest): Promise {
const response = await fetch(`${this.baseURL}/chat/completions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.apiKey}`
},
body: JSON.stringify({
model: 'gpt-5.1',
...request
})
});
if (!response.ok) {
const error = await response.json() as any;
throw new Error(`API Error: ${response.status} - ${error.detail?.message || 'Unknown'}`);
}
return response.json();
}
async stream(request: ChatCompletionRequest): Promise> {
const response = await fetch(`${this.baseURL}/chat/completions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.apiKey}`
},
body: JSON.stringify({
...request,
stream: true
})
});
if (!response.ok) {
throw new Error(`Stream error: ${response.status}`);
}
const reader = response.body?.getReader();
const decoder = new TextDecoder();
if (!reader) {
throw new Error('No response body');
}
return this.parseStream(reader, decoder);
}
private async *parseStream(reader: ReadableStreamDefaultReader, decoder: TextDecoder): AsyncGenerator {
let buffer = '';
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value);
const lines = buffer.split('\n');
buffer = lines.pop() || '';
for (const line of lines) {
if (line.startsWith('data: ')) {
const data = line.slice(6);
if (data === '[DONE]') return;
try {
const chunk = JSON.parse(data) as ChatCompletionResponse;
yield chunk;
} catch (e) {
console.error('Parse error:', e);
}
}
}
}
}
}
// Usage Example
const client = new API3Client();
// Basic chat with type safety
async function example() {
const messages: Message[] = [
{ role: 'system', content: 'You are a TypeScript expert.' },
{ role: 'user', content: 'Explain generics in TypeScript' }
];
const response = await client.chat({
model: 'gpt-5.1',
messages,
temperature: 0.7
});
console.log(response.choices[0].message.content);
}
// Function calling with types
async function functionCallingExample() {
const tools: Tool[] = [
{
type: 'function',
function: {
name: 'create_file',
description: 'Create a file with content',
parameters: {
type: 'object',
properties: {
path: { type: 'string' },
content: { type: 'string' }
},
required: ['path', 'content']
}
}
}
];
const messages: Message[] = [
{ role: 'user', content: 'Create a TypeScript file with a hello world function' }
];
const response = await client.chat({
model: 'gpt-5.1-codex',
messages,
tools,
tool_choice: 'auto'
});
// Type-safe function call handling
const toolCalls = response.choices[0].message.tool_calls;
if (toolCalls) {
for (const toolCall of toolCalls) {
console.log(`Calling ${toolCall.function.name} with args:`, toolCall.function.arguments);
}
}
}
import { useState, useCallback } from 'react';
interface UseChatOptions {
model?: string;
temperature?: number;
onStream?: (chunk: string) => void;
onError?: (error: Error) => void;
}
export function useChat(options: UseChatOptions = {}) {
const [loading, setLoading] = useState(false);
const [messages, setMessages] = useState([]);
const [error, setError] = useState(null);
const sendMessage = useCallback(async (content: string) => {
setLoading(true);
setError(null);
const newMessage: Message = { role: 'user', content };
setMessages(prev => [...prev, newMessage]);
try {
const response = await fetch('https://api3.exploit.bot/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': 'Bearer eric',
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: options.model || 'gpt-5.1',
messages: [...messages, newMessage],
temperature: options.temperature || 0.7
})
});
if (!response.ok) {
throw new Error(`Error: ${response.status}`);
}
const data = await response.json();
const assistantMessage = data.choices[0].message;
setMessages(prev => [...prev, assistantMessage]);
return assistantMessage;
} catch (err) {
const error = err as Error;
setError(error);
options.onError?.(error);
throw error;
} finally {
setLoading(false);
}
}, [messages, options]);
const streamMessage = useCallback(async (content: string) => {
setLoading(true);
setError(null);
const newMessage: Message = { role: 'user', content };
setMessages(prev => [...prev, newMessage]);
try {
const response = await fetch('https://api3.exploit.bot/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': 'Bearer eric',
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: options.model || 'gpt-5.1',
messages: [...messages, newMessage],
stream: true
})
});
const reader = response.body?.getReader();
const decoder = new TextDecoder();
let assistantContent = '';
if (reader) {
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value);
const lines = chunk.split('\n');
for (const line of lines) {
if (line.startsWith('data: ')) {
const data = line.slice(6);
if (data === '[DONE]') return;
try {
const parsed = JSON.parse(data);
const delta = parsed.choices[0]?.delta;
if (delta?.content) {
assistantContent += delta.content;
options.onStream?.(delta.content);
}
} catch (e) {
// Ignore parse errors
}
}
}
}
}
const assistantMessage: Message = {
role: 'assistant',
content: assistantContent
};
setMessages(prev => [...prev, assistantMessage]);
return assistantMessage;
} catch (err) {
const error = err as Error;
setError(error);
options.onError?.(error);
throw error;
} finally {
setLoading(false);
}
}, [messages, options]);
return {
messages,
loading,
error,
sendMessage,
streamMessage,
clearMessages: () => setMessages([])
};
}
// Usage in component
function ChatComponent() {
const { messages, loading, sendMessage, streamMessage } = useChat({
model: 'gpt-5.1',
onStream: (chunk) => console.log('Received:', chunk)
});
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
const formData = new FormData(e.target as HTMLFormElement);
const message = formData.get('message') as string;
await streamMessage(message);
};
return (
{messages.map((msg, i) => (
{msg.role}: {msg.content}
))}
{loading && Typing...}
);
}
from openai import OpenAI
# Initialize client with API3
client = OpenAI(
api_key="eric",
base_url="https://api3.exploit.bot/v1"
)
# Basic chat
def basic_chat():
response = client.chat.completions.create(
model="gpt-5.1",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Explain the theory of relativity"}
],
temperature=0.7,
max_tokens=500
)
return response.choices[0].message.content
# Streaming
def streaming_example():
stream = client.chat.completions.create(
model="gpt-5.1",
messages=[{"role": "user", "content": "Write a story"}],
stream=True,
temperature=0.8
)
full_response = ""
for chunk in stream:
content = chunk.choices[0].delta.content
if content:
full_response += content
print(content, end='', flush=True)
return full_response
# Function calling
def function_calling_example():
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location"]
}
}
},
{
"type": "function",
"function": {
"name": "calculate_tip",
"description": "Calculate tip amount",
"parameters": {
"type": "object",
"properties": {
"bill_amount": {"type": "number"},
"tip_percentage": {"type": "number"}
},
"required": ["bill_amount"]
}
}
}
]
response = client.chat.completions.create(
model="gpt-5.1",
messages=[
{"role": "user", "content": "What's the weather in Boston and calculate a 20% tip on a $50 bill?"}
],
tools=tools,
parallel_tool_calls=True,
tool_choice="auto"
)
return response.choices[0].message
# Using codex-cli with reasoning effort
def codex_reasoning_example():
response = client.chat.completions.create(
model="codex-cli high",
messages=[
{"role": "user", "content": "Implement a binary search tree in Python with all operations"}
],
x_codex={
"reasoning_effort": "high",
"sandbox": "read-only",
"hide_reasoning": False
}
)
return response.choices[0].message.content
import OpenAI from 'openai';
// Initialize client
const openai = new OpenAI({
apiKey: 'eric',
baseURL: 'https://api3.exploit.bot/v1',
dangerouslyAllowBrowser: true // Only for browser usage
});
// Basic chat completion
async function basicChat() {
const completion = await openai.chat.completions.create({
model: 'gpt-5.1',
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'Explain JavaScript closures' }
],
temperature: 0.7,
max_tokens: 500
});
return completion.choices[0].message.content;
}
// Streaming with SDK
async function streamingChat() {
const stream = await openai.chat.completions.create({
model: 'gpt-5.1',
messages: [
{ role: 'user', content: 'Write a mystery story' }
],
stream: true
});
let fullResponse = '';
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content;
if (content) {
fullResponse += content;
process.stdout.write(content);
}
}
return fullResponse;
}
// Function calling with SDK
async function functionCalling() {
const tools = [
{
type: 'function',
function: {
name: 'search_web',
description: 'Search the web for information',
parameters: {
type: 'object',
properties: {
query: {
type: 'string',
description: 'Search query'
},
num_results: {
type: 'number',
description: 'Number of results to return'
}
},
required: ['query']
}
}
}
];
const response = await openai.chat.completions.create({
model: 'gpt-5.1',
messages: [
{ role: 'user', content: 'Search for recent AI breakthroughs' }
],
tools,
tool_choice: 'auto'
});
const toolCalls = response.choices[0].message.tool_calls;
if (toolCalls) {
for (const toolCall of toolCalls) {
console.log(`Function: ${toolCall.function.name}`);
console.log(`Args: ${toolCall.function.arguments}`);
}
}
return response.choices[0].message;
}
// Advanced usage with custom logic
class ChatAssistant {
private client: OpenAI;
private conversation: Array = [];
constructor(model: string = 'gpt-5.1') {
this.client = new OpenAI({
apiKey: 'eric',
baseURL: 'https://api3.exploit.bot/v1'
});
}
async addMessage(role: 'user' | 'assistant', content: string) {
this.conversation.push({ role, content });
}
async respond(useTools = false) {
const response = await this.client.chat.completions.create({
model: 'gpt-5.1',
messages: this.conversation,
tools: useTools ? this.getTools() : undefined,
tool_choice: useTools ? 'auto' : undefined
});
const message = response.choices[0].message;
this.conversation.push(message as any);
// Handle tool calls
if (message.tool_calls) {
const results = await this.executeToolCalls(message.tool_calls);
// Add tool results and get final response
for (const result of results) {
this.conversation.push({
role: 'tool',
tool_call_id: result.tool_call_id,
content: result.content
});
}
return this.respond(false); // Get final response without tools
}
return message.content;
}
private getTools() {
return [
{
type: 'function' as const,
function: {
name: 'get_current_time',
description: 'Get current time',
parameters: { type: 'object', properties: {} }
}
}
];
}
private async executeToolCalls(toolCalls: OpenAI.Chat.Completions.ChatCompletionMessageToolCall[]) {
const results = [];
for (const toolCall of toolCalls) {
if (toolCall.function.name === 'get_current_time') {
results.push({
tool_call_id: toolCall.id,
content: new Date().toISOString()
});
}
}
return results;
}
}
// Usage
const assistant = new ChatAssistant();
await assistant.addMessage('user', 'What time is it?');
const response = await assistant.respond(true);
console.log(response);
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"time"
)
// Structs for API requests and responses
type Message struct {
Role string `json:"role"`
Content string `json:"content,omitempty"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
}
type ToolCall struct {
ID string `json:"id"`
Type string `json:"type"`
Function struct {
Name string `json:"name"`
Arguments string `json:"arguments"`
} `json:"function"`
}
type Tool struct {
Type string `json:"type"`
Function struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters map[string]interface{} `json:"parameters"`
} `json:"function"`
}
type ChatRequest struct {
Model string `json:"model"`
Messages []Message `json:"messages"`
Stream bool `json:"stream,omitempty"`
Temperature float64 `json:"temperature,omitempty"`
MaxTokens int `json:"max_tokens,omitempty"`
Tools []Tool `json:"tools,omitempty"`
ToolChoice string `json:"tool_choice,omitempty"`
ParallelToolCalls bool `json:"parallel_tool_calls,omitempty"`
XCodex *XCodex `json:"x_codex,omitempty"`
}
type XCodex struct {
ReasoningEffort string `json:"reasoning_effort,omitempty"`
Sandbox string `json:"sandbox,omitempty"`
NetworkAccess bool `json:"network_access,omitempty"`
HideReasoning bool `json:"hide_reasoning,omitempty"`
}
type ChatResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []struct {
Index int `json:"index"`
Message Message `json:"message"`
FinishReason string `json:"finish_reason"`
} `json:"choices"`
Usage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
} `json:"usage"`
}
// Client struct
type API3Client struct {
BaseURL string
APIKey string
Client *http.Client
}
func NewClient(apiKey string) *API3Client {
return &API3Client{
BaseURL: "https://api3.exploit.bot/v1",
APIKey: apiKey,
Client: &http.Client{
Timeout: 120 * time.Second,
},
}
}
// Basic chat completion
func (c *API3Client) Chat(ctx context.Context, req ChatRequest) (*ChatResponse, error) {
req.Stream = false // Ensure non-streaming
jsonData, err := json.Marshal(req)
if err != nil {
return nil, err
}
httpReq, err := http.NewRequestWithContext(ctx, "POST", c.BaseURL+"/chat/completions", bytes.NewBuffer(jsonData))
if err != nil {
return nil, err
}
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+c.APIKey)
resp, err := c.Client.Do(httpReq)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("API error: %d - %s", resp.StatusCode, string(body))
}
var result ChatResponse
err = json.NewDecoder(resp.Body).Decode(&result)
return &result, err
}
// Streaming chat
func (c *API3Client) ChatStream(ctx context.Context, req ChatRequest, callback func(*ChatResponse) error) error {
req.Stream = true
jsonData, err := json.Marshal(req)
if err != nil {
return err
}
httpReq, err := http.NewRequestWithContext(ctx, "POST", c.BaseURL+"/chat/completions", bytes.NewBuffer(jsonData))
if err != nil {
return err
}
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+c.APIKey)
httpReq.Header.Set("Accept", "text/event-stream")
resp, err := c.Client.Do(httpReq)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("API error: %d - %s", resp.StatusCode, string(body))
}
decoder := json.NewDecoder(resp.Body)
for {
var line string
_, err = fmt.Fscanf(resp.Body, "data: %s\n", &line)
if err != nil {
if err == io.EOF {
break
}
continue
}
if line == "[DONE]" {
break
}
var chunk ChatResponse
if err := json.Unmarshal([]byte(line), &chunk); err == nil {
if callback != nil {
if err := callback(&chunk); err != nil {
return err
}
}
}
}
return nil
}
// Function calling example
func functionCallingExample() {
client := NewClient("eric")
tools := []Tool{
{
Type: "function",
Function: struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters map[string]interface{} `json:"parameters"`
}{
Name: "calculate",
Description: "Perform mathematical calculations",
Parameters: map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"expression": map[string]interface{}{
"type": "string",
"description": "Mathematical expression",
},
},
"required": []string{"expression"},
},
},
},
}
req := ChatRequest{
Model: "gpt-5.1",
Messages: []Message{
{Role: "user", Content: "What is 15 * 23?"},
},
Tools: tools,
ToolChoice: "auto",
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
resp, err := client.Chat(ctx, req)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
// Check for tool calls
if len(resp.Choices) > 0 && len(resp.Choices[0].Message.ToolCalls) > 0 {
toolCall := resp.Choices[0].Message.ToolCalls[0]
fmt.Printf("Function called: %s\n", toolCall.Function.Name)
fmt.Printf("Arguments: %s\n", toolCall.Function.Arguments)
// Execute function and continue conversation...
} else {
fmt.Printf("Response: %s\n", resp.Choices[0].Message.Content)
}
}
// Streaming example
func streamingExample() {
client := NewClient("eric")
req := ChatRequest{
Model: "gpt-5.1",
Messages: []Message{
{Role: "user", Content: "Write a story about a robot"},
},
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
fmt.Print("Streaming: ")
err := client.ChatStream(ctx, req, func(chunk *ChatResponse) error {
if len(chunk.Choices) > 0 {
content := chunk.Choices[0].Message.Content
if content != "" {
fmt.Print(content)
}
}
return nil
})
if err != nil {
fmt.Printf("\nError: %v\n", err)
} else {
fmt.Println("\n\nStream complete")
}
}
func main() {
// Basic example
client := NewClient("eric")
req := ChatRequest{
Model: "gpt-5.1",
Messages: []Message{
{Role: "system", Content: "You are a helpful assistant."},
{Role: "user", Content: "Explain Go concurrency"},
},
Temperature: 0.7,
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
resp, err := client.Chat(ctx, req)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
fmt.Printf("Response: %s\n", resp.Choices[0].Message.Content)
// Run other examples
functionCallingExample()
streamingExample()
}
use reqwest;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::collections::HashMap;
use tokio_stream::StreamExt;
use futures::Stream;
// API structs
#[derive(Debug, Serialize, Deserialize)]
struct Message {
role: String,
content: Option,
tool_calls: Option>,
tool_call_id: Option,
}
#[derive(Debug, Serialize, Deserialize)]
struct ToolCall {
id: String,
r#type: String,
function: FunctionCall,
}
#[derive(Debug, Serialize, Deserialize)]
struct FunctionCall {
name: String,
arguments: String,
}
#[derive(Debug, Serialize)]
struct Tool {
r#type: String,
function: FunctionDef,
}
#[derive(Debug, Serialize)]
struct FunctionDef {
name: String,
description: String,
parameters: serde_json::Value,
}
#[derive(Debug, Serialize)]
struct ChatRequest {
model: String,
messages: Vec,
stream: Option,
temperature: Option,
max_tokens: Option,
tools: Option>,
tool_choice: Option,
parallel_tool_calls: Option,
x_codex: Option,
}
#[derive(Debug, Serialize)]
struct XCodex {
reasoning_effort: Option,
sandbox: Option,
network_access: Option,
hide_reasoning: Option,
}
#[derive(Debug, Deserialize)]
struct ChatResponse {
id: String,
object: String,
created: u64,
model: String,
choices: Vec,
usage: Usage,
}
#[derive(Debug, Deserialize)]
struct Choice {
index: u32,
message: Message,
finish_reason: String,
}
#[derive(Debug, Deserialize)]
struct Usage {
prompt_tokens: u32,
completion_tokens: u32,
total_tokens: u32,
}
// Client struct
struct API3Client {
base_url: String,
api_key: String,
client: reqwest::Client,
}
impl API3Client {
fn new(api_key: &str) -> Self {
Self {
base_url: "https://api3.exploit.bot/v1".to_string(),
api_key: api_key.to_string(),
client: reqwest::Client::new(),
}
}
async fn chat(&self, request: ChatRequest) -> Result> {
let url = format!("{}/chat/completions", self.base_url);
let mut req_body = request;
req_body.stream = Some(false);
let response = self.client
.post(&url)
.header("Authorization", format!("Bearer {}", self.api_key))
.header("Content-Type", "application/json")
.json(&req_body)
.send()
.await?;
if !response.status().is_success() {
let error_text = response.text().await?;
return Err(format!("API error: {}", error_text).into());
}
let chat_response: ChatResponse = response.json().await?;
Ok(chat_response)
}
async fn chat_stream(
&self,
request: ChatRequest,
) -> Result>>, Box> {
let url = format!("{}/chat/completions", self.base_url);
let mut req_body = request;
req_body.stream = Some(true);
let response = self.client
.post(&url)
.header("Authorization", format!("Bearer {}", self.api_key))
.header("Content-Type", "application/json")
.json(&req_body)
.send()
.await?;
if !response.status().is_success() {
let error_text = response.text().await?;
return Err(format!("API error: {}", error_text).into());
}
let byte_stream = response.bytes_stream();
let line_stream = byte_stream
.map(|result| {
result.map_err(|e| Box::new(e) as Box)
})
.scan(Vec::new(), |buffer, chunk| {
async move {
let chunk = chunk?;
buffer.extend_from_slice(&chunk);
let mut lines = Vec::new();
while let Some(newline_pos) = buffer.iter().position(|&b| b == b'\n') {
let line = buffer.drain(..=newline_pos).collect::>();
let line_str = String::from_utf8_lossy(&line[..line.len()-1]);
lines.push(line_str.to_string());
}
Some(Ok(lines))
}
})
.flat_map(futures::stream::iter);
let chunk_stream = line_stream
.filter_map(|lines_result| async move {
match lines_result {
Ok(lines) => Some(futures::stream::iter(lines).collect::>()),
Err(e) => Some(futures::stream::iter(vec![Err(e)]).collect::>()),
}
})
.flatten()
.filter_map(|line_result| async move {
match line_result {
Ok(line) => {
if line.starts_with("data: ") {
let data = &line[6..];
if data == "[DONE]" {
None
} else {
match serde_json::from_str::(data) {
Ok(chunk) => Some(Ok(chunk)),
Err(e) => Some(Err(Box::new(e))),
}
}
} else {
None
}
}
Err(e) => Some(Err(e)),
}
});
Ok(chunk_stream)
}
}
// Basic usage example
#[tokio::main]
async fn main() -> Result<(), Box> {
let client = API3Client::new("eric");
// Basic chat
let request = ChatRequest {
model: "gpt-5.1".to_string(),
messages: vec![
Message {
role: "system".to_string(),
content: Some("You are a helpful Rust assistant.".to_string()),
tool_calls: None,
tool_call_id: None,
},
Message {
role: "user".to_string(),
content: Some("Explain Rust's ownership system".to_string()),
tool_calls: None,
tool_call_id: None,
},
],
stream: None,
temperature: Some(0.7),
max_tokens: Some(500),
tools: None,
tool_choice: None,
parallel_tool_calls: None,
x_codex: None,
};
let response = client.chat(request).await?;
println!("Response: {}", response.choices[0].message.content.as_ref().unwrap());
// Function calling example
let tools = vec![
Tool {
r#type: "function".to_string(),
function: FunctionDef {
name: "execute_code".to_string(),
description: "Execute code and return result".to_string(),
parameters: json!({
"type": "object",
"properties": {
"code": {
"type": "string",
"description": "Code to execute"
},
"language": {
"type": "string",
"enum": ["python", "javascript", "rust"],
"description": "Programming language"
}
},
"required": ["code", "language"]
}),
},
},
];
let tool_request = ChatRequest {
model: "gpt-5.1".to_string(),
messages: vec![
Message {
role: "user".to_string(),
content: Some("Calculate the factorial of 10".to_string()),
tool_calls: None,
tool_call_id: None,
},
],
tools: Some(tools),
tool_choice: Some(json!("auto")),
parallel_tool_calls: Some(true),
..Default::default()
};
let tool_response = client.chat(tool_request).await?;
if let Some(tool_calls) = &tool_response.choices[0].message.tool_calls {
for tool_call in tool_calls {
println!("Function: {}", tool_call.function.name);
println!("Args: {}", tool_call.function.arguments);
// Execute function and continue conversation...
}
} else {
println!("No function calls made");
}
// Streaming example
println!("\nStreaming:");
let stream_request = ChatRequest {
model: "gpt-5.1".to_string(),
messages: vec![
Message {
role: "user".to_string(),
content: Some("Write a poem about programming".to_string()),
tool_calls: None,
tool_call_id: None,
},
],
..Default::default()
};
let mut stream = client.chat_stream(stream_request).await?;
while let Some(chunk_result) = stream.next().await {
match chunk_result {
Ok(chunk) => {
if let Some(content) = chunk.choices.get(0).and_then(|c| c.message.content.as_ref()) {
print!("{}", content);
}
}
Err(e) => {
eprintln!("Stream error: {:?}", e);
break;
}
}
}
println!("\n\nStream complete");
Ok(())
}
// Helper macro for Default implementation
macro_rules! default {
($struct_name:ident { $($field:ident: $default:expr),* }) => {
impl Default for $struct_name {
fn default() -> Self {
$struct_name {
$($field: $default),*,
..Default::default()
}
}
}
};
}
// Implement Default for ChatRequest
impl Default for ChatRequest {
fn default() -> Self {
Self {
model: String::new(),
messages: Vec::new(),
stream: None,
temperature: None,
max_tokens: None,
tools: None,
tool_choice: None,
parallel_tool_calls: None,
x_codex: None,
}
}
}
impl Default for Message {
fn default() -> Self {
Self {
role: String::new(),
content: None,
tool_calls: None,
tool_call_id: None,
}
}
}
Models support image analysis through vision capabilities. Include images as base64 data or URLs:
{
"model": "gpt-5.1-codex",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Analyze this screenshot of code"
},
{
"type": "image_url",
"image_url": {
"url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...",
"detail": "high" // "low" or "high" resolution
}
}
]
}
]
}
{
"model": "gpt-5.1-codex",
"messages": [...],
"response_format": {
"type": "json_object" // or "text"
}
}
Best practices for system messages:
{
"x_codex": {
"network_access": true, // Allow internet access
"sandbox": "read-only",
"reasoning_effort": "high",
"hide_reasoning": false // Show reasoning process
}
}
Process multiple requests concurrently using the parallel processing capability:
import asyncio
import aiohttp
async def batch_process(prompts, max_concurrent=5):
semaphore = asyncio.Semaphore(max_concurrent)
async def process(prompt):
async with semaphore:
# Make API call
return await call_api(prompt)
tasks = [process(p) for p in prompts]
results = await asyncio.gather(*tasks)
return results
import hashlib
import json
from functools import lru_cache
def cache_key(model, messages, **kwargs):
"""Generate cache key for requests"""
content = json.dumps({
'model': model,
'messages': messages,
**kwargs
}, sort_keys=True)
return hashlib.md5(content.encode()).hexdigest()
@lru_cache(maxsize=100)
def cached_response(cache_key):
"""Cache responses to identical requests"""
# API call here
pass
| Status Code | Meaning | When Occurs |
|---|---|---|
| 200 | OK | Successful request |
| 400 | Bad Request | Invalid request body or parameters |
| 401 | Unauthorized | Missing or invalid API key |
| 404 | Not Found | Model not found or invalid endpoint |
| 429 | Too Many Requests | Rate limit exceeded |
| 500 | Internal Server Error | Server error during processing |
{
"detail": {
"message": "Model 'gpt-4' is not available. Choose one of: gpt-5.1, codex-cli, ...",
"type": "invalid_request_error",
"code": "model_not_found"
}
}
{
"detail": {
"message": "Tool 'invalid_tool' not found in tools list",
"type": "invalid_request_error",
"code": "tool_not_found"
}
}
{
"detail": {
"message": "Stream connection interrupted",
"type": "stream_error",
"code": "connection_lost"
}
}
import time
import random
import asyncio
async def resilient_api_call(request_func, max_retries=5):
"""Resilient API calling with exponential backoff"""
for attempt in range(max_retries):
try:
return await request_func()
except Exception as e:
# Don't retry on client errors (4xx)
if hasattr(e, 'response') and 400 <= e.response.status_code < 500:
raise
# Calculate backoff with jitter
base_delay = 2 ** attempt
jitter = random.uniform(0, 1)
delay = min(base_delay + jitter, 30) # Cap at 30 seconds
if attempt < max_retries - 1:
print(f"Attempt {attempt + 1} failed, retrying in {delay:.2f}s...")
await asyncio.sleep(delay)
else:
raise
# Usage
async def safe_chat_request():
async def make_request():
# Your API call here
pass
return await resilient_api_call(make_request)
import logging
from dataclasses import dataclass
from typing import Optional
from datetime import datetime
@dataclass
class APIError:
timestamp: datetime
error_type: str
status_code: Optional[int]
message: str
request_id: Optional[str]
retry_count: int
class ErrorMonitor:
def __init__(self):
self.errors = []
self.logger = logging.getLogger('api_errors')
def record_error(self, error: Exception, status_code: Optional[int] = None):
error_info = APIError(
timestamp=datetime.now(),
error_type=type(error).__name__,
status_code=status_code,
message=str(error),
request_id=getattr(error, 'request_id', None),
retry_count=getattr(error, 'retry_count', 0)
)
self.errors.append(error_info)
self.logger.error(f"API Error: {error_info}")
# Check for patterns
self.check_error_patterns()
def check_error_patterns(self):
"""Analyze error patterns for insights"""
recent_errors = [e for e in self.errors
if (datetime.now() - e.timestamp).seconds < 300]
# High error rate?
if len(recent_errors) > 10:
self.logger.warning("High error rate detected!")
# Rate limiting?
rate_limits = [e for e in recent_errors if e.status_code == 429]
if len(rate_limits) > 5:
self.logger.warning("Frequent rate limiting - consider throttling")
RATE_LIMIT_PER_MINUTE environment variableX-RateLimit-Limit: 60
X-RateLimit-Remaining: 45
X-RateLimit-Reset: 1704067200
Retry-After: 60
import time
from collections import deque
import asyncio
class RateLimiter:
def __init__(self, max_requests=60, time_window=60):
self.max_requests = max_requests
self.time_window = time_window
self.requests = deque()
async def acquire(self):
"""Wait if rate limit would be exceeded"""
now = time.time()
# Remove old requests
while self.requests and self.requests[0] < now - self.time_window:
self.requests.popleft()
# Check if we can make a request
if len(self.requests) >= self.max_requests:
sleep_time = self.time_window - (now - self.requests[0])
await asyncio.sleep(sleep_time)
return await self.acquire()
self.requests.append(now)
# Usage
limiter = RateLimiter(max_requests=30, time_window=60)
async def make_api_call():
await limiter.acquire()
# Make API call here
pass
class AdaptiveRateLimiter:
def __init__(self):
self.base_limit = 60
self.current_limit = 60
self.error_count = 0
self.last_adjustment = time.time()
async def acquire(self):
now = time.time()
# Adjust limit based on recent errors
if now - self.last_adjustment > 60:
if self.error_count > 5:
self.current_limit = max(10, self.current_limit // 2)
print(f"Rate limit reduced to {self.current_limit}")
elif self.error_count == 0:
self.current_limit = min(self.base_limit, self.current_limit + 10)
self.error_count = 0
self.last_adjustment = now
await super().acquire()
def record_error(self):
self.error_count += 1
// app/api/chat/route.ts
import { NextRequest, NextResponse } from 'next/server';
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const response = await fetch('https://api3.exploit.bot/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer eric'
},
body: JSON.stringify({
model: 'gpt-5.1',
...body
})
});
if (!response.ok) {
const error = await response.json();
return NextResponse.json(
{ error: error.detail?.message || 'API Error' },
{ status: response.status }
);
}
if (body.stream) {
// Handle streaming
const reader = response.body?.getReader();
const encoder = new TextEncoder();
const readable = new ReadableStream({
async start(controller) {
while (true) {
const { done, value } = await reader!.read();
if (done) break;
controller.enqueue(value);
}
controller.close();
}
});
return new NextResponse(readable, {
headers: {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
}
});
}
const data = await response.json();
return NextResponse.json(data);
} catch (error) {
console.error('Chat API error:', error);
return NextResponse.json(
{ error: 'Internal server error' },
{ status: 500 }
);
}
}
// components/ChatComponent.tsx
'use client';
import { useState } from 'react';
export default function ChatComponent() {
const [messages, setMessages] = useState([]);
const [loading, setLoading] = useState(false);
const [input, setInput] = useState('');
const sendMessage = async () => {
if (!input.trim() || loading) return;
setLoading(true);
const userMessage = { role: 'user', content: input };
setMessages(prev => [...prev, userMessage]);
try {
const response = await fetch('/api/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
messages: [...messages, userMessage],
stream: true
})
});
const reader = response.body?.getReader();
const decoder = new TextDecoder();
let assistantMessage = { role: 'assistant', content: '' };
setMessages(prev => [...prev, assistantMessage]);
while (true) {
const { done, value } = await reader!.read();
if (done) break;
const chunk = decoder.decode(value);
const lines = chunk.split('\n');
for (const line of lines) {
if (line.startsWith('data: ')) {
const data = line.slice(6);
if (data === '[DONE]') return;
try {
const parsed = JSON.parse(data);
const content = parsed.choices[0]?.delta?.content;
if (content) {
assistantMessage.content += content;
setMessages(prev => {
const newMessages = [...prev];
newMessages[newMessages.length - 1] = assistantMessage;
return newMessages;
});
}
} catch (e) {
// Ignore parse errors
}
}
}
}
} catch (error) {
console.error('Error:', error);
} finally {
setLoading(false);
setInput('');
}
};
return (
{messages.map((msg, i) => (
{msg.content}
))}
setInput(e.target.value)}
onKeyPress={(e) => e.key === 'Enter' && sendMessage()}
placeholder="Type a message..."
disabled={loading}
/>
);
}
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
import httpx
import asyncio
app = FastAPI(title="API3 Proxy")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
class API3Client:
def __init__(self):
self.base_url = "https://api3.exploit.bot/v1"
self.api_key = "eric"
async def chat_completion(self, request_data: dict):
async with httpx.AsyncClient() as client:
response = await client.post(
f"{self.base_url}/chat/completions",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
},
json=request_data,
timeout=60.0
)
if response.status_code != 200:
raise HTTPException(
status_code=response.status_code,
detail=response.json()
)
return response.json()
api3 = API3Client()
@app.post("/chat/completions")
async def chat_completions(request: dict):
try:
# Add default model if not specified
if "model" not in request:
request["model"] = "gpt-5.1"
# Handle streaming
if request.get("stream", False):
return await StreamingResponse(api3.stream_chat(request))
return await api3.chat_completion(request)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
class StreamingResponse:
def __init__(self, stream_generator):
self.stream_generator = stream_generator
async def __aiter__(self):
async for chunk in self.stream_generator:
yield f"data: {chunk}\n\n"
yield "data: [DONE]\n\n"
# Add authentication, logging, monitoring, etc.
@app.middleware("http")
async def add_process_time_header(request, call_next):
import time
start_time = time.time()
response = await call_next(request)
process_time = time.time() - start_time
response.headers["X-Process-Time"] = str(process_time)
return response
// API3Service.java
@Service
public class API3Service {
private final RestTemplate restTemplate;
private final String BASE_URL = "https://api3.exploit.bot/v1";
private final String API_KEY = "eric";
public API3Service(RestTemplateBuilder builder) {
this.restTemplate = builder
.defaultHeader("Authorization", "Bearer " + API_KEY)
.defaultHeader("Content-Type", "application/json")
.build();
}
public ChatResponse chatCompletion(ChatRequest request) {
if (request.getModel() == null) {
request.setModel("gpt-5.1");
}
try {
return restTemplate.postForObject(
BASE_URL + "/chat/completions",
request,
ChatResponse.class
);
} catch (HttpClientErrorException e) {
throw new API3Exception(e.getResponseBodyAsString());
}
}
public Flux streamChat(ChatRequest request) {
if (request.getModel() == null) {
request.setModel("gpt-5.1");
}
request.setStream(true);
return WebClient.builder()
.baseUrl(BASE_URL)
.defaultHeader("Authorization", "Bearer " + API_KEY)
.build()
.post()
.uri("/chat/completions")
.bodyValue(request)
.retrieve()
.bodyToFlux(String.class)
.filter(line -> line.startsWith("data: "))
.map(line -> line.substring(6))
.takeWhile(data -> !data.equals("[DONE]"));
}
}
// ChatController.java
@RestController
@RequestMapping("/api/chat")
@CrossOrigin(origins = "*")
public class ChatController {
private final API3Service api3Service;
public ChatController(API3Service api3Service) {
this.api3Service = api3Service;
}
@PostMapping
public ResponseEntity chat(@RequestBody ChatRequest request) {
try {
ChatResponse response = api3Service.chatCompletion(request);
return ResponseEntity.ok(response);
} catch (API3Exception e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
}
@GetMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux> streamChat(@RequestBody ChatRequest request) {
return api3Service.streamChat(request)
.map(data -> ServerSentEvent.builder(data).build());
}
}
# Dockerfile
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
# docker-compose.yml
version: '3.8'
services:
app:
build: .
ports:
- "3000:3000"
environment:
- API3_API_KEY=eric
- API3_BASE_URL=https://api3.exploit.bot/v1
- NODE_ENV=production
depends_on:
- redis
restart: unless-stopped
redis:
image: redis:7-alpine
volumes:
- redis_data:/data
restart: unless-stopped
nginx:
image: nginx:alpine
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- ./ssl:/etc/nginx/ssl
depends_on:
- app
restart: unless-stopped
volumes:
redis_data:
API3 is fully compatible with OpenAI's official SDKs. Simply change the base URL:
# Full compatibility with version 1.0+
from openai import OpenAI
client = OpenAI(
api_key="eric",
base_url="https://api3.exploit.bot/v1"
)
# All standard methods work:
completion = client.chat.completions.create(
model="gpt-5.1",
messages=[{"role": "user", "content": "Hello!"}]
)
# Streaming
stream = client.chat.completions.create(
model="gpt-5.1",
messages=[{"role": "user", "content": "Stream me"}],
stream=True
)
# Function calling
response = client.chat.completions.create(
model="gpt-5.1",
messages=[{"role": "user", "content": "Use tools"}],
tools=[...],
tool_choice="auto"
)
import OpenAI from 'openai';
const openai = new OpenAI({
apiKey: 'eric',
baseURL: 'https://api3.exploit.bot/v1',
dangerouslyAllowBrowser: true // For browser usage
});
// Compatible with all standard methods
const completion = await openai.chat.completions.create({
model: 'gpt-5.1',
messages: [{ role: 'user', content: 'Hello!' }]
});
// Streaming
const stream = await openai.chat.completions.create({
model: 'gpt-5.1',
messages: [{ role: 'user', content: 'Stream please' }],
stream: true
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content || '');
}
// Old OpenAI code
import OpenAI from 'openai';
const openai = new OpenAI({ apiKey: 'sk-...' });
// Migrated to API3 - just change baseURL and API key!
const openai = new OpenAI({
baseURL: 'https://api3.exploit.bot/v1',
apiKey: 'eric'
});
// Everything else stays the same!
const response = await openai.chat.completions.create({
model: 'gpt-5.1', // Use available model
messages: messages
});
// You can use the /v1/responses endpoint for Anthropic-style
const response = await fetch('https://api3.exploit.bot/v1/responses', {
method: 'POST',
headers: {
'Authorization': 'Bearer eric',
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'gpt-5.1',
input: [
{ role: 'user', content: 'Hello!' }
]
})
});
Most libraries that support custom base URLs will work with API3:
from langchain.chat_models import ChatOpenAI
llm = ChatOpenAI(
model="gpt-5.1",
openai_api_base="https://api3.exploit.bot/v1",
openai_api_key="eric"
)
result = llm(["What is RAG?"])
print(result)
from llama_index import LLMPredictor, ServiceContext
llm_predictor = LLMPredictor(
llm=ChatOpenAI(
model="gpt-5.1",
openai_api_base="https://api3.exploit.bot/v1",
openai_api_key="eric"
)
)
service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor)
# In .env file
OPENAI_API_BASE=https://api3.exploit.bot/v1
OPENAI_API_KEY=eric
OPENAI_API_MODEL=gpt-5.1