GPT4Free Providers Configuration Options: Complete Guide to Global and Provider-Level Settings

GPT4Free (g4f) provides extensive configuration options through the AppConfig class for global runtime settings and base provider attributes for per-provider customization, all manageable via environment variables or programmatic Python APIs.

The GPT4Free library (commonly imported as g4f) offers a flexible configuration system that allows developers to control both global runtime behavior and individual provider characteristics. Understanding these gpt4free providers configuration options is essential for optimizing request handling, managing authentication, and customizing provider selection in production environments.

Global Configuration Options in GPT4Free

Global configuration in GPT4Free is managed by the AppConfig class defined in [g4f/config.py](https://github.com/xtekky/gpt4free/blob/main/g4f/config.py) (lines 37-48). These settings affect the entire library runtime, including default providers, timeouts, proxy settings, and API key management.

Core Global Settings

The following options control fundamental library behavior:

  • g4f_api_key – Master API key shared with providers requiring authentication (e.g., OpenAI, Claude)
  • timeout – Global request timeout in seconds (default varies by provider)
  • stream_timeout – Specific timeout for streaming responses
  • proxy – HTTP/HTTPS proxy URL (format: http://host:port)
  • model – Default model name when requests don't specify one (e.g., openai/gpt-4o-mini)
  • provider – Default provider class name (e.g., OpenAIChat)
  • media_provider – Provider for image/audio/video generation (e.g., PollinationsAI)
  • ignore_cookie_files – Boolean to skip loading cookie/HAR files on startup
  • disable_custom_api_key – Security flag to prevent explicit api_key arguments in requests
  • ignored_providers – List of provider class names excluded from auto-selection

Environment Variables for GPT4Free Configuration

The simplest method for configuring GPT4Free is through environment variables with the G4F_ prefix. The library automatically calls AppConfig.load_from_env() on first request:

export G4F_API_KEY="sk-xxxx"
export G4F_TIMEOUT=400
export G4F_PROXY="http://127.0.0.1:3128"
export G4F_MODEL="openai/gpt-4o-mini"
export G4F_PROVIDER="OpenAIChat"
export G4F_DISABLE_CUSTOM_API_KEY=1

Programmatic Configuration with AppConfig

For dynamic configuration within Python applications, use the set_config method:

from g4f import AppConfig

# Configure at application startup

AppConfig.set_config(
    g4f_api_key="sk-xxxx",
    timeout=300,
    proxy="http://127.0.0.1:3128",
    model="openai/gpt-4o-mini",
    provider="OpenAIChat",
    ignored_providers=["BingCreateImages", "DeepSeek"],
    disable_custom_api_key=True,
)

Provider-Level Configuration Attributes

Individual providers in GPT4Free inherit from BaseProvider defined in [g4f/providers/types.py](https://github.com/xtekky/gpt4free/blob/main/g4f/providers/types.py) (lines 7-28). These gpt4free providers configuration options control provider-specific capabilities and requirements.

Base Provider Attributes

Every provider can override these class attributes to define its behavior:

  • url – Primary endpoint URL for the provider's API
  • working – Boolean flag indicating if the provider is currently functional
  • needs_auth – True if the provider requires an API key or authentication token
  • supports_stream – True if the provider supports streaming partial responses
  • supports_message_history – True if the provider accepts full conversation history
  • supports_system_message – True if the provider understands system-role messages
  • params – Arbitrary string for router query-string parameters (rarely used)
  • live – Counter tracking successful responses for debugging/metrics

Provider-Specific Defaults and Templates

Concrete providers extend these defaults with service-specific configurations. The OpenaiTemplate (lines 18-33) demonstrates typical provider-specific gpt4free providers configuration options:

  • base_url – Primary API endpoint (must be set by subclasses)
  • backup_url – Alternate endpoint for fallback authentication
  • api_key – Provider-specific API key (falls back to AppConfig or AuthManager)
  • api_endpoint – Override for chat completion path (default: /chat/completions)
  • default_model – Model used when requests don't specify one
  • fallback_models – Alternative models if model-list requests fail
  • sort_models – Boolean to alphabetically sort returned model lists
  • models_needs_auth – Whether fetching available models requires authentication
  • use_image_size – Enable automatic size parameter for image generation
  • max_tokens – Provider-wide token limit (overridable per request)

Individual providers in g4f/Provider/ customize these values. For example, PollinationsAI sets use_image_size = True, while OpenaiChat sets needs_auth = True.

Practical Configuration Examples

Selecting Specific Providers Per Request

Bypass global defaults by passing provider classes directly to the client:

from g4f.client import Client
from g4f.Provider import Gemini, Copilot

client = Client()

# Use Gemini specifically for this request

response = client.chat.completions.create(
    model="gemini-1.5-flash",
    messages=[{"role": "user", "content": "Explain quantum computing"}],
    provider=Gemini,
)
print(response.choices[0].message.content)

Inspecting Provider Capabilities

Check provider attributes before making requests:

from g4f.Provider import OpenAIChat

print("Authentication required:", OpenAIChat.needs_auth)
print("Base URL:", OpenAIChat.base_url)
print("Supports streaming:", OpenAIChat.supports_stream)
print("Supports message history:", OpenAIChat.supports_message_history)

Security Configuration: Disabling Custom API Keys

Prevent users from overriding API keys in request arguments:

import os
os.environ["G4F_DISABLE_CUSTOM_API_KEY"] = "1"

# Or programmatically:

from g4f import AppConfig
AppConfig.set_config(disable_custom_api_key=True)

# Now any api_key= argument passed to requests will be ignored

Summary

Frequently Asked Questions

How do I set a default provider in GPT4Free?

Set the G4F_PROVIDER environment variable or use AppConfig.set_config(provider="ProviderName"). For example, export G4F_PROVIDER=OpenAIChat sets OpenAIChat as the default for all requests that don't explicitly specify a provider. This value corresponds to the provider class name in g4f/Provider/.

What is the difference between global and provider-level configuration in g4f?

Global configuration in [g4f/config.py](https://github.com/xtekky/gpt4free/blob/main/g4f/config.py) affects the entire library runtime—settings like timeouts, proxies, and default API keys apply to all providers. Provider-level configuration in [g4f/providers/types.py](https://github.com/xtekky/gpt4free/blob/main/g4f/providers/types.py) defines class-specific attributes like needs_auth, supports_stream, and url that vary by individual provider capabilities.

How can I disable streaming support for a specific provider request?

While global streaming preferences are controlled via the client, you can check a provider's streaming capability by inspecting the supports_stream attribute: print(ProviderName.supports_stream). To force non-streaming behavior, simply don't pass stream=True to the chat.completions.create() method. The provider will automatically handle the request as a standard completion regardless of its supports_stream setting.

Where are provider-specific defaults like base_url and api_key defined?

Provider-specific defaults are defined in individual provider modules under g4f/Provider/ and templates like [OpenaiTemplate.py](https://github.com/xtekky/gpt4free/blob/main/g4f/Provider/template/OpenaiTemplate.py). These files contain class-level attributes such as base_url, api_key, default_model, and fallback_models that define the default behavior for each specific provider service.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →