Skip to content

Collapsing

Collapsing is the system that organizes toolkits into a hierarchical structure. Instead of exposing every function to the model simultaneously, toolkits are grouped and presented as high-level "skill" containers. The model first selects a container, then the functions inside it expand for use.

This reduces prompt length and helps models with large tool sets navigate functions more effectively.

Collapsing is enabled by default for all C# toolkits registered via AgentBuilder.


Configuration

csharp
var config = new AgentConfig
{
    Collapsing = new CollapsingConfig
    {
        Enabled = true,
        SkillInstructionMode = SkillInstructionMode.Both   // default
    }
};

Properties

PropertyTypeDefaultDescription
EnabledbooltrueEnable collapsing for C# toolkits
CollapseClientToolsboolfalseAlso collapse client-side tools (browser/remote)
MaxFunctionNamesInDescriptionint10Max function names to list in the auto-generated container description
SkillInstructionModeSkillInstructionModeBothWhere to inject skill expansion instructions
PersistSystemPromptInjectionsboolfalseKeep injected skill instructions across turns (not just the current one)
EnableErrorRecoverybooltrueAuto-expand a collapsed toolkit if the model calls a function that should be inside it
NeverCollapseHashSet<string>?nullToolkit names that should never be collapsed — always fully expanded
MCPServerInstructionsDictionary<string, string>?nullCustom instructions to inject after a specific MCP server container expands
ClientToolsInstructionsstring?nullInstructions to inject after the ClientTools container expands

SkillInstructionMode

ValueDescription
PromptMiddlewareOnlyInject skill instructions only via prompt middleware — keeps ChatOptions clean.
BothInject via both prompt middleware and directly into ChatOptions (default)

Examples

Disable collapsing for a specific toolkit

csharp
Collapsing = new CollapsingConfig
{
    NeverCollapse = new HashSet<string> { "FileToolkit", "CalculatorToolkit" }
}

These toolkits will always have all their functions directly visible to the model.

Per-server MCP instructions

csharp
Collapsing = new CollapsingConfig
{
    MCPServerInstructions = new Dictionary<string, string>
    {
        ["my-database-server"] = "Always filter results by the current user's tenant ID.",
        ["my-search-server"] = "Prefer results from the last 6 months."
    }
}

Disable collapsing entirely

csharp
Collapsing = new CollapsingConfig { Enabled = false }

All functions are exposed to the model at all times. Suitable for agents with a small number of tools.


JSON Example

json
{
    "Collapsing": {
        "Enabled": true,
        "CollapseClientTools": false,
        "MaxFunctionNamesInDescription": 10,
        "SkillInstructionMode": "Both",
        "PersistSystemPromptInjections": false,
        "EnableErrorRecovery": true,
        "NeverCollapse": ["CalculatorToolkit"],
        "ClientToolsInstructions": null
    }
}

MCPServerInstructions can also be expressed as a JSON object.


See Also

Released under the MIT License.