<!DOCTYPE HTML>
<html lang="en" class="navy sidebar-visible" dir="ltr">
<head>
<meta charset="UTF-8">
<title>LLM - towl Documentation</title>
<meta name="description" content="Documentation for towl - a fast CLI tool to scan codebases for TODO comments">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="../favicon.svg">
<link rel="shortcut icon" href="../favicon.png">
<link rel="stylesheet" href="../css/variables.css">
<link rel="stylesheet" href="../css/general.css">
<link rel="stylesheet" href="../css/chrome.css">
<link rel="stylesheet" href="../css/print.css" media="print">
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../fonts/fonts.css">
<link rel="stylesheet" id="highlight-css" href="../highlight.css">
<link rel="stylesheet" id="tomorrow-night-css" href="../tomorrow-night.css">
<link rel="stylesheet" id="ayu-highlight-css" href="../ayu-highlight.css">
<script>
const path_to_root = "../";
const default_light_theme = "navy";
const default_dark_theme = "navy";
window.path_to_searchindex_js = "../searchindex.js";
</script>
<script src="../toc.js"></script>
</head>
<body>
<div id="mdbook-help-container">
<div id="mdbook-help-popup">
<h2 class="mdbook-help-title">Keyboard shortcuts</h2>
<div>
<p>Press <kbd>←</kbd> or <kbd>→</kbd> to navigate between chapters</p>
<p>Press <kbd>S</kbd> or <kbd>/</kbd> to search in the book</p>
<p>Press <kbd>?</kbd> to show this help</p>
<p>Press <kbd>Esc</kbd> to hide this help</p>
</div>
</div>
</div>
<div id="body-container">
<script>
try {
let theme = localStorage.getItem('mdbook-theme');
let sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<script>
const default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? default_dark_theme : default_light_theme;
let theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('navy')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<script>
let sidebar = null;
const sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
sidebar_toggle.checked = false;
}
if (sidebar === 'visible') {
sidebar_toggle.checked = true;
} else {
html.classList.remove('sidebar-visible');
}
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="../toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="default_theme">Auto</button></li>
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search (`/`)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="/ s" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">towl Documentation</h1>
<div class="right-buttons">
<a href="../print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/glottologist/towl" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
<a href="https://github.com/glottologist/towl/edit/main/docs/src/src/api/llm.md" title="Suggest an edit" aria-label="Suggest an edit" rel="edit">
<i id="git-edit-button" class="fa fa-edit"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<div class="search-wrapper">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
<div class="spinner-wrapper">
<i class="fa fa-spinner fa-spin"></i>
</div>
</div>
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="llm"><a class="header" href="#llm">LLM</a></h1>
<p>The LLM module provides AI-powered TODO validation using Claude (Anthropic API) or any OpenAI-compatible endpoint.</p>
<h2 id="llmprovider"><a class="header" href="#llmprovider"><code>LlmProvider</code></a></h2>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>pub enum LlmProvider {
Claude(ClaudeProvider),
OpenAi(OpenAiProvider),
ClaudeCode(ClaudeCodeProvider),
Codex(CodexProvider),
}
<span class="boring">}</span></code></pre></pre>
<p>Dispatches LLM calls to the configured provider. Follows towl's existing enum dispatch pattern (<code>FormatterImpl</code>, <code>WriterImpl</code>).</p>
<h3 id="call_raw"><a class="header" href="#call_raw"><code>call_raw</code></a></h3>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>pub async fn call_raw(
&self,
user_content: &str,
system_prompt: &str,
api_key: &SecretString,
) -> Result<(String, LlmUsage), TowlLlmError>
<span class="boring">}</span></code></pre></pre>
<p>Sends a prompt to the LLM and returns the response text and token usage.</p>
<h3 id="build_provider"><a class="header" href="#build_provider"><code>build_provider</code></a></h3>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>pub fn build_provider(config: &LlmConfig) -> Result<LlmProvider, TowlLlmError>
<span class="boring">}</span></code></pre></pre>
<p>Factory function that creates the appropriate provider from configuration.</p>
<h2 id="analyse_todos"><a class="header" href="#analyse_todos"><code>analyse_todos</code></a></h2>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>pub async fn analyse_todos(
todos: &mut [TodoComment],
config: &LlmConfig,
on_progress: impl FnMut(usize, usize),
) -> Result<AnalysisSummary, TowlLlmError>
<span class="boring">}</span></code></pre></pre>
<p>Main entry point for TODO analysis. Calls <code>on_progress(completed, total)</code> after each TODO is analysed, allowing callers to render progress feedback (e.g. a progress bar).</p>
<p>For each TODO (up to <code>max_analyse_count</code>):</p>
<ol>
<li>Reads expanded context (~30 lines around the TODO + full function body)</li>
<li>Constructs a prompt with the TODO description, file path, and code context</li>
<li>Calls the LLM to determine validity</li>
<li>Parses the structured JSON response into an <code>AnalysisResult</code></li>
<li>Attaches the result to <code>TodoComment.analysis</code></li>
<li>Calls <code>on_progress</code> with the current count</li>
</ol>
<p><strong>Errors:</strong></p>
<ul>
<li><code>NotConfigured</code> -- <code>TOWL_LLM_API_KEY</code> not set</li>
<li><code>UnsupportedProvider</code> -- Provider is not "claude" or "openai"</li>
<li><code>ApiError</code>, <code>AuthError</code>, <code>RateLimited</code> -- From the LLM API</li>
</ul>
<h2 id="validity"><a class="header" href="#validity"><code>Validity</code></a></h2>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>pub enum Validity {
Valid,
Invalid,
Uncertain,
}
<span class="boring">}</span></code></pre></pre>
<p>Whether a TODO is still valid:</p>
<div class="table-wrapper"><table><thead><tr><th>Value</th><th>Meaning</th></tr></thead><tbody>
<tr><td><code>Valid</code></td><td>TODO describes work that still needs to be done</td></tr>
<tr><td><code>Invalid</code></td><td>TODO has been resolved, is irrelevant, or is nonsensical</td></tr>
<tr><td><code>Uncertain</code></td><td>Cannot determine validity from available context</td></tr>
</tbody></table>
</div>
<h2 id="analysisresult"><a class="header" href="#analysisresult"><code>AnalysisResult</code></a></h2>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>pub struct AnalysisResult {
pub validity: Validity,
pub reasoning: String,
pub is_resolved: bool,
pub is_relevant: bool,
pub is_actionable: bool,
pub confidence: f64,
pub enrichment: String,
}
<span class="boring">}</span></code></pre></pre>
<div class="table-wrapper"><table><thead><tr><th>Field</th><th>Description</th></tr></thead><tbody>
<tr><td><code>validity</code></td><td>Overall assessment</td></tr>
<tr><td><code>reasoning</code></td><td>Explanation of why the TODO is valid/invalid/uncertain</td></tr>
<tr><td><code>is_resolved</code></td><td>Whether the code already implements what the TODO asks</td></tr>
<tr><td><code>is_relevant</code></td><td>Whether the code/feature the TODO references still exists</td></tr>
<tr><td><code>is_actionable</code></td><td>Whether the TODO describes a clear, specific task</td></tr>
<tr><td><code>confidence</code></td><td>0.0-1.0 confidence in the assessment</td></tr>
<tr><td><code>enrichment</code></td><td>Enhanced description suitable for a GitHub issue body</td></tr>
</tbody></table>
</div>
<h2 id="analysissummary"><a class="header" href="#analysissummary"><code>AnalysisSummary</code></a></h2>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>pub struct AnalysisSummary {
pub valid_count: usize,
pub invalid_count: usize,
pub uncertain_count: usize,
pub error_count: usize,
}
<span class="boring">}</span></code></pre></pre>
<p>Summary counts returned by <code>analyse_todos()</code>.</p>
<h2 id="providers"><a class="header" href="#providers">Providers</a></h2>
<h3 id="claudeprovider"><a class="header" href="#claudeprovider"><code>ClaudeProvider</code></a></h3>
<p>POST to <code>https://api.anthropic.com/v1/messages</code> with headers:</p>
<ul>
<li><code>x-api-key</code>: API key</li>
<li><code>anthropic-version</code>: <code>2023-06-01</code></li>
</ul>
<p>System prompt is a top-level <code>system</code> field (not in the messages array).</p>
<h3 id="openaiprovider"><a class="header" href="#openaiprovider"><code>OpenAiProvider</code></a></h3>
<p>POST to <code>{base_url}/chat/completions</code> with <code>Authorization: Bearer {key}</code>.
Default base URL: <code>https://api.openai.com/v1</code>. Configurable for Ollama, vLLM, etc.</p>
<p>System prompt is the first message in the <code>messages</code> array with <code>role: "system"</code>.</p>
<h3 id="claudecodeprovider"><a class="header" href="#claudecodeprovider"><code>ClaudeCodeProvider</code></a></h3>
<p>Invokes the <code>claude</code> CLI as a subprocess with <code>-p --output-format json</code>. The combined system prompt and user content are passed as the final argument. No API key required.</p>
<p>Default command: <code>claude</code>. Configurable via <code>llm.command</code> and <code>llm.args</code>.</p>
<p>Auto-falls back to <code>ClaudeProvider</code> (API) if the CLI binary is not found on PATH.</p>
<h3 id="codexprovider"><a class="header" href="#codexprovider"><code>CodexProvider</code></a></h3>
<p>Invokes the <code>codex</code> CLI as a subprocess with <code>-q</code>. The combined prompt is passed as the final argument. No API key required.</p>
<p>Default command: <code>codex</code>. Configurable via <code>llm.command</code> and <code>llm.args</code>.</p>
<p>Auto-falls back to <code>OpenAiProvider</code> (API) with <code>gpt-4o</code> if the CLI binary is not found on PATH.</p>
<h3 id="is_cli_provider"><a class="header" href="#is_cli_provider"><code>is_cli_provider</code></a></h3>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>pub const fn is_cli_provider(&self) -> bool
<span class="boring">}</span></code></pre></pre>
<p>Returns <code>true</code> for <code>ClaudeCode</code> and <code>Codex</code> variants. Used to skip the API key requirement for CLI-based providers.</p>
<h2 id="configuration"><a class="header" href="#configuration">Configuration</a></h2>
<p>See <a href="../getting-started/configuration.html#llm-section">Configuration</a> for the <code>[llm]</code> config section.</p>
<h2 id="errors"><a class="header" href="#errors">Errors</a></h2>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>pub enum TowlLlmError {
ApiError { message: String, status: Option<u16> },
AuthError,
RateLimited { retry_after_secs: u64 },
ParseError { message: String },
NotConfigured,
UnsupportedProvider { provider: String },
IoError { message: String },
}
<span class="boring">}</span></code></pre></pre>
<div class="table-wrapper"><table><thead><tr><th>Variant</th><th>Cause</th></tr></thead><tbody>
<tr><td><code>ApiError</code></td><td>LLM API returned a non-200 status</td></tr>
<tr><td><code>AuthError</code></td><td>401 -- invalid or missing API key</td></tr>
<tr><td><code>RateLimited</code></td><td>429 -- too many requests</td></tr>
<tr><td><code>ParseError</code></td><td>LLM response could not be parsed as valid JSON</td></tr>
<tr><td><code>NotConfigured</code></td><td><code>TOWL_LLM_API_KEY</code> environment variable not set</td></tr>
<tr><td><code>UnsupportedProvider</code></td><td>Provider is not "claude", "openai", "claude-code", or "codex"</td></tr>
<tr><td><code>IoError</code></td><td>File I/O error during context gathering</td></tr>
</tbody></table>
</div>
<h3 id="retryable-errors"><a class="header" href="#retryable-errors">Retryable Errors</a></h3>
<p><code>TowlLlmError</code> implements <code>is_retryable()</code> which returns <code>true</code> for:</p>
<ul>
<li><code>RateLimited</code> -- always retryable</li>
<li><code>ApiError</code> with status >= 500 -- server errors</li>
<li><code>ApiError</code> with no status -- network failures</li>
</ul>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<a rel="prev" href="../api/github.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../api/processor.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="../api/github.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../api/processor.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<script>
window.playground_copyable = true;
</script>
<script src="../elasticlunr.min.js"></script>
<script src="../mark.min.js"></script>
<script src="../searcher.js"></script>
<script src="../clipboard.min.js"></script>
<script src="../highlight.js"></script>
<script src="../book.js"></script>
</div>
</body>
</html>