Implementation:Microsoft Semantic kernel OpenAIPromptExecutionSettings
| Knowledge Sources | |
|---|---|
| Domains | AI_Orchestration, LLM_Parameter_Tuning |
| Last Updated | 2026-02-11 19:00 GMT |
Overview
Concrete tool for configuring OpenAI-specific generation parameters to fine-tune LLM response behavior provided by the Microsoft Semantic Kernel library.
Description
OpenAIPromptExecutionSettings is a configuration class that encapsulates all parameters supported by the OpenAI chat completion API. It extends the base PromptExecutionSettings class with OpenAI-specific properties such as MaxTokens, Temperature, TopP, FrequencyPenalty, PresencePenalty, and ResponseFormat. These settings control how the language model generates its response, affecting output length, randomness, repetition, and structural format.
The settings object is passed to the kernel through KernelArguments, either as a constructor parameter (making it the default settings for the invocation) or through the execution settings dictionary for multi-service scenarios. At invocation time, the kernel extracts the settings and passes them to the OpenAI connector, which maps each property to the corresponding OpenAI API parameter.
Usage
Use OpenAIPromptExecutionSettings whenever you need to control the behavior of an OpenAI model beyond its defaults. Common adjustments include setting Temperature to 0 for deterministic responses, limiting MaxTokens for cost control, and setting ResponseFormat to "json_object" for structured output that can be parsed programmatically.
Code Reference
Source Location
- Repository: semantic-kernel
- File:
dotnet/src/Connectors/Connectors.OpenAI/Settings/OpenAIPromptExecutionSettings.cs:L20-591
Signature
public sealed class OpenAIPromptExecutionSettings : PromptExecutionSettings
{
public int? MaxTokens { get; set; }
public double? Temperature { get; set; }
public double? TopP { get; set; }
public double? FrequencyPenalty { get; set; }
public double? PresencePenalty { get; set; }
public object? ResponseFormat { get; set; }
public IList<string>? StopSequences { get; set; }
public long? Seed { get; set; }
// Additional properties available in the full class
}
Import
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| MaxTokens | int? |
No | Maximum number of tokens to generate in the response. Controls output length and cost. Default is model-dependent. |
| Temperature | double? |
No | Controls randomness of the output. Range: 0.0 (deterministic) to 2.0 (maximum randomness). Default: 1.0. |
| TopP | double? |
No | Nucleus sampling threshold. Only tokens within the top cumulative probability P are considered. Range: 0.0 to 1.0. Default: 1.0. |
| FrequencyPenalty | double? |
No | Penalizes tokens proportionally to how often they appear in the output. Range: -2.0 to 2.0. Default: 0.0. |
| PresencePenalty | double? |
No | Penalizes any token that has appeared at least once. Range: -2.0 to 2.0. Default: 0.0. |
| ResponseFormat | object? |
No | Controls the output format. Set to "json_object" for JSON mode, or use ChatResponseFormat for schema-constrained output. Default: text. |
| StopSequences | IList<string>? |
No | Up to 4 sequences where the model will stop generating. Useful for controlling output boundaries. |
| Seed | long? |
No | Optional seed for deterministic output (best effort by the API). Useful for reproducible testing. |
Outputs
| Name | Type | Description |
|---|---|---|
| (configuration object) | OpenAIPromptExecutionSettings |
This is a settings object, not a method. It is passed into KernelArguments and consumed by the OpenAI connector during prompt invocation. It does not return a value directly. |
Usage Examples
Controlling Temperature and Max Tokens
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;
Kernel kernel = Kernel.CreateBuilder()
.AddOpenAIChatClient(
modelId: TestConfiguration.OpenAI.ChatModelId,
apiKey: TestConfiguration.OpenAI.ApiKey)
.Build();
KernelArguments arguments = new(new OpenAIPromptExecutionSettings
{
MaxTokens = 500,
Temperature = 0.5
})
{
{ "topic", "dogs" }
};
Console.WriteLine(await kernel.InvokePromptAsync(
"Tell me a story about {{$topic}}",
arguments));
JSON Response Format
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;
KernelArguments arguments = new(new OpenAIPromptExecutionSettings
{
ResponseFormat = "json_object"
})
{
{ "topic", "chocolate" }
};
Console.WriteLine(await kernel.InvokePromptAsync(
"Create a JSON object with facts about {{$topic}}. Return valid JSON only.",
arguments));
Deterministic Output with Low Temperature
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;
KernelArguments arguments = new(new OpenAIPromptExecutionSettings
{
Temperature = 0,
Seed = 42
});
// Low temperature + seed for reproducible, deterministic responses
Console.WriteLine(await kernel.InvokePromptAsync(
"What is the capital of France?",
arguments));
Reducing Repetition with Penalties
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;
KernelArguments arguments = new(new OpenAIPromptExecutionSettings
{
MaxTokens = 1000,
Temperature = 0.8,
FrequencyPenalty = 0.5,
PresencePenalty = 0.3
});
Console.WriteLine(await kernel.InvokePromptAsync(
"Write a creative essay about the future of space exploration.",
arguments));
Using Stop Sequences
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;
KernelArguments arguments = new(new OpenAIPromptExecutionSettings
{
StopSequences = new List<string> { "\n\n", "END" },
MaxTokens = 200
});
Console.WriteLine(await kernel.InvokePromptAsync(
"Write the first paragraph of a news article about AI advancements.",
arguments));