Setup Environment

mkdir humanlayer-demo && cd humanlayer-demo

Install the required package with pip:

pip install humanlayer openai

Next, get your OpenAI API key from the OpenAI dashboard.

export OPENAI_API_KEY=<your-openai-api-key>

and set your HumanLayer API key

export HUMANLAYER_API_KEY=<your-humanlayer-api-key>

Create a simple agent

First, let’s create a simple math agent that can perform basic arithmetic operations.

You can run this script with:

python main.py

Or, with uv:

uv run main.py

You should see output like:

INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
INFO:__main__:last message led to 1 tool calls: [('multiply', '{"x":2,"y":5}')]
INFO:__main__:CALL tool multiply with {'x': 2, 'y': 5}
INFO:__main__:tool multiply responded with 10
INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
INFO:__main__:last message led to 1 tool calls: [('add', '{"x":10,"y":32}')]
INFO:__main__:CALL tool add with {'x': 10, 'y': 32}
INFO:__main__:tool add responded with 42
INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"


----------Result----------


The result of multiplying 2 and 5, and then adding 32, is 42.

Sure, not a particularly useful agent, but enough to get you started with tool calling.

Add HumanLayer

Now, let’s add a human approval step to the script to see how it works.

We’ll wrap the multiply function with @hl.require_approval() so that the LLM has to get your approval before it can run the function.

At the top of the script, import HumanLayer and instantiate it:

from humanlayer import HumanLayer

hl = HumanLayer(
    verbose=True,
    run_id="openai-math-example",
)

Then, wrap the multiply function with @hl.require_approval():

  def add(x: int, y: int) -> int:
      """Add two numbers together."""
      return x + y

+ @hl.require_approval()
  def multiply(x: int, y: int) -> int:
      """multiply two numbers"""
      return x * y

Now, when you run the script, it will prompt you for approval before performing the multiply operation.

Since we chose verbose mode, you’ll see a note in the console.

INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
INFO:__main__:last message led to 1 tool calls: [('multiply', '{"x":2,"y":5}')]
INFO:__main__:CALL tool multiply with {'x': 2, 'y': 5}
HumanLayer: waiting for approval for multiply

Try rejecting the tool call with something like “use 7 instead of 5” to see how your feedback is passed into the agent.

You should see some more output:

HumanLayer: human denied multiply with message: use 7 instead of 5
INFO:__main__:tool multiply responded with "User denied multiply with message: use 7 instead of 5"
INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
INFO:__main__:last message led to 1 tool calls: [('multiply', '{"x":2,"y":7}')]
INFO:__main__:CALL tool multiply with {'x': 2, 'y': 7}
HumanLayer: waiting for approval for multiply

This time, you can approve the tool call.

That’s it, you’ve now added a human approval step to your tool-calling agent!

Connect Slack or try an email approval

Head over to the integrations panel to connect Slack as an approval channel and run the script again.

Or you can try an email approval by setting the contact channel in @require_approval.

Integrate your favorite Agent Framework

HumanLayer is designed to work with any LLM agent framework. See Frameworks for more information.