Should I file a patent before signing an NDA or starting a pilot?
Yes. An NDA keeps a disclosure private; it does not give you any rights in the invention, and it does nothing if the counterparty independently develops, or already holds, the same idea. Filing a first application before the pilot fixes a priority date that the pilot cannot disturb, however many of the customer's engineers see the system, and it lets you disclose more freely during the pilot, which usually makes the pilot go better. It also changes the negotiation: a customer will happily sign an IP clause that assigns "all inventions arising from the project" when nothing is filed, and will accept a carve-out for your pre-existing, patent-pending technology when something is.
Pilots create three specific risks that an NDA does not cover. The customer's staff may contribute to an improvement and become joint inventors, giving their employer a claim on the resulting patent. The customer may deploy the system in a way that is visible to the public or to other suppliers, which can be a public disclosure. And the customer's standard contract may contain a work-product clause that assigns everything created during the engagement to them. A filed application answers the first and second and gives you a defined thing to exclude in the third. In practice: file a provisional or priority application covering the core mechanism before kick-off, get the NDA signed before any technical detail is shared, and make sure the pilot agreement identifies your background IP by reference to that application. If the pilot has already started without a filing, file now; disclosures under the NDA are not public, so the position is usually still recoverable. See also Who owns IP created in a pilot or joint development project?