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.
  • Convert the output to a different format before returning it to the agent.
After Execution Modifier

Below we use the afterToolExecute 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.

1const response = await openai.chat.completions.create({
2 model: "gpt-4o-mini",
3 messages,
4 tools,
5 tool_choice: "auto",
6});
7
8const { tool_calls } = response.choices[0].message;
9console.log(tool_calls);
10
11if (tool_calls) {
12 const {
13 function: { arguments: toolArgs },
14 } = tool_calls[0];
15
16 const result = await composio.tools.execute(
17 "HACKERNEWS_GET_USER",
18 {
19 userId,
20 arguments: JSON.parse(toolArgs),
21 },
22 {
23 afterToolExecute: (toolSlug, _, result) => {
24 if (toolSlug === "HACKERNEWS_GET_USER") {
25 const { data } = result;
26 const { karma } = data.response_data as { karma: number };
27 return {
28 ...result,
29 data: { karma },
30 };
31 }
32 return result;
33 },
34 }
35 );
36 console.log(JSON.stringify(result, null, 2));
37}