Using LLM in the shebang line of a script

· Source: Simon Willison's Weblog · Field: Technology & Digital — Artificial Intelligence & Machine Learning, Software Development & Engineering · Depth: Intermediate, quick

Summary

Simon Willison investigated methods for integrating the LLM CLI tool directly into shebang lines, inspired by a Hacker News comment suggesting English text files could use shebangs. The simplest approach leverages LLM fragments, allowing direct generation requests like creating an SVG of a pelican. More advanced patterns incorporate tool calls using the `-T name_of_tool` option, enabling LLM to execute specific functions, such as fetching the current time for a haiku. The article also demonstrates executing YAML templates directly via `llm -t`, which can define custom Python functions for calculations. An example shows a `calc.sh` script performing arithmetic operations by calling `add` and `multiply` functions defined within the YAML template, outputting the result along with tool debug information.

Key takeaway

For AI Engineers developing command-line utilities, consider embedding LLM CLI commands directly into shebang lines. This approach streamlines script execution for generative tasks or tool-augmented operations, reducing boilerplate. You can define custom Python functions within YAML templates for complex logic, making your scripts more powerful and self-contained. Explore the `-f`, `-T`, and `-t` options to integrate LLM capabilities efficiently into your shell scripts.

Key insights

LLM CLI can execute generative AI tasks and tool calls directly from a script's shebang line.

Principles

Method

Integrate `llm` with `-f` for fragments, `-T` for tools, or `-t` for YAML templates directly into a shebang line to execute generative AI tasks or custom functions.

In practice

Topics

Best for: AI Engineer, Software Engineer

Related on AIssential

Open in AIssential →

Editorial summary, takeaway, and curation by AIssential. Original article published by Simon Willison's Weblog.