Skip to content

Apply Patch#

AG2 Shell Tool Integration: Command Execution and Multi-Tool DevOps Orchestration

AG2's shell tool integration with OpenAI's Responses API enables agents to execute shell commands directly, unlocking powerful automation capabilities for filesystem operations, build processes, and system diagnostics. Combined with the apply_patch tool, you can orchestrate complete DevOps pipelines—from project creation to deployment validation—all within a single agent workflow.

This article explores how to leverage AG2's built-in tools for command execution, file operations, and multi-tool orchestration, with practical examples for automating development workflows and building production-ready DevOps pipelines.

{/ more /}

Traditional agent workflows often require custom integrations for every system operation—file creation, command execution, testing, and deployment each need separate tooling. AG2's built-in tools eliminate this complexity by providing native support for:

  • Shell command execution: Run any shell command through a controlled interface
  • Structured file operations: Create, update, and delete files with precise control
  • Multi-tool orchestration: Combine tools seamlessly in agent workflows
  • Security controls: Built-in protection against dangerous commands

Key Features:

  • Native Shell Integration: Execute shell commands directly through OpenAI's Responses API

  • Multi-Tool Support: Use apply_patch and shell tools together in the same workflow

  • Security Controls: Configure dangerous patterns, allowed commands, and denied commands

  • DevOps Automation: Complete pipeline orchestration from code to deployment

  • Concurrent Execution: Multiple commands can run simultaneously within a single shell call

  • Production Ready: Built-in safeguards and validation for production deployments

Why This Matters:

Building automated development workflows traditionally requires complex integrations, custom scripts, and manual coordination between different tools. AG2's built-in tools provide a unified interface that enables agents to handle the entire software development lifecycle—from initial project setup through testing, building, and deployment—with intelligent routing and error handling.

When to Use Built-in Tools:

Use AG2's built-in tools when you need:

  • Filesystem Operations: Create, modify, or delete files programmatically
  • Command Execution: Run tests, builds, deployments, or system diagnostics
  • DevOps Automation: Orchestrate complete CI/CD pipelines
  • Multi-Step Workflows: Chain file operations and commands together
  • Development Automation: Automate repetitive development tasks

Don't use built-in tools for simple text generation or when you need custom tool integrations that aren't supported.

Understanding Built-in Tools

AG2 provides two powerful built-in tools that work seamlessly with OpenAI's Responses API:

1. Shell Tool: Executes shell commands through your system's command-line interface - Supports concurrent command execution - Includes timeout and output length limits - Provides security controls for dangerous commands - Works on Mac/Linux and Windows

2. Apply Patch Tool: Performs structured file operations - Create new files with content - Update existing files using unified diff format - Delete files when needed - Maintains file structure and formatting

Together, these tools enable complete automation of software development workflows.

GPT-5.1 Apply Patch Tool: Automated Code Editing in AG2

AG2 now supports the apply_patch tool (from GPT-5.1 onward) through OpenAI's Responses API, enabling agents to create, update, and delete files using structured diffs. This integration brings precise, controlled file operations directly into your agent workflows.

This article explores how to use apply_patch in AG2, with practical examples for automated code editing, project scaffolding, and multi-file refactoring.

What is Apply Patch?

The apply_patch tool is a built-in capability in GPT-5.1 and above models that enables agents to perform structured file operations using unified diff format. Unlike traditional code generation approaches where agents output raw code blocks that you must manually integrate, apply_patch provides a standardized interface for file modifications that can be directly applied to your codebase.

The tool handles three core operations: - create_file: Generate new files with specified content - update_file: Modify existing files using unified diff format - delete_file: Remove files from the workspace

Why Apply Patch Matters:

Traditional agent-based code generation often requires manual intervention—you receive code blocks, review them, and manually integrate changes. Apply patch transforms this workflow by enabling agents to: - Make precise, targeted changes using diff format - Handle multi-file operations in a single interaction - Provide structured, reviewable changes before application - Support iterative refinement through feedback loops

When to use Apply Patch:

Use apply_patch when you need: - Multi-file refactoring: Renaming symbols, extracting helpers, or reorganizing modules across multiple files - Project scaffolding: Generating complete project structures with multiple files and directories - Iterative code improvement: Making incremental changes based on feedback or test results - Automated code fixes: Applying structured fixes to codebases based on linter output or error messages - Documentation and test generation: Creating test files, fixtures, and documentation alongside code changes

Don't use apply_patch for simple, single-file code generation where direct code output is sufficient—standard AG2 code generation patterns are more efficient for that.

Note: AG2's apply_patch implementation integrates with OpenAI's Responses API. For protocol-level details, see OpenAI's Apply Patch documentation.