Code2Prompt is a CLI tool that converts a codebase into a single LLM prompt with source tree visualization, prompt templating, and token counting.
The tool solves the problem of preparing codebases for use with large language models by automating context extraction and formatting. It ingests entire codebases and structures them into a single prompt that can be fed to LLMs like ChatGPT or Claude. The approach works by traversing the codebase, building a source tree representation, and formatting the output according to templates while tracking token usage to help users stay within model limits.
Code2Prompt suits developers who need to share codebase context with AI models, whether for manual prompting, building AI agents via Python, or running MCP servers. The tool handles the tedious work of collecting and organizing code snippets into a coherent prompt format. It offers optional features like clipboard integration and an entity-map feature for semantic code navigation that helps agents reduce exploration overhead by providing a compact map of functions and classes before reading source files.
The project shows active development with multiple installation methods supported including Cargo, Homebrew, and pip for Python SDK usage. The tool includes an agent skill for integration with coding agents, allowing them to use code2prompt for repository navigation and scoped context gathering. Documentation and community support are maintained through a website, documentation portal, and Discord community.