| layout | default |
|---|---|
| title | Chapter 1: Getting Started |
| nav_order | 1 |
| parent | Shotgun Tutorial |
Welcome to Chapter 1: Getting Started. In this part of Shotgun Tutorial: Spec-Driven Development for Coding Agents, you will build an intuitive mental model first, then move into concrete implementation details and practical production tradeoffs.
This chapter gets Shotgun running in a repository so you can generate your first spec-driven workflow.
uvx shotgun-sh@latest- Launch Shotgun in your project directory.
- Let it index the repository when prompted.
- Start with a research-oriented request before asking for implementation.
Example prompt:
Research how authentication currently works and propose a staged implementation plan for password reset.
- Python: 3.11+
- install method:
uvx shotgun-sh@latest - default interaction: TUI-first workflow
- default execution mode: Planning
You now have Shotgun running with a first research and planning loop.
Next: Chapter 2: Router Architecture and Agent Lifecycle
The FileTokenCount class in scripts/count_tokens.py handles a key part of this chapter's functionality:
class FileTokenCount(NamedTuple):
"""Token count for a single file."""
path: Path
tokens: int
chars: int
class FolderSummary(NamedTuple):
"""Token count summary for a folder."""
path: Path
files: list[FileTokenCount]
total_tokens: int
total_chars: int
def get_encoder(model: str = "cl100k_base") -> tiktoken.Encoding:
"""Get tiktoken encoder. cl100k_base is used by Claude/GPT-4."""
return tiktoken.get_encoding(model)
def count_tokens(text: str, encoder: tiktoken.Encoding | None = None) -> int:
"""Count tokens in a string."""
if encoder is None:
encoder = get_encoder()
return len(encoder.encode(text))
def count_file_tokens(This class is important because it defines how Shotgun Tutorial: Spec-Driven Development for Coding Agents implements the patterns covered in this chapter.
The FolderSummary class in scripts/count_tokens.py handles a key part of this chapter's functionality:
class FolderSummary(NamedTuple):
"""Token count summary for a folder."""
path: Path
files: list[FileTokenCount]
total_tokens: int
total_chars: int
def get_encoder(model: str = "cl100k_base") -> tiktoken.Encoding:
"""Get tiktoken encoder. cl100k_base is used by Claude/GPT-4."""
return tiktoken.get_encoding(model)
def count_tokens(text: str, encoder: tiktoken.Encoding | None = None) -> int:
"""Count tokens in a string."""
if encoder is None:
encoder = get_encoder()
return len(encoder.encode(text))
def count_file_tokens(
file_path: Path, encoder: tiktoken.Encoding
) -> FileTokenCount | None:
"""Count tokens in a single file. Returns None for binary/unreadable files."""
try:
content = file_path.read_text(encoding="utf-8")
tokens = count_tokens(content, encoder)
return FileTokenCount(path=file_path, tokens=tokens, chars=len(content))
except (UnicodeDecodeError, PermissionError):This class is important because it defines how Shotgun Tutorial: Spec-Driven Development for Coding Agents implements the patterns covered in this chapter.
The get_encoder function in scripts/count_tokens.py handles a key part of this chapter's functionality:
def get_encoder(model: str = "cl100k_base") -> tiktoken.Encoding:
"""Get tiktoken encoder. cl100k_base is used by Claude/GPT-4."""
return tiktoken.get_encoding(model)
def count_tokens(text: str, encoder: tiktoken.Encoding | None = None) -> int:
"""Count tokens in a string."""
if encoder is None:
encoder = get_encoder()
return len(encoder.encode(text))
def count_file_tokens(
file_path: Path, encoder: tiktoken.Encoding
) -> FileTokenCount | None:
"""Count tokens in a single file. Returns None for binary/unreadable files."""
try:
content = file_path.read_text(encoding="utf-8")
tokens = count_tokens(content, encoder)
return FileTokenCount(path=file_path, tokens=tokens, chars=len(content))
except (UnicodeDecodeError, PermissionError):
return None
def count_tokens_in_directory(
directory: Path,
extensions: set[str] | None = None,
) -> tuple[list[FolderSummary], int]:
"""
Count tokens for all files in a directory tree.This function is important because it defines how Shotgun Tutorial: Spec-Driven Development for Coding Agents implements the patterns covered in this chapter.
The count_tokens function in scripts/count_tokens.py handles a key part of this chapter's functionality:
Usage:
python scripts/count_tokens.py [path]
If no path is provided, defaults to .shotgun/ in current directory.
Can also be imported:
from scripts.count_tokens import count_tokens_in_directory, count_tokens
"""
import sys
from pathlib import Path
from typing import NamedTuple
import tiktoken
class FileTokenCount(NamedTuple):
"""Token count for a single file."""
path: Path
tokens: int
chars: int
class FolderSummary(NamedTuple):
"""Token count summary for a folder."""
path: Path
files: list[FileTokenCount]
total_tokens: int
total_chars: intThis function is important because it defines how Shotgun Tutorial: Spec-Driven Development for Coding Agents implements the patterns covered in this chapter.
flowchart TD
A[FileTokenCount]
B[FolderSummary]
C[get_encoder]
D[count_tokens]
E[count_file_tokens]
A --> B
B --> C
C --> D
D --> E