Building a Disk Usage Analyzer Quickly with Large Language Models
This article explains how to use a large language model to write a disk usage analysis tool. It covers how to write requirements that produce usable code, how to implement directory scanning and size aggregation, how to design safe cleanup workflows, and how to deal with cloud API latency and stability. It also reviews where AI-assisted coding helps and where human review is still needed.
1. Background: Why Disk Space Needs Analysis
Disk usage on personal computers and servers tends to grow slowly. In development environments, the most common sources are dependency caches, build outputs, container images, virtual environments, download folders, and system logs. Users often know that a disk is full but not which files or directories are using the space.
Checking manually is inefficient. Graphical disk analyzers show a rough breakdown, but developers usually need to filter by rules, generate lists in batches, and confirm before anything is deleted. A small tool can meet these needs.
Writing this kind of tool used to require familiarity with file system APIs, traversal logic, and exception handling. With LLM-assisted programming, a developer can describe the goal in natural language, have the model generate a first version, and then review and test the code. This shifts the effort toward defining requirements and verifying results, rather than toward syntax details.
2. Requirements: The Basis for Usable Generated Code
The quality of generated code depends heavily on the requirements. If you only write “help me clean up the disk,” the model has to guess the scope of deletion, and the result is hard to control. A useful requirement usually includes the following elements:
- Scan scope: Specify the root directory and whether the scan may cross mount points.
- Aggregation level: Summarize sizes by file, first-level directory, or second-level directory.
- Output format: List the largest items, with their sizes and modification times.
- Cleanup rules: Define which categories count as cache, which require manual confirmation, and which must be kept.
- Execution mode: Default to read-only, and generate a candidate list before any cleanup.
Take “disk analysis plus cleanup” as an example. A one-sentence prompt can be expanded into a structured specification. The more specific the specification, the smaller the deviation in the model’s output. After the code is generated, ask the model to add unit tests and check edge cases, such as directories without permission, symbolic link loops, and paths that contain spaces.
3. Core Logic: Scanning and Aggregation
Disk analysis is built on recursive traversal and accumulation. The following pseudocode shows the main steps. It does not depend on any particular programming language:
Input: root directory root, minimum size threshold min_size, number of results top_nInitialize an empty record tableFor each entry under root:If it is a symbolic link, record it and skip it to avoid loopsIf access is denied, log the error and continueIf it is a file:Get its size and modification timeAdd the record to the tableAdd its size to the totals of all of its parent directoriesIf it is a directory:Process that directory recursivelyRemove items smaller than min_sizeSort by size in descending order and keep the top top_n itemsOutput path, size, and modification time
Keep several points in mind during implementation. First, scanning large directories takes time, so show progress or let the user interrupt the scan. Second, separate “allocated space” from “logical file size.” Sparse files and hard links make these two values differ. Third, a permission error should not stop the whole scan. Instead, record it in a log so the user can review it later.
4. Cleanup Strategy: Safe by Default, Authorized Step by Step
The main risk in disk cleanup is deleting the wrong data. An automated tool should follow three principles: read-only by default, explicit confirmation, and traceability.
Sort candidate items into three categories:
- Rebuildable cache: For example, package manager caches and intermediate build outputs. These are usually safe to delete, but the tool should still list them for confirmation.
- Large files that need human judgment: For example, installers, virtual machine images, and backup files. The tool should only flag them and never delete them automatically.
- Content that must be kept: For example, user documents, project source code, and database files. Use an allowlist or path exclusions to prevent accidental deletion.
The cleanup process can follow four steps: generate a candidate list, show the summary, have the user confirm each category, and then delete and write a log. If deletion cannot be undone, consider moving files to a temporary directory first, then permanently removing them after a period of observation.
5. Benefits and Limits of LLM-Assisted Programming
For small tools like this, the benefits of LLMs are clear. A model can quickly generate framework code, fill in common exception handling, and suggest test cases. For unfamiliar interfaces, it can also provide example usage, which shortens the time spent searching for documentation.
The limits are equally important. A model may generate functions or parameters that look plausible but do not exist. It may also ignore platform differences, such as path separators, permission models, and recycle bin behavior. For this reason, the code must be compiled or run, and critical logic must be reviewed by a person. Deletion features should be tested first in a test directory.
It helps to treat the model as “an efficient junior implementer.” The developer remains responsible for design decisions and safety. This division of work uses the model’s speed while keeping human control over the results.
6. Service Stability: Trade-offs Between Cloud APIs and Local Deployment
When you use an LLM for development, response speed directly affects productivity. You can roughly estimate generation time with this formula:
Time ≈ output tokens ÷ output speed per second + first-packet latency
If the output speed is 30 tokens per second, generating about 3,000 tokens of code and explanation takes about 100 seconds. This does not include queuing or network delays. When speed drops, each iteration takes longer, and the developer’s focus is easier to break.
Common problems with cloud APIs include:
- Throughput drops during peak hours, which lengthens waits for long outputs.
- Connections occasionally drop, which truncates streamed output.
- Rate limits or quota changes cause requests to fail.
To handle these problems, engineering teams can take the following measures:
- Set reasonable timeouts, and use exponential backoff for retryable errors.
- Use streaming output, and save partial results as they arrive so that an interruption does not lose everything.
- Design requests to be idempotent, so that repeated execution does not cause unintended side effects.
- Record the latency and failure rate of each request on the client side, and use the data to judge service quality.
Local deployment is another option. Its advantages are that data stays on your machine, network dependence is low, and latency is predictable. The cost is a higher hardware requirement. Model parameter count, quantization precision, VRAM capacity, and memory bandwidth together determine inference speed. A practical approach is to validate your workflow on a small model first, then decide whether to upgrade hardware based on task complexity. For most individual developers, combining cloud APIs with local models is often safer than relying on a single option.
7. Checklist Before Deployment
Before you use a tool like this in daily work, check the following items:
- Does the requirement specify the scan scope, aggregation level, and output format?
- Are cleanup rules classified by risk level, and do they default to read-only?
- Does deletion require manual confirmation, and is there a log?
- Are permission errors, symbolic links, and special files handled?
- Has end-to-end testing been completed in a test directory?
- Has the LLM-generated code been compiled, run, and reviewed by a person?
- Are timeouts, retries, and streaming output configured for the model service?
By following this checklist, a developer can build a reliable disk analysis tool in a relatively short time. The tool can also be modified and extended safely when requirements change.
