Skip to main content
I recently read a great blog post by Thorsten Ball on how simple it is to build coding agents and was inspired to make a python version guide here! We’ll create an LLM that can call tools that allow it to create, edit and read the contents of files and repos!

Setup

First, let’s import the necessary libraries. We’ll be using the together library to interact with the Together AI API.

Basic Chat Interaction

Let’s start with a simple loop that takes user input, sends it to a language model (LLM) using the Together AI client, and prints the LLM’s response. We’ll maintain a messages_history to keep track of the conversation context.
Usage:

Tool use by LLMs

Tool use is pretty simple - we tell the model that it has access to certain tools and instruct it to use them when it feels it would help resolve a prompt. As Thorsten says: To summarize, all there is to tools and tool use are two things:
  1. You tell the model what tools are available
  2. When the model wants to execute the tool, it tells you, you execute the tool and send the response up
To make (1) easier, the big model providers have built-in APIs to send tool definitions along. To get the intuition behind tool_use you don’t need to make any code changes - we can simply use the same chat() function above:
Pretty simple! We asked the model to use the get_weather() function if needed and it did. When it did we provided it information it wanted and it followed us by using that information to answer our original question! This is all function calling/tool-use really is!

Defining Tools for the Agent

To make this workflow of instructing the model to use tools and then running the functions it calls and sending it the response more convenient people have built scaffolding where we can pass in pre-specified tools to LLMs as follows:
Function schema:
We can now pass these function/tool into an LLM and if needed it will use it to read files! Let’s create a file first:
Now let’s see if the model can use the new read_file tool to discover the secret!
This will output a tool call from the model:

Calling Tools

Now we need to run the function that the model has asked for and feed the response back to the model, this can be done by simply checking if the model asked for a tool call and executing the corresponding function and sending the response to the model:
Output:
Above, we simply did the following:
  1. See if the model wanted us to use a tool.
  2. If so, we used the tool for it.
  3. We appended the output from the tool back into messages and called the model again to make sense of the function response.
Now let’s make our coding agent more interesting by creating two more tools!

More tools: list_files and edit_file

We’ll want our coding agent to be able to see what files exist in a repo and also modify pre-existing files as well so we’ll add two more tools:

list_files Tool: Given a path to a repo, this tool lists the files in that repo.

edit_file Tool: Edit files by adding new content or replacing old content

Incorporating Tools into the Coding Agent

Now we can add all three of these tools into the simple looping chat function we made and call it!
Output:
This code will give you a new python script: congrats.py which you can run!
Output: