AD SPACE AVAILABLE · BRAND PARTNERS WELCOME

How to Configure Custom API Keys and Execution Loop Limits in AgentGPT

前山千鶴 · 365Finds Editorial
Indexed 88,650 Wiki articles

Put 365 Wiki to work with your AI

Copy the prompt below, paste it into the AI you already use, then ask your question.

Download full Skill

Try it with the prompt first. If you use 365 Wiki often, install the full 365Wiki Skill so you do not have to paste it again. No regular AI? Gemini or Microsoft Copilot on the web will work.

365Wiki Skill

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

EnvironmentAPI Key ConfigurationLoop Limit ConfigurationCredential Storage Location
Hosted Web App (agentgpt.reworkd.ai)Settings Modal → API Key inputGoverned by tier limits and advanced run settingsBrowser LocalStorage (client-side only)
Self-Hosted (Docker / Node.js).env variable: OPENAI_API_KEY.env variable: NEXT_PUBLIC_MAX_LOOPSServer 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

  1. Navigate to the AgentGPT web interface.
  2. Click the Settings (gear icon) located in the bottom-left sidebar.

Step 2: Enter Your OpenAI API Key

  1. Select the Settings or API tab in the pop-up modal.
  2. Locate the OpenAI API Key input field.
  3. Paste your personal OpenAI secret key (starting with sk-...).
  4. 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

Step 4: Save and Verify

  1. Click Save.
  2. Deploy a brief test agent (for example: "List 3 emerging trends in renewable energy").
  3. 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 RangeTypical Use CaseExpected DepthRunaway Risk
3 – 8 LoopsQuick definitions; single-topic summaries; initial brainstormingSuperficial to moderate; produces immediate direct outputMinimal; low token consumption
10 – 20 LoopsMulti-angle research; structured technical comparisons; multi-section outlinesHigh; allows recursive cross-checking and refinementControlled; ideal operating baseline for custom keys
25+ LoopsFull autonomous report generation; multi-tier code project planningVery deep; capable of complex decompositionHigh; risk of looping on failed sub-goals and context exhaustion

Practical Guidance for Loop Management


Essential Security and Budget Safeguards

  1. 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).
  2. 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.
  3. 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.

References