After Execution Modifiers

Learn how to use after execution modifiers to transform tool results after execution.

After execution modifiers are part of Composio SDK’s powerful middleware capabilities that allow you to customize and extend the behavior of tools.

After Execution Modifiers

These modifiers are called after the tool is executed by the LLM. This allows you to modify the result of the tool before it is returned to the agent.

Useful for:

  • Modifying or truncating the output of the tool.
  • Converting the output to a different format before returning it to the agent.
After Execution Modifier

Below we use the afterExecute modifier to truncate the output of the HACKERNEWS_GET_USER tool and only return the karma of the user.

Since completion providers don’t have a function execution step — Composio will execute the tool call directly.

Hence, the modifier is configured on the tools.execute method.

1from composio import Composio, after_execute
2from composio.types import ToolExecutionResponse
3
4@after_execute(tools=["HACKERNEWS_GET_USER"])
5def after_execute_modifier(
6 tool: str,
7 toolkit: str,
8 response: ToolExecutionResponse,
9) -> ToolExecutionResponse:
10 return {
11 **response,
12 "data": {
13 "karma": response["data"]["karma"],
14 },
15 }
16
17# Get response from the LLM
18response = openai_client.chat.completions.create(
19 model="gpt-4o-mini",
20 tools=tools,
21 messages=messages,
22)
23print(response)
24
25# Execute the function calls.
26result = composio.provider.handle_tool_calls(
27 response=response,
28 user_id="default",
29 modifiers=[
30 after_execute_modifier,
31 ]
32)
33print(result)