# Run the joint optimization
optimized_prompt, optimized_tools = joint_optimization(
    optimization_interactions,
    current_system_prompt,
    current_descriptions
)
```

The analysis identified specific improvements for both the system prompt and tool descriptions.

For example, here's how the system prompt can be enhanced:

- **Before:** "You help users manage their todo lists efficiently."
- **After:** "You help users manage their todo lists efficiently. Use specific commands like 'Add', 'Show', 'Update', 'Replace', or 'Delete' followed by the task details. For example, you can say 'Add task X' to create a new todo or 'Show my todos' to list all todos."

And here's how tool descriptions can be improved:

| Tool | Original Description | Optimized Description |
|------|---------------------|----------------------|
| `deleteTodo` | "Delete a todo" | "Delete a todo by ID. Example usage: deleteTodo(18) to remove the todo with ID 18." |
| `updateTodo` | "Update a todo" | "Update a todo by ID. Example usage: updateTodo(1, 'New details') to change the details of the todo with ID 1." |
| `createTodo` | "Create a new todo" | "Create a new todo. Example usage: createTodo('New task description') to add a new task." |
| `listTodos` | "List all todos" | "List all todos. Example usage: listTodos() to retrieve all current todos." |

### Step 5: Generate the optimized OpenAPI document

The framework automatically generated an updated OpenAPI document with Gram's `x-gram` extensions:

```yaml
paths:
  /todos/{id}:
    delete:
      operationId: deleteTodo
      summary: Delete a todo
      x-gram:
        name: deletetodo
        description: 'Delete a todo by ID. Example usage: deleteTodo(18) to remove the todo with ID 18.'
    put:
      operationId: updateTodo
      summary: Update a todo
      x-gram:
        name: updatetodo
        description: 'Update a todo by ID. Example usage: updateTodo(1, "New details") to change the details of the todo with ID 1.'
```

### Step 6: Update the Gram server

Next, we updated the Gram server configuration. To do this yourself:

1. Log in to the Gram dashboard.
2. Navigate to **Toolsets**.
3. Click on the API Source you're updating, then select **Update** in the dropdown menu.
4. Upload the optimized OpenAPI document.

![Screenshot of the Gram UI showing the API Source update process](/assets/blog/cost-aware-pass-rate/gram-api-source.png)

The new descriptions will apply to any MCP servers using tools from the updated API Source.

### Step 7: Test the optimized server

After deploying the updated server, we ran the same test queries to measure improvements. Our example query for deleting a todo item performed better with clear parameter guidance, resulting in a higher success rate and faster response time.

## Implementing CAPR and joint optimization for your tools

The [Bloomberg research repository](https://github.com/Bingo-W/ToolOptimization) contains the complete code for Bloomberg's CAPR and joint optimization framework, including benchmarks. To implement similar optimizations for your own tools, follow these steps:

1. **Analyze your interaction data:** Collect and analyze user interactions with your tools to identify common failure points and areas for improvement.
2. **Define clear tool descriptions:** Ensure that each tool has a clear and specific description, including example usage patterns.
3. **Incorporate usage examples:** Add usage examples to your tool descriptions to clarify expected input formats and behaviors.
4. **Test and iterate:** Continuously test your tools with real user queries, gather feedback, and iterate on your descriptions and prompts to improve performance.

## Risks when implementing CAPR and joint optimization

The research revealed an unexpected insight: Verbalized optimization can overfit just like traditional machine learning. After two to three optimization iterations, tool call counts began to increase again despite maintaining accuracy.

There isn't a clear solution to this problem yet, but we recommend tracking efficiency metrics closely to identify potential overfitting early.

## Practical next steps

If you're building agents and tools for your organization, consider updating your AI tool usage metrics to take cost into account. This is essential to optimizing for efficiency and effectiveness.

If you maintain MCP servers, we recommend implementing similar strategies to optimize your servers' tool descriptions. You could also make it easier for users to customize your tools' descriptions to better fit their specific needs.

At Speakeasy, we're following this research closely and building the infrastructure to enable our users to curate, measure, and optimize their MCP servers effectively.