
Add AI to Your CLI Tool with 20 Lines of Code
Add AI to Your CLI Tool with 20 Lines of Code AI-powered CLI tools are the next evolution of developer tooling. Instead of rigid commands with fixed logic, your tool can understand natural language queries, generate code, summarize data, and make intelligent suggestions. And with modern AI APIs, adding this takes surprisingly little code. This article shows how to integrate OpenAI, Anthropic, or local models into any Node.js CLI tool — from basic completions to streaming output and tool use. The Simplest Integration: 20 Lines import Anthropic from ' @anthropic-ai/sdk ' ; const client = new Anthropic (); export async function askAI ( prompt : string , context ?: string ): Promise < string > { const messages = []; if ( context ) { messages . push ({ role : ' user ' , content : context }); messages . push ({ role : ' assistant ' , content : ' I understand the context. What would you like to know? ' }); } messages . push ({ role : ' user ' , content : prompt }); const response = await clie
Continue reading on Dev.to JavaScript
Opens in a new tab



