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 responsesproxy– 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 startupdisable_custom_api_key– Security flag to prevent explicitapi_keyarguments in requestsignored_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 APIworking– Boolean flag indicating if the provider is currently functionalneeds_auth–Trueif the provider requires an API key or authentication tokensupports_stream–Trueif the provider supports streaming partial responsessupports_message_history–Trueif the provider accepts full conversation historysupports_system_message–Trueif the provider understands system-role messagesparams– 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 authenticationapi_key– Provider-specific API key (falls back toAppConfigorAuthManager)api_endpoint– Override for chat completion path (default:/chat/completions)default_model– Model used when requests don't specify onefallback_models– Alternative models if model-list requests failsort_models– Boolean to alphabetically sort returned model listsmodels_needs_auth– Whether fetching available models requires authenticationuse_image_size– Enable automaticsizeparameter for image generationmax_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
- Global configuration in [
g4f/config.py](https://github.com/xtekky/gpt4free/blob/main/g4f/config.py) controls library-wide settings including timeouts, proxies, default models, and API keys via theAppConfigclass. - Provider-level attributes defined in [
g4f/providers/types.py](https://github.com/xtekky/gpt4free/blob/main/g4f/providers/types.py) allow each provider to specify capabilities such as streaming support, authentication requirements, and message history handling. - Provider-specific defaults in templates like
OpenaiTemplateand individual provider modules enable customization of endpoints, model lists, and feature flags. - Configuration can be set via environment variables with the
G4F_prefix or programmatically usingAppConfig.set_config().
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:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →