Skip to content

Code Summarization

In addition to the non-LLM based functions of the Repository class, kit also integrates directly with LLMs via the Summarizer class to provide intelligent code summarization capabilities. This helps you quickly understand the purpose and functionality of entire code files, specific functions, or classes.

To use code summarization, you’ll need an LLM provider configured. Currently, OpenAI, Anthropic, and Google Cloud’s Generative AI models are supported.

  1. Install Dependencies:

    Terminal window
    # Ensure you are in your project's virtual environment
    uv pip install cased-kit

    The installation includes all dependencies for OpenAI, Anthropic, and Google Cloud’s Generative AI models.

  2. Set API Key(s): Configure the API key(s) for your chosen provider(s) as environment variables:

    Terminal window
    # For OpenAI
    export OPENAI_API_KEY="sk-..."
    # For Anthropic
    export ANTHROPIC_API_KEY="sk-ant-..."
    # For Google
    export GOOGLE_API_KEY="AIzaSy..."

    You only need to set the key for the provider(s) you intend to use. If no specific configuration is provided to the Summarizer (see ‘Configuration (Advanced)’ below), kit defaults to using OpenAI via OpenAIConfig(), which expects OPENAI_API_KEY.

    For OpenAIConfig, you can also customize parameters such as the model, temperature, or base_url (e.g., for connecting to services like OpenRouter). See the ‘Configuration (Advanced)’ section for detailed examples.

The primary way to access summarization is through the Repository object’s get_summarizer() factory method.

import kit
import os
# Ensure API key is set (replace with your actual key handling)
# os.environ["OPENAI_API_KEY"] = "YOUR_API_KEY"
try:
# Load the repository
repo = kit.Repository("/path/to/your/project")
# Get the summarizer instance (defaults to OpenAIConfig using env var OPENAI_API_KEY)
# See 'Configuration (Advanced)' for using Anthropic or Google.
summarizer = repo.get_summarizer()
# Summarize a specific file
file_path = "src/main_logic.py"
summary = summarizer.summarize_file(file_path)
print(f"Summary for {file_path}:\n{summary}")
except Exception as e:
print(f"An error occurred with the LLM provider: {e}")
except Exception as e:
print(f"An error occurred: {e}")

When you call summarize_file, kit performs the following steps:

  1. Retrieves the content of the specified file using repo.get_file_content().
  2. Constructs a prompt containing the file content, tailored for code summarization.
  3. Sends the prompt to the configured LLM provider and model (e.g., OpenAI’s GPT-4o).
  4. Parses the response and returns the summary text.

While environment variables are the default, you can provide specific configuration:

from kit.summaries import OpenAIConfig, AnthropicConfig, GoogleConfig
# Load repo
repo = kit.Repository("/path/to/your/project")
# Define custom OpenAI configuration
openai_custom_config = OpenAIConfig(
api_key="sk-...", # Can be omitted if OPENAI_API_KEY is set
model="gpt-4o-mini"
)
# Get summarizer with specific OpenAI config
openai_summarizer = repo.get_summarizer(config=openai_custom_config)
# Summarize using the custom OpenAI configuration
openai_summary = openai_summarizer.summarize_file("src/utils_openai.py")
print(f"OpenAI Summary:\n{openai_summary}")

Using OpenAI-Compatible Endpoints (e.g., OpenRouter)

Section titled “Using OpenAI-Compatible Endpoints (e.g., OpenRouter)”

The OpenAIConfig also supports a base_url parameter, allowing you to use any OpenAI-compatible API endpoint, such as OpenRouter. This is useful for accessing a wide variety of models through a single API key and endpoint.

To use OpenRouter:

  1. Your api_key in OpenAIConfig should be your OpenRouter API key.
  2. Set the base_url to OpenRouter’s API endpoint (e.g., https://openrouter.ai/api/v1).
  3. The model parameter should be the specific model string as recognized by OpenRouter (e.g., meta-llama/llama-3.3-70b-instruct).
# Example for OpenRouter
openrouter_config = OpenAIConfig(
api_key="YOUR_OPENROUTER_API_KEY", # Replace with your OpenRouter key
model="meta-llama/llama-3.3-70b-instruct", # Example model on OpenRouter
base_url="https://openrouter.ai/api/v1"
)
openrouter_summarizer = repo.get_summarizer(config=openrouter_config)

anthropic_config = AnthropicConfig( api_key=“sk-ant-…”, # Can be omitted if ANTHROPIC_API_KEY is set model=“claude-3-haiku-20240307” )

google_config = GoogleConfig( api_key=“AIzaSy…”, # Can be omitted if GOOGLE_API_KEY is set model=“gemini-1.5-flash-latest” )

## Advanced Usage
### Summarizing Functions and Classes
Beyond entire files, you can target specific functions or classes:
```python
import kit
repo = kit.Repository("/path/to/your/project")
summarizer = repo.get_summarizer() # Assumes OPENAI_API_KEY is set
# Summarize a specific function
function_summary = summarizer.summarize_function(
file_path="src/core/processing.py",
function_name="process_main_data"
)
print(f"Function Summary:\n{function_summary}")
# Summarize a specific class
class_summary = summarizer.summarize_class(
file_path="src/models/user.py",
class_name="UserProfile"
)
print(f"Class Summary:\n{class_summary}")

You can combine the Summarizer with other Repository methods for powerful workflows. For example, find all classes in a file and then summarize each one:

import kit
repo = kit.Repository("/path/to/your/project")
summarizer = repo.get_summarizer()
file_to_analyze = "src/services/notification_service.py"
# 1. Find all symbols in the file
symbols = repo.extract_symbols(file_path=file_to_analyze)
# 2. Filter for classes
class_symbols = [s for s in symbols if s.get('type') == 'class']
# 3. Summarize each class
for sym in class_symbols:
class_name = sym.get('name')
if class_name:
print(f"--- Summarizing Class: {class_name} ---")
try:
summary = summarizer.summarize_class(
file_path=file_to_analyze,
class_name=class_name
)
print(summary)
except Exception as e:
print(f"Could not summarize {class_name}: {e}")
print("\n")