AI agents need a way to act on the world, and two common primitives are tool calling and code execution. Tool calling restricts the model to a set of predefined functions, while code execution lets it generate and run arbitrary scripts. The choice affects reliability, safety, and flexibility.
The article from Machine Learning Mastery grounds this theory in practice by using the same get_weather function, backed by Open-Meteo, for both examples. Running against a real API makes the trade-offs concrete: tool calling offers structure and validation, whereas code execution offers expressiveness at the cost of more risk.
Developers should weigh these trade-offs based on their use case. When actions are well-defined, tool calling is likely the safer default; when tasks are open-ended, code execution may be necessary. The source does not take a hard stance, instead demonstrating both with the same API.