Add error handling to agent