Skip to main content

Overview

AutoGen for .NET integrates with dotnet-interactive to execute code snippets dynamically. This enables agents to:
  • Run C# and F# code
  • Execute Python scripts (with Python kernel)
  • Run PowerShell commands
  • Access execution results
  • Handle compilation and runtime errors

Installation

Install the AutoGen.DotnetInteractive package:
For Python support, also add the dotnet-tools feed:

Basic C# Execution

1

Create a kernel

2

Execute code

Agent with Code Execution

Create an agent that can execute code snippets:

Supported Languages

C# (Default)

F#

PowerShell

Python

Python support requires Python to be installed on the system.

Code Extraction

Extract code blocks from markdown:

Complete Example: Code Assistant

Full example with multiple languages:

Error Handling

Handle compilation and runtime errors:

State Preservation

The kernel maintains state across executions:

Advanced: Custom Kernel Configuration

Integration with Group Chat

Code execution in multi-agent scenarios:

Best Practices

  • Validate and sanitize code before execution
  • Run in isolated environments for untrusted code
  • Implement timeouts to prevent infinite loops
  • Limit resource usage (memory, CPU)
  • Never execute code with elevated privileges
  • Reuse kernel instances when possible
  • Clear kernel state periodically if needed
  • Consider compilation caching for repeated code
  • Monitor memory usage for long-running kernels
  • Always validate extracted code is non-null
  • Support multiple code block formats
  • Handle mixed language documents
  • Preserve indentation and formatting

Next Steps

Function Calling

Combine code execution with function calls

Group Chat

Use code execution in multi-agent workflows

Agents

Learn more about agent capabilities

Examples

See complete code execution examples