Surgical code isolation for AI agents
Project description
Vial
Surgical code isolation for AI agents.
Vial extracts a single function or class from a large Python file into a clean, focused workspace file so an agent only sees — and edits — exactly what it needs to.
Install
pip install vial-ml
# or with uv:
uv add vial-ml
CLI
# Isolate a function
vial extract billing.py process_payment
# Agent edits .vial_workspace/process_payment_isolated.py ...
# Stitch it back (validates syntax first)
vial merge
Python API
from vial import Vial
v = Vial()
isolated_path = v.extract("billing.py", "process_payment")
# agent edits isolated_path ...
v.merge()
Token efficiency
Vial reduces the context an agent sees to only what matters. On a representative 175-line module:
Target Isolated tokens Full-file tokens Savings
----------------------------------------------------------------
compute_hash ~139 ~1,172 88% ████████████████████████████░░░
read_config ~148 ~1,172 87% ███████████████████████████░░░░
write_config ~156 ~1,172 87% ███████████████████████████░░░░
ConnectionPool ~297 ~1,172 75% ███████████████████████░░░░░░░░
ConfigManager ~290 ~1,172 75% ███████████████████████░░░░░░░░
EventBus ~337 ~1,172 71% ██████████████████████░░░░░░░░░
Token estimate: 1 token ≈ 4 characters (standard approximation for code).
Run it yourself
git clone https://github.com/Madara-Labs/vial-ml
cd vial-ml
uv run python benchmarks/run_benchmark.py
No API key needed — the benchmark uses only local file analysis.
How this compares to standard approaches
Every AI coding tool has to solve the same problem: the model needs to see relevant code without consuming the entire codebase. Here's how common approaches compare to Vial.
Claude Code / Gemini CLI — shell-tool exploration
Terminal-native agents hand the model shell tools (grep, find, cat) and let it explore the repo the way a new hire would — searching for keywords, opening files one at a time. For a question like "what calls process_payment?", an agent might open 10–25 files and burn tens of thousands of tokens before writing a single line. The model sees everything it found, not just what it needs.
Independent measurements on real codebases put Claude Code's grep-only mode at 108,000–117,000 tokens for a single targeted task (source). Those measurements were on larger codebases than Vial's benchmark fixture — direct comparison isn't apples-to-apples — but the dynamic is the same: the agent reads far more than it needs.
GitHub Copilot — cursor-proximity windowing
Copilot focuses context on the area around the cursor and recently open files. This is fast and works well for completions, but the agent has no understanding of where a function starts and ends — it sends a fixed character window, which may include half a function above and half a function below the target. Inline completions use an 8–16k token local window; agent mode uses the full model context (source).
Cursor — RAG + embeddings
Cursor indexes the codebase and retrieves semantically relevant chunks via embeddings. Better than raw grep, but retrieval returns chunks (fixed line ranges), not semantic units (full function/class boundaries). A retrieved chunk can cut off mid-function, leaving the agent with incomplete context (source).
Aider — whole-file map
Aider builds a repo-map summarising every file's symbols and sends relevant whole files to the model. More precise than grep, but the unit of context is still the whole file. A 500-line file gets sent even if only a 20-line function needs changing. With structural retrieval Aider uses 8,500–13,000 tokens per task — better than grep-mode agents, but still file-scoped (source).
Vial — AST-bounded isolation
Vial uses Python's AST to find the exact start and end line of the target, so the isolated file always contains exactly one complete, syntactically valid function or class — nothing more. Surrounding code is never included, never at risk of accidental modification, and never charged as tokens.
Approach Context unit Risk of side-effects Token cost
──────────────────────────────────────────────────────────────────────
grep / cat Whole file High High
Cursor RAG Fixed-line chunk Medium (cut-off risk) Medium
Aider map Whole file Medium Medium-High
Vial Single AST node None Low (71–88% less)
The tradeoff: Vial works at the function/class level. It is not a replacement for tools that handle cross-file refactors or project-wide search — it is the right tool once you know what to edit.
How it works
- Extract — AST-based line-bound finder locates the exact start/end of the target. A protected context header (imports + module-level globals, commented out) is prepended so the agent has the context it needs without being able to accidentally modify it.
- Edit — The agent modifies only the isolated file.
- Merge — Vial validates the modified code's syntax, then splices it back into the original file at the exact line range. Surrounding code is never touched.
All session state lives in a metadata.json file inside the workspace directory.
Dependencies
| Package | Purpose |
|---|---|
typer |
CLI interface |
The standard library covers everything else: ast for parsing, pathlib for file I/O, json for workspace state.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file vial_ml-0.1.4.tar.gz.
File metadata
- Download URL: vial_ml-0.1.4.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eba213510f2e6f70bdf5a21cbf78243aaf7f2eabd3642bbecfb74e835be3a939
|
|
| MD5 |
f21d2c60534d76049648d5ca55f09375
|
|
| BLAKE2b-256 |
42fefd1f5a2ac47bbbbc141431cbc1c39a5d01a6a78197f80c2fc5423f51033b
|
File details
Details for the file vial_ml-0.1.4-py3-none-any.whl.
File metadata
- Download URL: vial_ml-0.1.4-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d8ce08e621e6b2f25abd7e7c5438377887344990b38105feaba441be4e25938e
|
|
| MD5 |
ea5bb1f05d42df47b159f3fe2373536e
|
|
| BLAKE2b-256 |
ca82340aa3ba067eff1077a603f21e3d81e565e55639f95be2971e94018cc426
|