Skip to content

Analyze Your First File

Time: 5 minutes | Tools: analyze_code | Tier: Community

Learn how analyze_code parses Python files and extracts structure information.

What You'll Learn

  • How analyze_code works
  • What information it extracts
  • How to interpret the results

Prerequisites

  • Code Scalpel installed
  • Sample code (see below)

The Sample Code

Create a file called calculator.py:

# calculator.py

def add(a, b):
    """Add two numbers."""
    return a + b

def divide(a, b):
    """Divide a by b."""
    return a / b

class Calculator:
    """Simple calculator class."""

    def __init__(self):
        self.history = []

    def compute(self, operation, *args):
        """Perform a calculation."""
        result = add(*args) if operation == "add" else divide(*args)
        self.history.append(result)
        return result

Step 1: Ask Your AI to Analyze

Simply ask your AI assistant:

"Analyze the structure of calculator.py"

Step 2: Understand the Tool Call

Behind the scenes, the AI calls:

{
  "tool": "analyze_code",
  "parameters": {
    "file_path": "calculator.py"
  }
}

Step 3: Interpret the Response

The tool returns structured information:

{
  "data": {
    "file_path": "calculator.py",
    "language": "python",
    "functions": [
      {
        "name": "add",
        "line_start": 3,
        "line_end": 5,
        "parameters": ["a", "b"],
        "docstring": "Add two numbers.",
        "complexity": 1
      },
      {
        "name": "divide",
        "line_start": 7,
        "line_end": 9,
        "parameters": ["a", "b"],
        "docstring": "Divide a by b.",
        "complexity": 1
      }
    ],
    "classes": [
      {
        "name": "Calculator",
        "line_start": 11,
        "line_end": 21,
        "docstring": "Simple calculator class.",
        "methods": [
          {
            "name": "__init__",
            "line_start": 14,
            "line_end": 15,
            "parameters": ["self"]
          },
          {
            "name": "compute",
            "line_start": 17,
            "line_end": 21,
            "parameters": ["self", "operation", "*args"],
            "docstring": "Perform a calculation."
          }
        ]
      }
    ],
    "imports": [],
    "complexity_total": 4
  },
  "tier_applied": "community",
  "duration_ms": 23
}

What Each Field Means

Functions

Field Meaning
name Function name
line_start / line_end Where it's defined
parameters Function arguments
docstring Documentation string
complexity Cyclomatic complexity

Classes

Field Meaning
name Class name
methods List of method definitions
docstring Class documentation

Complexity

Cyclomatic complexity measures code paths:

Score Meaning
1-5 Simple, easy to understand
6-10 Moderate complexity
11+ Complex, consider refactoring

Try It Yourself

Exercise 1: Analyze a More Complex File

Try analyzing a file from your own project:

"Analyze the structure of [your-file.py]"

Exercise 2: Compare Languages

If you have JavaScript or TypeScript files, try:

"Analyze the structure of app.js"

analyze_code supports Python, JavaScript, TypeScript, and Java.

Exercise 3: Find Complex Functions

Ask:

"Analyze my project and show me functions with complexity over 10"

Common Questions

Q: Why use this instead of reading the file?

A: analyze_code gives you structured data. Instead of parsing text, your AI gets clean JSON to work with.

Q: What languages are supported?

A: Python, JavaScript, TypeScript, Java, JSX, and TSX.

Q: Can I analyze multiple files?

A: For multiple files, use crawl_project or call analyze_code for each file.

Key Takeaways

  1. analyze_code parses files and extracts structure
  2. It returns functions, classes, imports, and complexity
  3. The AI uses this structured data to understand your code
  4. This is the foundation for other operations (extraction, modification)

Next Tutorial

Now that you can analyze code, learn to extract specific parts:

Extract a Function →