With just hours to spare before the opportunity was gone, federal and state child welfare officials signed an agreement to allow federal child welfare money to be spent on an array of services to help keep kids out of foster care.

Both state and federal officials said they hope the changes will provide cheaper and better services for children who get involved with the child welfare system.

“We’re very excited. It’s hard to tell who’s more pleased, the state of California or the Department of Health and Human Services,” said Susan Orr, head of the Children’s Bureau in the federal Administration for Children and Families, which approved the waiver agreement.

The state could start implementing the agreement between October and January, said David Sanders, director of Los Angeles County’s Department of Children and Family Services, which helped the state put together the waiver.

Federal rules allow states to spend federal child welfare dollars only on foster care. But under the agreement signed Friday, up to 20 California counties will be allowed to spend some of their foster care money on services designed to prevent children from going into foster care in the first place and help those who do end up in the system return safely to their families or find a permanent home faster. The agreement could affect as many as 200,000 children each year, state officials said.

State officials said counties that participate in the waiver could spend as much of their federal foster care funding as they wish on such programs. They did not yet know which counties besides Los Angeles will participate.

Alameda County — which helped put the waiver request together — is interested, Social Services Agency head Chet Hewitt said.

The waiver could help the county expand the prevention and early intervention services it already provides. Right now, the programs — which Hewitt credited with helping to lower the county’s child welfare caseload by nearly one-quarter during the past six years — are offered on a limited basis and are paid for with a patchwork of grants, Hewitt said.

California submitted the waiver request two years ago. An earlier waiver agreement from 1997 that covered just a handful of counties, including Alameda, only allowed those counties to use federal money to pay for wraparound services for children, Sanders said. It expired Friday.

Friday was the last day federal child welfare officials were allowed to enter into such agreements. Orr said 18 states had waiver agreements before the deadline, with four more added before her department’s authority to issue them expired.

Under California’s new, five-year agreement, the state’s foster care funding will be capped, with a 2 percent annual increase. Money saved through the program can be reinvested in locals’ child welfare systems.

, instead of being retained by the federal government, state and federal officials said.

The agreement also caps federal funding for adoptions.

Hewitt said the agreement isn’t a total victory for California. The state had asked for twice as much budget growth as it got, and more funded adoption growth.

Cliff Allenby, interim director of the state Department of Social Services, said the agreement is “a fair compromise” between state and federal officials.