It’s been 159 years since trains first ran from San Francisco to San Jose. Now the oldest continually operating rail line west of the Mississippi is on the verge of a 21st-century transformation.

Caltrain’s brand-new electrified trains are in the Bay Area, and passersby might catch a glimpse of the gleaming red- and-white fleet quietly sailing through Silicon Valley on test runs. By late 2024, these trains will phase out the agency’s loud and polluting diesel-powered locomotives on the 51-mile stretch to San Jose’s Tamien Station.

In their place are 19 trains built by a Swiss company that set up shop in the windswept plains of Salt Lake City. Eventually, passengers will glide up the Peninsula, with a power outlet at every seat, LCD screen maps and a coveted baby changing station. It’s part of a green vision of Bay Area train travel akin to sleek rail systems in Europe.

“Here you have Swiss design,” said Dietmar Schwarz, who helped design the trains and visited them during an event at San Jose’s Diridon station Tuesday. “And here you have American design,” he added while pointing to Caltrain’s hulking, 30-year-old trains after they screeched into the station. “It’s a different world.”

The project has been decades in the making. It survived years of delays and ran about $500 million over budget, with a final price tag of $2.4 billion. There were even hijinks from President Donald Trump’s administration, which temporarily withheld vast sums of federal dollars. Silicon Valley train riders owe some thanks to the late Republican Sen. Orrin Hatch, who boosted the project due to its economic impact in Utah.

Caltrain’s electrified tracks are planned to be shared with California’s high-speed rail in the coming decades. For now, the top speed is 79 mph — the same as the old trains — with limited time savings coming from faster rates of acceleration and deceleration.

But Caltrain has major hurdles ahead to do away with the older trains and usher in a new era of electrification. Among the agency’s chief problems is a ridership collapse: With only about 40% of pre-pandemic riders returning as of September, Caltrain is in the midst of one of the nation’s worst post-pandemic recoveries.

Without the fare revenue from the affluent Caltrain passengers who once packed trains commuting to tech campuses, the agency is tasked with running a more expensive service with less income. It now expects a budget deficit in the next fiscal year, which will nearly double to $48.5 million annually in late 2024, when electrification takes effect.

That means Caltrain may not have the money to keep these trains running — at least not at the high frequencies promised to riders. Even before the new trains hit the rails, the fiscal crunch could impact service. This is after voters passed a sales tax measure in 2020 to maintain and expand service to the tune of $108 million annually.

“We’ve more than survived the dot-com bust in the early 2000s, the Great Recession in 2008, and here we are at the tail end of the pandemic,” said Michelle Bouchard, Caltrain’s executive director. Now she is pinning the agency’s survival on transforming Caltrain from a commuter-oriented service packed with Bay Area techies into a service that appeals to a wider spectrum of travelers.

In the meantime, service cuts are a possibility. The agency also may turn to the three counties that contribute to operations — Santa Clara, San Mateo and San Francisco — to increase their funding. “I hate to say it,” Bouchard said. “But everything has got to be on the table.”

Besides the cost of operating the trains, which could fall largely on local taxpayers, the agency also has a $410 million budget hole to bring construction to the finish line. Some of the inflated budget is due to pandemic delays and cost escalations. But the agency also fired a contractor and had to redo its signal system, costing over $100 million and adding years to the timeline.

Bouchard said she is confident that the state’s booming budget surplus in large part will meet the funding needs to bring Caltrain’s electrification to the finish line. The agency’s biggest advantage in securing money is that the work is almost done, she said.

“What’s so great about being so close to service is that we not only have a story to tell,” said Bouchard as she stood in the new train’s conductor cabin, encircled by expansive windows, “but we have this.”