Improve ctxt_agent code