---
name: callgraph
description: Build and visualize function call graphs. Shows which functions call which other functions.
---

# Call Graph Skill

## Description

This skill builds call graphs from source code to show function call relationships. It helps understand how functions interact and identify entry points, leaf nodes, and call chains.

## When to Use

- When the user asks "who calls this function?" or "what does this function call?"
- When understanding code flow before refactoring
- When debugging to trace execution paths
- When the user wants to visualize code relationships

## How to Use

### Step 1: Determine Scope

Decide whether to analyze a single file or entire directory.

### Step 2: Build Call Graph

Use the `build_call_graph` tool:

```python
# Basic analysis
build_call_graph(path="src/main.py")

# Focus on specific function
build_call_graph(
    path="src/",
    focus_function="process_data",
    output_format="summary"
)

# Generate Mermaid diagram
build_call_graph(
    path="src/",
    output_format="mermaid"
)
```

### Step 3: Present Results

Choose output format based on user needs:
- `summary`: JSON with callers, callees, entry points
- `mermaid`: Diagram code for visualization
- `dot`: GraphViz DOT format

## Examples

### Example 1: Find Who Calls a Function

**User Request:** "Who calls the `authenticate` function?"

**Approach:**
1. Call `build_call_graph(path=".", focus_function="authenticate")`
2. Show the callers from the result
3. Show the full call chain if needed

### Example 2: Visualize Module Structure

**User Request:** "Show me a call graph for the auth module"

**Approach:**
1. Call `build_call_graph(path="src/auth/", output_format="mermaid")`
2. Present the Mermaid diagram
3. Explain key relationships

### Example 3: Find Entry Points

**User Request:** "What are the main entry points in this code?"

**Approach:**
1. Call `build_call_graph(path=".")`
2. List the `entry_points` (functions not called by others)
3. These are likely main functions, API endpoints, or event handlers

## Output Formats

### Summary (JSON)
```json
{
  "total_functions": 25,
  "total_calls": 42,
  "entry_points": ["main", "handle_request"],
  "leaf_nodes": ["log", "validate"],
  "focus": "process_data",
  "callers": ["handle_request", "batch_process"],
  "callees": ["validate", "transform", "save"]
}
```

### Mermaid
```mermaid
graph TD
    main --> process_data
    process_data --> validate
    process_data --> transform
```

## Notes

- Entry points are functions not called by any other function
- Leaf nodes are functions that don't call other functions
- Use `focus_function` to reduce noise in large codebases
- Method calls show the receiver object when available
