Przejdลบ do treล›ci

๐Ÿงฐ AI Shell: Tool Calling

LLM Tool Calling is the mechanism by which an AI model can request the execution of a predefined function or CLI tool. This bridges the gap between natural language and deterministic automation, making AI a safe orchestrator of shell commands.


๐ŸŽฏ What is Tool Calling?

Instead of the AI returning a guess at a shell command, it returns a structured payload describing what it wants to run:

1
2
3
4
5
6
7
{
  "name": "run_shell_command",
  "arguments": {
    "cmd": "ls -l /tmp",
    "timeout": 5
  }
}

Your application then executes this command in a controlled environment and returns the output to the AI.


๐Ÿ”ง Defining a Shell Tool for AI

Hereโ€™s how you might define a shell execution tool in a system like OpenAI Functions or Anthropic Tools:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
{
  "name": "run_shell_command",
  "description": "Execute a shell command with timeout and return stdout/stderr.",
  "parameters": {
    "type": "object",
    "properties": {
      "cmd": {
        "type": "string",
        "description": "The shell command to execute."
      },
      "timeout": {
        "type": "integer",
        "description": "Timeout in seconds (default: 10)",
        "default": 10
      }
    },
    "required": ["cmd"]
  }
}

๐Ÿ›ก๏ธ Safe Tool Execution Wrapper

Here's a minimal Python-based tool executor that ensures commands are sandboxed and logged:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# ai_shell_executor.py
import subprocess
import json
import sys

def run_shell_command(cmd: str, timeout: int = 10):
    """Execute a shell command safely."""
    try:
        result = subprocess.run(
            cmd,
            shell=True,
            capture_output=True,
            text=True,
            timeout=timeout
        )
        return {
            "stdout": result.stdout,
            "stderr": result.stderr,
            "returncode": result.returncode
        }
    except subprocess.TimeoutExpired:
        return {
            "error": f"Command timed out after {timeout} seconds",
            "returncode": 124
        }
    except Exception as e:
        return {
            "error": str(e),
            "returncode": 1
        }

if __name__ == "__main__":
    # Read JSON input from stdin
    input_data = json.load(sys.stdin)
    cmd = input_data.get("cmd")
    timeout = input_data.get("timeout", 10)

    result = run_shell_command(cmd, timeout)
    print(json.dumps(result))

You'd invoke this from your AI backend like:

1
echo '{"cmd": "ls -l /tmp", "timeout": 5}' | python3 ai_shell_executor.py

๐Ÿงช Practical Use Case: AI-Guided Debugging

Imagine an AI trying to debug a failing service:

  1. AI: "Let me check if the service is running." โ†’ Tool Call: { "name": "run_shell_command", "arguments": { "cmd": "systemctl is-active myservice" } }
  2. Executor runs the command.
  3. AI gets result: "inactive"
  4. AI: "It's not running. Let me check logs." โ†’ Tool Call: { "name": "run_shell_command", "arguments": { "cmd": "journalctl -u myservice -n 20" } }
  5. AI analyzes logs and suggests fix.

๐Ÿง  Prompting for Tool Calling

To make the AI use your tool, prompt it like this:

You are an expert Linux system administrator. You can run shell commands using the run_shell_command function. Your goal is to investigate why the nginx service is not responding. Only run one command at a time. Wait for the result before proceeding.

This trains the AI to think step-by-step and prevents it from generating a dangerous multi-command script.


๐Ÿงพ Summary

โœ… Tool Calling turns AI from a "guessing machine" into a safe, step-wise debugger. โœ… It enforces structured interaction with the system. โœ… It enables audit trails and safe execution environments. โœ… Itโ€™s essential for production-grade AI-shell integration.


๐Ÿงพ See Also