Configuring a custom API key and setting strict execution loop limits in AgentGPT allows you to unlock advanced reasoning models while keeping recursive API token consumption under tight control. Autonomous agents break high-level objectives into recursive sub-tasks; without defined boundaries, an agent caught in an ambiguous instruction can iterate indefinitely, rapidly depleting your token budget.
This guide details step-by-step configuration for both the hosted web application and self-hosted instances, provides practical guidelines for balancing execution depth against cost, and outlines essential safeguards.
Quick Setup Overview
| Environment | API Key Configuration | Loop Limit Configuration | Credential Storage Location |
|---|---|---|---|
Hosted Web App (agentgpt.reworkd.ai) | Settings Modal → API Key input | Governed by tier limits and advanced run settings | Browser LocalStorage (client-side only) |
| Self-Hosted (Docker / Node.js) | .env variable: OPENAI_API_KEY | .env variable: NEXT_PUBLIC_MAX_LOOPS | Server environment file |
Step-by-Step Configuration: Hosted Web Application
For users operating through the official web interface, AgentGPT provides client-side configuration that uses your personal OpenAI credentials instead of the shared community queue.
Step 1: Open Settings
- Navigate to the AgentGPT web interface.
- Click the Settings (gear icon) located in the bottom-left sidebar.
Step 2: Enter Your OpenAI API Key
- Select the Settings or API tab in the pop-up modal.
- Locate the OpenAI API Key input field.
- Paste your personal OpenAI secret key (starting with
sk-...). - Select your preferred model:
- GPT-3.5-Turbo: Best for rapid prototyping, simple content outlines, and low-cost exploration.
- GPT-4 / GPT-4o: Recommended for complex, multi-stage reasoning tasks where step accuracy prevents unproductive loops.
Step 3: Adjust Model Behavior and Temperature
- Set Temperature between
0.2and0.5for research, structured synthesis, or factual workflows. Lower temperatures enforce deterministic reasoning and reduce hallucinated sub-tasks that trigger unnecessary loops. - Avoid temperatures above
0.8for autonomous workflows unless open-ended brainstorming is explicitly desired.
Step 4: Save and Verify
- Click Save.
- Deploy a brief test agent (for example: "List 3 emerging trends in renewable energy").
- Confirm that execution logs start immediately without shared queue wait times.
Storage Notice: Custom API keys entered in the hosted web UI are stored strictly in your browser's
localStorage. They are never stored permanently on the central platform servers. If you clear browser cache, switch devices, or use Incognito mode, you must re-enter your key.
Step-by-Step Configuration: Self-Hosted Instances
If you deploy AgentGPT locally via Docker or Node.js, loop constraints and API credentials are hardcoded into environment files to prevent accidental overages across all local sessions.
Step 1: Access Environment Configuration
Locate the root .env file within your cloned AgentGPT directory (or copy .env.example to .env).
Step 2: Configure the API Key
Set your OpenAI key under the core environment variables:
OPENAI_API_KEY="sk-your-openai-api-key-here"
Optional search and enrichment keys (such as SERPER_API_KEY) can also be added here if your agent requires live web search capabilities.
Step 3: Set Hard Loop Limits
Locate the platform execution threshold in .env:
# Platform Config: Maximum execution cycles per agent
NEXT_PUBLIC_MAX_LOOPS="25"
REWORKD_PLATFORM_MAX_LOOPS="25"
By default, self-hosted deployments may set this value up to 100. Lowering this to between 15 and 30 is recommended for standard tasks to prevent infinite execution traps when sub-tasks stall.
Step 4: Rebuild and Restart
Restart your containers to apply configuration changes:
docker compose down && docker compose up -d
Balancing Execution Depth vs. API Cost
Autonomous agents execute in cycles: Task Formulation → Sub-Task Execution → Evaluation → Next Task Generation. Because every iteration feeds accumulated history back into the model context window, later loops consume significantly more tokens than initial loops.
| Loop Setting Range | Typical Use Case | Expected Depth | Runaway Risk |
|---|---|---|---|
| 3 – 8 Loops | Quick definitions; single-topic summaries; initial brainstorming | Superficial to moderate; produces immediate direct output | Minimal; low token consumption |
| 10 – 20 Loops | Multi-angle research; structured technical comparisons; multi-section outlines | High; allows recursive cross-checking and refinement | Controlled; ideal operating baseline for custom keys |
| 25+ Loops | Full autonomous report generation; multi-tier code project planning | Very deep; capable of complex decomposition | High; risk of looping on failed sub-goals and context exhaustion |
Practical Guidance for Loop Management
- Start tight, expand incrementally: Run new prompts with a 10-loop threshold first. If the agent makes tangible progress but stops before finalizing the deliverable, raise the limit to 20.
- Factor in Model Efficiency: GPT-4 requires fewer loops than GPT-3.5 to achieve a complete result because it decomposes tasks more coherently on the first attempt. A 10-loop GPT-4 run frequently yields higher accuracy than a 30-loop GPT-3.5 run, often at comparable total cost due to eliminated waste.
- Active Monitoring: Always monitor the live execution feed on the right panel. If you see repeated tasks with near-identical phrasing (e.g., "Search for more details" repeated four times), click Stop Agent immediately rather than waiting for the loop ceiling.
Essential Security and Budget Safeguards
- Set Hard Spending Caps in OpenAI: Do not rely solely on AgentGPT loop limits to protect your budget. Go to
platform.openai.com→ Settings → Billing → Usage limits, and configure both a Soft Limit (notification trigger) and a strict Hard Limit (immediate API cutoff). - Revoke Keys After Shared Sessions: Because keys in the web UI persist in browser storage, always clear browser data or manually delete the key in the Settings modal when using shared or non-primary devices.
- Use Restricted API Keys: In OpenAI's developer portal, generate an API key restricted solely to Model Capabilities rather than administrative permissions, minimizing exposure in the event of local workstation compromise.