Use deterministic tools for deterministic work
Break what you are trying to do into smaller steps. If code can handle a step reliably, write that code and give it to the agent as a tool. Do not ask a model to imitate a process that ordinary software can do reliably.