Introduction: Completing the Human-in-the-Loop Workflow
In the previous lesson, you completed Factor 6 by implementing pause and resume endpoints that give clients lifecycle control over agent workflows. Now, you'll implement Factor 7 — Contact humans with tool calls. As you learned when studying the 12-Factor Agents methodology, this factor treats human escalation as a first-class tool: when the agent lacks information, it doesn't guess or fail — it calls a structured ask_human tool, pauses itself, and waits for a response. The human's input is recorded in the context just like any other tool output, making the interaction auditable and reproducible. By the end of this lesson, you'll have a complete human-in-the-loop system where agents and users collaborate seamlessly to solve problems together.
Defining the ask_human Tool Schema
To allow the agent to request information from users, you need to create a new tool schema. The ask_human tool requires only one parameter: the question or prompt that the agent wants to present to the user. Create a new file at src/core/tools/schemas/ask_human.json with the following content:
JSON
{ "type": "function", "name": "ask_human", "description": "Ask the user for clarification or additional information.", "strict": true, "parameters": { "type": "object", "properties": { "question": { "type": "string", "description": "The question or prompt to ask the user" } }, "required": ["question"], "additionalProperties": false }}
This schema follows the same structure as your other tools, like sum_numbers or final_answer. The name field identifies the function as ask_human, while the description tells the language model when it should use this tool. The parameters section defines a single required field called question that holds the text the agent wants to show to the user. By keeping the schema simple with just one string parameter, you make it easy for the language model to formulate clear questions without worrying about complex argument structures.
Loading the ask_human Schema in the Agent
Now you need to load this schema and register it alongside your existing tools in the agent initialization. Open src/core/agent.py and modify the __init__ method to include the new schema:
Python
def __init__( self, model: str = "gpt-6-luna", reasoning_effort: str = "low", extra_instructions: str = "", max_steps: int = 10): self.model = model self.reasoning_effort = reasoning_effort self.max_steps = max_steps prompt_path = Path(__file__).resolve().parent / "prompts" / "base_system.md" self.system_prompt = prompt_path.read_text(encoding="utf-8") + extra_instructions schemas_dir = Path(__file__).resolve().parent / "tools" / "schemas" with open(schemas_dir / "math.json", "r", encoding="utf-8") as f: math_schemas = json.load(f) with open(schemas_dir / "final_answer.json", "r", encoding="utf-8") as f: final_answer_schema = json.load(f) # Load ask_human tool schema with open(schemas_dir / "ask_human.json", "r", encoding="utf-8") as f: ask_human_schema = json.load(f) self.tool_schemas = [ *math_schemas, final_answer_schema, ask_human_schema # Include ask_human schema to tool list ]
The code opens the new schema file and loads it as JSON, just like it does for the math and final_answer schemas. Then, it includes ask_human_schema in the self.tool_schemas list, making the tool available to the language model during each agent step.
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal
Handling ask_human in the Agent's Step Logic
When the agent calls the ask_human tool, you need special tool dispatch logic that differs from regular tools. Regular tools execute immediately and return results, while final_answer terminates the workflow. The ask_human tool should pause the workflow and wait for external input before continuing. Add a new case to the tool dispatch logic in the _next_step method:
match call_name: case "ask_human": # Remove this tool call from state.pending_tool_calls state.pending_tool_calls.remove(function_call) # Set state.status to waiting_human_input state.status = "waiting_human_input" # Return state return state case "final_answer": state.pending_tool_calls = [] state.status = "complete" state.final_answer = call_arguments.get("answer") return state # ... other cases for math tools ...
When the agent detects an ask_human call, the handler performs three steps:
Removes the call from state.pending_tool_calls to prevent reprocessing it later.
Sets state.status to waiting_human_input, which signals to the rest of your system that the agent is paused and waiting for user interaction.
Returns the state immediately without continuing the loop.
Unlike final_answer, which ends the workflow permanently, ask_human represents a temporary pause that will resume after the user provides input.
There is a critical detail to notice here: by the time the code reaches this match statement, the function_call entry — including its call_id and name — has already been appended to state.context earlier in the loop. This means the ask_human call is preserved in the context even though it's removed from pending_tool_calls. This is essential because later, when the human responds, we will search through state.context to find this exact entry and extract its call_id, linking the human's answer back to the original question.
Creating the provide_input Endpoint Request Model
To accept human responses and resume agent execution, you need to create a new API endpoint. Start by defining a Pydantic model for the request payload in src/server/main.py:
class ProvideInputRequest(BaseModel): id: str answer: str
This model requires two fields: id identifies which agent state should receive the input, and answer contains the human's response to the agent's question.
Implementing the provide_input Route Handler
Now implement the route handler that validates the request and prepares the state for resumption:
@app.post("/agent/provide_input", response_model=State)def provide_input(payload: ProvideInputRequest, background_tasks: BackgroundTasks): """Provide human input to a state waiting for human input and resume execution""" with get_db_session() as session: db_state = session.query(StateModel).filter(StateModel.id == payload.id).first() if not db_state: raise HTTPException(status_code=404, detail="State not found") # Check status while still in session if db_state.status != "waiting_human_input": raise HTTPException( status_code=400, detail=f"State is not waiting for human input. Current status: {db_state.status}" ) # Convert to Pydantic while still in session working_state = db_to_pydantic(db_state)
The endpoint opens a database session and queries for the state record matching the provided id, returning a 404 error if it does not exist. Before accepting the input, the handler validates that the state's current status is waiting_human_input. This prevents clients from accidentally sending answers to agents that are not waiting for them. After validation passes, the code converts the database record to a Pydantic model to work with in the subsequent logic.
Extracting the call_id from the Context
Before you can add the human's answer to the agent's context, you need to determine which specific ask_human call the answer corresponds to. The language model requires this information because it tracks function calls and their outputs using unique call_id values. For that, let's implement a module-level helper function in src/server/main.py that searches backwards through the context:
def _get_call_id_from_state(state: State) -> Optional[str]: """Extract the call_id from the last ask_human call in context""" # Search backwards through context to find the most recent ask_human call # We need the call_id to match the human's response with the original call for item in reversed(state.context): if isinstance(item, dict) and item.get("type") == "function_call" and item.get("name") == "ask_human": return item.get("call_id") return None
We will place this function above the provide_input route handler so it's available when the endpoint calls it. The function iterates through the context in reverse order using Python's reversed() function, which is more efficient than searching from the beginning when the ask_human call is likely near the end. For each item, it checks whether the item is a dictionary with type equal to function_call and name equal to ask_human. When it finds such an item, it extracts and returns the call_id field. If no matching call is found, the function returns None.
Using the call_id to Validate the Context
Now, back inside the provide_input route handler, you can call this helper to retrieve the call_id needed for constructing the response:
# Find the call_id from the last ask_human call# This matches the human's answer to the original ask_human function callcall_id = _get_call_id_from_state(working_state)if not call_id: raise HTTPException( status_code=400, detail="Could not find ask_human call in state context" )
After extracting the call_id, the code validates that it exists. If _get_call_id_from_state returns None, the endpoint raises a 400 error, indicating that the context is in an invalid state.
Constructing the Function Call Output and Securing the Lifecycle
With the call_id identified, you can construct the function_call_output structure that the language model expects and append it to the agent's context.
# Add the human's answer as a function_call_output to context# Format matches what the Responses API expectshuman_response = { "type": "function_call_output", "call_id": call_id, # Match with the original ask_human call "output": json.dumps({"answer": payload.answer}) # Wrap answer in JSON}working_state.context.append(human_response)# Update state in database with new contextwith get_db_session() as session: db_state = session.query(StateModel).filter(StateModel.id == payload.id).first() db_state.context = working_state.context db_state.status = "running" # Change status back to running so agent can continue# Run agent in background (non-blocking)background_tasks.add_task(_run_agent_in_background, payload.id, working_state)return working_state
The human_response dictionary links the output to the original ask_human call using the extracted call_id, and wraps the human's response in a JSON string. The endpoint then sets the status to running and schedules the background task, allowing the agent to continue seamlessly.
Important Note: Because the agent is now waiting for specific input, you must also secure your existing /agent/resume endpoint. If a client blindly calls resume while the agent is waiting for a human, the agent will loop indefinitely or fail. You should update /agent/resume to reject requests with a 400 error if the state is waiting_human_input, forcing clients to use /agent/provide_input instead. You'll do this in the upcoming exercise!
Testing the Launch and Waiting Phase
To verify that the human-in-the-loop workflow functions correctly, let's write a test script that launches an agent with an incomplete prompt and waits for the ask_human call.
import timeimport jsonimport requestsBASE_URL = "http://localhost:8000"# Launch a new agent that will likely ask for one missing value (radius/diameter)response = requests.post( f"{BASE_URL}/agent/launch", json={"input_prompt": "Can you figure out the area of a circle for me? Use pi = 3.14."})state = response.json()print(f"Launched agent with ID: {state['id']}")# Poll until it's waiting for human inputwhile True: response = requests.get(f"{BASE_URL}/agent/state/{state['id']}") current_state = response.json() print(f"Status: {current_state['status']}, Steps: {current_state['steps']}") if current_state["status"] == "waiting_human_input": question = None for item in reversed(current_state.get("context", [])): if ( isinstance(item, dict) and item.get("type") == "function_call" and item.get("name") == "ask_human" ): args = item.get("arguments") try: args_dict = json.loads(args) if isinstance(args, str) else (args or {}) except json.JSONDecodeError: args_dict = {} question = args_dict.get("question") break print(f"Question: {question}") break if current_state["status"] in ["complete", "max_steps_reached", "failed"]: print(f"Completed: {current_state.get('final_answer')}") break time.sleep(1)
Running this script produces output showing the agent launching and running until it needs human input:
Launched agent with ID: cb0636b7-003c-47ff-9c0d-2f0fb791a4bfStatus: running, Steps: 0Status: running, Steps: 1Status: running, Steps: 2Status: waiting_human_input, Steps: 2Question: What is the circle's size? Please provide either the radius or the diameter (and specify which one), and I'll compute the area using pi = 3.14.
The output shows that the agent launched successfully and ran for two steps before calling ask_human to request the missing circle measurement. The question it formulated is clear and specific, asking for either radius or diameter and mentioning that it will use pi equals three point one four as specified in the original prompt.
Testing the Input and Completion Phase
Now extend the test script to provide the human input and monitor completion:
# Provide human inputif current_state["status"] == "waiting_human_input": requests.post( f"{BASE_URL}/agent/provide_input", json={"id": state["id"], "answer": "The radius is 10 inches."} ) print("Provided input, agent resuming...") # Poll for completion while True: response = requests.get(f"{BASE_URL}/agent/state/{state['id']}") current_state = response.json() print(f"Status: {current_state['status']}, Steps: {current_state['steps']}") if current_state["status"] in ["complete", "max_steps_reached", "failed"]: print(f"Final answer: {current_state.get('final_answer')}") break time.sleep(1)
Running this continuation of the script produces output demonstrating the complete human-in-the-loop cycle:
Provided input, agent resuming...Status: running, Steps: 2Status: running, Steps: 3Status: running, Steps: 4Status: running, Steps: 5Status: running, Steps: 6Status: complete, Steps: 6Final answer: The area of the circle is 314.0 square inches (using π = 3.14 and radius = 10 inches).
After the script provides the radius of ten inches, the agent resumes execution and continues for four more steps, ultimately reaching completion with the correct calculated area of 314.0 square inches. If you examine the full context in the final state, you'll see the ask_human function call followed by the function_call_output containing the human's answer — both linked by the same call_id, providing a complete audit trail of the collaboration.
Summary
You've implemented Factor 7 — Contact humans with tool calls by building a complete human-in-the-loop system. The ask_human tool schema, the provide_input endpoint, and the call matching logic work together so that agents can request structured information from users, record the exchange in context like any other tool call, and resume seamlessly. Combined with the launch, pause, and resume endpoints from earlier lessons (Factor 6), your API now supports rich, collaborative workflows between agents and humans.
match call_name: case "ask_human": # Remove this tool call from state.pending_tool_calls state.pending_tool_calls.remove(function_call) # Set state.status to waiting_human_input state.status = "waiting_human_input" # Return state return state case "final_answer": state.pending_tool_calls = [] state.status = "complete" state.final_answer = call_arguments.get("answer") return state # ... other cases for math tools ...
Python
class ProvideInputRequest(BaseModel): id: str answer: str
Python
@app.post("/agent/provide_input", response_model=State)def provide_input(payload: ProvideInputRequest, background_tasks: BackgroundTasks): """Provide human input to a state waiting for human input and resume execution""" with get_db_session() as session: db_state = session.query(StateModel).filter(StateModel.id == payload.id).first() if not db_state: raise HTTPException(status_code=404, detail="State not found") # Check status while still in session if db_state.status != "waiting_human_input": raise HTTPException( status_code=400, detail=f"State is not waiting for human input. Current status: {db_state.status}" ) # Convert to Pydantic while still in session working_state = db_to_pydantic(db_state)
Python
def _get_call_id_from_state(state: State) -> Optional[str]: """Extract the call_id from the last ask_human call in context""" # Search backwards through context to find the most recent ask_human call # We need the call_id to match the human's response with the original call for item in reversed(state.context): if isinstance(item, dict) and item.get("type") == "function_call" and item.get("name") == "ask_human": return item.get("call_id") return None
Python
# Find the call_id from the last ask_human call# This matches the human's answer to the original ask_human function callcall_id = _get_call_id_from_state(working_state)if not call_id: raise HTTPException( status_code=400, detail="Could not find ask_human call in state context" )
Python
# Add the human's answer as a function_call_output to context# Format matches what the Responses API expectshuman_response = { "type": "function_call_output", "call_id": call_id, # Match with the original ask_human call "output": json.dumps({"answer": payload.answer}) # Wrap answer in JSON}working_state.context.append(human_response)# Update state in database with new contextwith get_db_session() as session: db_state = session.query(StateModel).filter(StateModel.id == payload.id).first() db_state.context = working_state.context db_state.status = "running" # Change status back to running so agent can continue# Run agent in background (non-blocking)background_tasks.add_task(_run_agent_in_background, payload.id, working_state)return working_state
Python
import timeimport jsonimport requestsBASE_URL = "http://localhost:8000"# Launch a new agent that will likely ask for one missing value (radius/diameter)response = requests.post( f"{BASE_URL}/agent/launch", json={"input_prompt": "Can you figure out the area of a circle for me? Use pi = 3.14."})state = response.json()print(f"Launched agent with ID: {state['id']}")# Poll until it's waiting for human inputwhile True: response = requests.get(f"{BASE_URL}/agent/state/{state['id']}") current_state = response.json() print(f"Status: {current_state['status']}, Steps: {current_state['steps']}") if current_state["status"] == "waiting_human_input": question = None for item in reversed(current_state.get("context", [])): if ( isinstance(item, dict) and item.get("type") == "function_call" and item.get("name") == "ask_human" ): args = item.get("arguments") try: args_dict = json.loads(args) if isinstance(args, str) else (args or {}) except json.JSONDecodeError: args_dict = {} question = args_dict.get("question") break print(f"Question: {question}") break if current_state["status"] in ["complete", "max_steps_reached", "failed"]: print(f"Completed: {current_state.get('final_answer')}") break time.sleep(1)
text
Launched agent with ID: cb0636b7-003c-47ff-9c0d-2f0fb791a4bfStatus: running, Steps: 0Status: running, Steps: 1Status: running, Steps: 2Status: waiting_human_input, Steps: 2Question: What is the circle's size? Please provide either the radius or the diameter (and specify which one), and I'll compute the area using pi = 3.14.
Python
# Provide human inputif current_state["status"] == "waiting_human_input": requests.post( f"{BASE_URL}/agent/provide_input", json={"id": state["id"], "answer": "The radius is 10 inches."} ) print("Provided input, agent resuming...") # Poll for completion while True: response = requests.get(f"{BASE_URL}/agent/state/{state['id']}") current_state = response.json() print(f"Status: {current_state['status']}, Steps: {current_state['steps']}") if current_state["status"] in ["complete", "max_steps_reached", "failed"]: print(f"Final answer: {current_state.get('final_answer')}") break time.sleep(1)
text
Provided input, agent resuming...Status: running, Steps: 2Status: running, Steps: 3Status: running, Steps: 4Status: running, Steps: 5Status: running, Steps: 6Status: complete, Steps: 6Final answer: The area of the circle is 314.0 square inches (using π = 3.14 and radius = 10 inches).