Skip to content

API AIConfig

Amayyas edited this page Jul 6, 2026 · 2 revisions

API Reference: AIConfig

Configuration class for AI requests.

Constructor

const AIConfig({
  required String apiKey,
  String? model,
  int? maxTokens,
  double? temperature,
  double? topP,
  double? frequencyPenalty,
  double? presencePenalty,
  List<String>? stopSequences,
  String? systemPrompt,
  List<Tool>? tools,
  ToolChoice? toolChoice,
  ResponseFormat? responseFormat,
  String? baseUrl,
  Duration? timeout,
  Map<String, String>? headers,
  Map<String, dynamic>? metadata,
})

Properties

apiKey

final String apiKey

Required. API key for authentication with the provider.


model

final String? model

The model to use. If not specified, uses provider's default.

Provider Default Model
OpenAI gpt-5.4-mini
Anthropic claude-sonnet-5
Google AI gemini-3.1-flash-lite

maxTokens

final int? maxTokens

Maximum number of tokens to generate in the response.


temperature

final double? temperature

Sampling temperature (0.0 to 2.0). Higher values = more random.

Value Behavior
0.0 Deterministic, focused
0.7 Balanced (default)
1.0+ Creative, varied

topP

final double? topP

Nucleus sampling parameter (0.0 to 1.0). Alternative to temperature.


frequencyPenalty

final double? frequencyPenalty

Reduces repetition of frequent tokens (-2.0 to 2.0).


presencePenalty

final double? presencePenalty

Reduces repetition of any used tokens (-2.0 to 2.0).


stopSequences

final List<String>? stopSequences

Sequences that stop text generation.

final config = AIConfig(
  apiKey: 'key',
  stopSequences: ['END', '\n\n', '```'],
);

systemPrompt

final String? systemPrompt

System instructions for the AI.

final config = AIConfig(
  apiKey: 'key',
  systemPrompt: 'You are a helpful coding assistant. Always provide examples.',
);

tools

final List<Tool>? tools

Available tools/functions for the AI to call.


toolChoice

final ToolChoice? toolChoice

How the model should use tools.

ToolChoice.auto       // Let the model decide (default)
ToolChoice.none       // Never use tools
ToolChoice.required   // Must use a tool
ToolChoice.function('name')  // Use specific tool

responseFormat

final ResponseFormat? responseFormat

Output format configuration.

ResponseFormat.text        // Plain text (default)
ResponseFormat.jsonObject  // JSON output

baseUrl

final String? baseUrl

Custom base URL for the API (for proxies or custom endpoints).

final config = AIConfig(
  apiKey: 'key',
  baseUrl: 'https://my-proxy.com/v1',
);

timeout

final Duration? timeout

Request timeout duration. Default is 60 seconds.

final config = AIConfig(
  apiKey: 'key',
  timeout: Duration(seconds: 120),
);

headers

final Map<String, String>? headers

Custom HTTP headers to include in requests.

final config = AIConfig(
  apiKey: 'key',
  headers: {
    'X-Custom-Header': 'value',
    'X-Request-ID': uuid(),
  },
);

metadata

final Map<String, dynamic>? metadata

Additional metadata for custom purposes.


Methods

copyWith

Creates a copy with updated fields.

AIConfig copyWith({
  String? apiKey,
  String? model,
  int? maxTokens,
  double? temperature,
  double? topP,
  double? frequencyPenalty,
  double? presencePenalty,
  List<String>? stopSequences,
  String? systemPrompt,
  List<Tool>? tools,
  ToolChoice? toolChoice,
  ResponseFormat? responseFormat,
  String? baseUrl,
  Duration? timeout,
  Map<String, String>? headers,
  Map<String, dynamic>? metadata,
})
final baseConfig = AIConfig(
  apiKey: 'key',
  model: 'gpt-5.5',
  temperature: 0.7,
);

// Create variations
final creativeConfig = baseConfig.copyWith(temperature: 1.2);
final fastConfig = baseConfig.copyWith(model: 'gpt-5.4-mini');

Examples

Minimal Configuration

final config = AIConfig(
  apiKey: 'your-api-key',
);

Full Configuration

final config = AIConfig(
  apiKey: 'your-api-key',
  model: 'gpt-5.5',
  maxTokens: 2000,
  temperature: 0.7,
  topP: 0.9,
  frequencyPenalty: 0.5,
  presencePenalty: 0.5,
  stopSequences: ['END'],
  systemPrompt: 'You are a helpful assistant.',
  tools: [weatherTool, calculatorTool],
  toolChoice: ToolChoice.auto,
  responseFormat: ResponseFormat.text,
  timeout: Duration(seconds: 60),
  headers: {'X-Request-ID': 'abc123'},
  metadata: {'user_id': '12345'},
);

Code Generation Config

final codeConfig = AIConfig(
  apiKey: 'key',
  model: 'gpt-5.5',
  temperature: 0.0,  // Deterministic
  maxTokens: 2000,
  stopSequences: ['```\n\n'],
  systemPrompt: '''
You are a code generator. 
- Output only valid, working code
- Include comments
- Follow best practices
''',
);

Creative Writing Config

final creativeConfig = AIConfig(
  apiKey: 'key',
  model: 'claude-opus-4-8',
  temperature: 1.0,
  topP: 0.95,
  maxTokens: 4000,
  systemPrompt: 'You are a creative storyteller.',
);

JSON Output Config

final jsonConfig = AIConfig(
  apiKey: 'key',
  model: 'gpt-5.5',
  temperature: 0.3,
  responseFormat: ResponseFormat.jsonObject,
  systemPrompt: '''
Always respond with valid JSON in this format:
{
  "answer": "your answer",
  "confidence": 0.0-1.0,
  "sources": ["list", "of", "sources"]
}
''',
);

Related

Clone this wiki locally