American Telephone & Telegraph Co. announced yesterday that it will buy up to a 20 percent stake in Sun Microsystems Inc., a Silicon Valley-based maker of powerful small computers known as workstations.

In addition to providing a capital infusion to the fast-growing smaller company, industry analysts said, the move is a new effort by AT&T to breathe vitality into its troubled venture into computers, this time by helping to refine its UNIX computer operating system.

The two companies already have been working as a team since 1985 to standardize conflicting versions of UNIX. AT&T's buy-in, which will be spread over three years, is not expected to immediately change the companies' current level of cooperation.

But analysts inside and outside AT&T suggested that the move was important as an admission by the mammoth company that in the long run it may have to look to smaller, entrepreneurial companies for technical assistance in computers.

"There's certainly a better chance for payoff out of Sun than out of AT&T's own computer division," said Jack Grubman, a telecommunications analyst for Paine Webber Inc.

Before AT&T's historic breakup in 1984, it was barred from most aspects of the computer business. Divestiture removed those barriers. With the so-called "information age" fast arriving, AT&T's managers viewed this new freedom as one of the brightest spots in their company's future.

However, progress in the field has come slowly. One of many stumbling blocks has been AT&T's attempts to make an industry standard out of UNIX, its main operating system, which is a complex set of commands that directs a computer's basic operations. Individual software programs are written to work on top of the system.

AT&T first developed UNIX for in-house use in 1969. Since the 1970s, it has licensed it out frequently, with licensees often reworking it so that it becomes incompatible with the original -- and in many analysts' eyes, better than the original. The many versions have made outside companies reluctant to write UNIX programs because each can be used by only one set of UNIX users.

Sun, which is based in Mountain View, Calif., has been among the many licensees, revising UNIX for its own line of computer workstations -- devices more advanced than personal computers that are used in a variety of engineering and scientific research applications. The company, which had sales of $538 million in the year ending June 30, has acquired a reputation for knowing UNIX perhaps even better than its originator.

Recently, AT&T and Sun have been working together to merge conflicting versions of UNIX to allow users to easily use software on different machines. AT&T has done the same with another UNIX licensee, software giant Microsoft Corp. The goal is to spur the development of new software programs and to create the "critical mass" to make UNIX a true industry standard.

Under the terms of the agreement, AT&T will buy, at Sun's option and at a 25 percent premium, newly issued shares in an amount to be equal to a 15 percent stake in the company. It will have rights to buy another 5 percent on the open market.

This will give the fast-expanding Sun -- which was founded in 1982 -- a welcome capital infusion. It also will help guard the company against hostile takeover attempts, which AT&T and Sun are anxious to avoid.

Enormous work remains. "We will continue over the course of a few years to bring about the continued conversion of the Sun operating system with AT&T's UNIX," said Robert Holder, director for business development of AT&T's data systems group.

The move came amid other potentially good news for AT&T and UNIX -- a decision by Digital Equipment Corp. and Wang Laboratories Inc. to drop out of the bidding for a $4.5 billion U.S. Air Force contract to supply more than 20,000 powerful desk-top computers. The two had been expected to offer tough competition to AT&T, which is among the other companies after the job.

In seeking bids, the Air Force specified that the equipment be compatible with AT&T's version of UNIX. Last fall, Digital, joined later by Wang, protested to a federal appeals board that this amounted to discrimination in favor of AT&T. The board ruled that it did not, and the two companies dropped out last month, saying they could not hope to compete fairly.